1598695200
Given a sorted array arr[] of N integers, The task is to find the multiple missing elements in the array between the ranges [arr[0], arr[N-1]].
Examples:
Input:_ arr[] = {6, 7, 10, 11, 13}_
_Output: _8 9 12
Explanation:
The elements of the array are present in the range of the maximum and minimum array element [6, 13]. Therefore, the total values will be {6, 7, 8, 9, 10, 11, 12, 13}.
The elements from the above range which are missing from the array are {8, 9, 12}.
Input:_ arr[] = {1, 2, 4, 6}_
_Output: _3 5
Naive Approach: The naive idea is to iterate over the difference between the consecutive pair of elements and the print all the numbers in this range if difference is non-zero. Below are the steps:
Below is the implementation of the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
// Function to find the missing elements
**void**
printMissingElements(``**int**
arr[],
**int**
N)
{
// Initialize diff
**int**
diff = arr[0] - 0;
**for**
(``**int**
i = 0; i < N; i++) {
// Check if diff and arr[i]-i
// both are equal or not
**if**
(arr[i] - i != diff) {
// Loop for consecutive
// missing elements
**while**
(diff < arr[i] - i) {
cout << i + diff <<
" "``;
diff++;
}
}
}
}
// Driver Code
**int**
main()
{
// Given array arr[]
**int**
arr[] = { 6, 7, 10, 11, 13 };
**int**
N =
**sizeof**``(arr) /
**sizeof**``(``**int**``);
// Function Call
printMissingElements(arr, N);
**return**
0;
}
Output:
8 9 12
Time Complexity:_ O(N2)_
Auxiliary Space:_ O(1)_
Efficient Approach: The idea is to use Hashing to optimize the above approach. Create a boolean array(say b[]) of size equal to the maximum element in the array and mark only those position in the array b[] which are present in the given array. Print all the index in the array b[] that are not marked.
Below are the steps:
Below is the implementation of the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
// Function to find the missing elements
**void**
printMissingElements(``**int**
arr[],
**int**
N)
{
// Initialize an array with zero
// of size equals to the maximum
// element in the array
**int**
b[arr[N - 1] + 1] = { 0 };
// Make b[i]=1 if i is present
// in the array
**for**
(``**int**
i = 0; i < N; i++) {
// If the element is present
// make b[arr[i]]=1
b[arr[i]] = 1;
}
// Print the indices where b[i]=0
**for**
(``**int**
i = arr[0]; i <= arr[N - 1]; i++) {
**if**
(b[i] == 0) {
cout << i <<
" "``;
}
}
}
// Driver Code
**int**
main()
{
// Given array arr[]
**int**
arr[] = { 6, 7, 10, 11, 13 };
**int**
N =
**sizeof**``(arr) /
**sizeof**``(``**int**``);
// Function Call
printMissingElements(arr, N);
**return**
0;
}
Output:
8 9 12
Time Complexity:_ O(M), where M is the maximum element of the array._
Auxiliary Space:_ O(M)_
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
#arrays #greedy #hash #mathematical #frequency-counting #hashtable
1603846800
Given two sorted arrays, arr[], brr[] of size N, and M, the task is to merge the two given arrays such that they form a sorted sequence of integers combining elements of both the arrays.
Examples:
Input:_ arr[] = {10}, brr[] = {2, 3}_
Output: 2 3 10
Explanation:_ The merged sorted array obtained by taking all the elements from the both the arrays is {2, 3, 10}._
Therefore, the required output is 2 3 10.
Input:_ arr[]__= {1, 5, 9, 10, 15, 20}, brr[] = {2, 3, 8, 13}_
Output:_ 1 2 3 5 8 9 10 13 15 20_
Naive Approach: Refer to Merge two sorted arrays for the simplest approach to merge the two given arrays.
_Time Complexity: _O(N * M)
Auxiliary Space:_ O(1)_
Space Optimized Approach: Refer to Merge two sorted arrays with O(1) extra space to merge the two given arrays without using any extra memory.
Time Complexity:_ O(N * M)_
Auxiliary Space:_ O(1)_
Efficient Space Optimized Approach: Refer to Efficiently merging two sorted arrays with O(1) extra space to merge the two given array without using any extra memory.
Time Complexity:_ O((N + M) * log(N + M))_
Auxiliary Space:_ O(1)_
Heap** – based Approach:** The problem can be solved using Heap. The idea is to traverse arr[] array and compare the value of arr[i] with brr[0] and check if arr[i] is greater than **brr[0] **or not. If found to be true then swap(arr[i], brr[0) and perform the heapify operation on brr[]. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
// Function to perform min heapify
// on array brr[]
**void**
minHeapify(``**int**
brr[],
**int**
i,
**int**
M)
{
// Stores index of left child
// of i.
**int**
left = 2 * i + 1;
// Stores index of right child
// of i.
**int**
right = 2 * i + 2;
// Stores index of the smallest element
// in (arr[i], arr[left], arr[right])
**int**
smallest = i;
// Check if arr[left] less than
// arr[smallest]
**if**
(left < M && brr[left] < brr[smallest]) {
// Update smallest
smallest = left;
}
// Check if arr[right] less than
// arr[smallest]
**if**
(right < M && brr[right] < brr[smallest]) {
// Update smallest
smallest = right;
}
// if i is not the index
// of smallest element
**if**
(smallest != i) {
// Swap arr[i] and arr[smallest]
swap(brr[i], brr[smallest]);
// Perform heapify on smallest
minHeapify(brr, smallest, M);
}
}
// Function to merge two sorted arrays
**void**
merge(``**int**
arr[],
**int**
brr[],
**int**
N,
**int**
M)
{
// Traverse the array arr[]
**for**
(``**int**
i = 0; i < N; ++i) {
// Check if current element
// is less than brr[0]
**if**
(arr[i] > brr[0]) {
// Swap arr[i] and brr[0]
swap(arr[i], brr[0]);
// Perform heapify on brr[]
minHeapify(brr, 0, M);
}
}
// Sort array brr[]
sort(brr, brr + M);
}
// Function to print array elements
**void**
printArray(``**int**
arr[],
**int**
N)
{
// Traverse array arr[]
**for**
(``**int**
i = 0; i < N; i++)
cout << arr[i] <<
" "``;
}
// Driver Code
**int**
main()
{
**int**
arr[] = { 2, 23, 35, 235, 2335 };
**int**
brr[] = { 3, 5 };
**int**
N =
**sizeof**``(arr) /
**sizeof**``(arr[0]);
**int**
M =
**sizeof**``(brr) /
**sizeof**``(brr[0]);
// Function call to merge
// two array
merge(arr, brr, N, M);
// Print first array
printArray(arr, N);
// Print Second array.
printArray(brr, M);
**return**
0;
}
Output:
2 3 5 23 35 235 2335
Time Complexity:_ O((N + M) * log (M))_
Auxiliary Space:_ O(1)_
Efficient Approach: Refer to merge two sorted arrays to efficiently merge the two given arrays.
_Time Complexity: _O(N + M)
Auxiliary Space:_ O(N + M)_
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
#arrays #divide and conquer #heap #mathematical #sorting #amazon #amdocs #array-merge #brocade #goldman sachs #heap sort #juniper networks #priority-queue #snapdeal
1603839600
Given two sorted arrays, arr[], brr[] of size N, and M, the task is to merge the two given arrays such that they form a sorted sequence of integers combining elements of both the arrays.
Examples:
Input:_ arr[] = {10}, brr[] = {2, 3}_
Output: 2 3 10
Explanation:_ The merged sorted array obtained by taking all the elements from the both the arrays is {2, 3, 10}._
Therefore, the required output is 2 3 10.
Input:_ arr[]__= {1, 5, 9, 10, 15, 20}, brr[] = {2, 3, 8, 13}_
Output:_ 1 2 3 5 8 9 10 13 15 20_
Naive Approach: Refer to Merge two sorted arrays for the simplest approach to merge the two given arrays.
_Time Complexity: _O(N * M)
Auxiliary Space:_ O(1)_
Space Optimized Approach: Refer to Merge two sorted arrays with O(1) extra space to merge the two given arrays without using any extra memory.
Time Complexity:_ O(N * M)_
Auxiliary Space:_ O(1)_
Efficient Space Optimized Approach: Refer to Efficiently merging two sorted arrays with O(1) extra space to merge the two given array without using any extra memory.
Time Complexity:_ O((N + M) * log(N + M))_
Auxiliary Space:_ O(1)_
Partition** – based Approach: **The idea is to consider the (N + 1)th element of the final sorted array as a pivot element and perform the quick sort partition around the pivot element. Finally, store the first N smaller elements of the final sorted array into the array, arr[] and the last M greater elements of the final sorted array into the array, brr[] in any order and sort both these arrays separately. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
// Function to perform the partition
// around the pivot element
**void**
partition(``**int**
arr[],
**int**
N,
**int**
brr[],
**int**
M,
**int**
Pivot)
{
// Stores index of each element
// of the array, arr[]
**int**
l = N - 1;
// Stores index of each element
// of the array, brr[]
**int**
r = 0;
// Traverse both the array
**while**
(l >= 0 && r < M) {
// If pivot is
// smaller than arr[l]
**if**
(arr[l] < Pivot)
l--;
// If Pivot is
// greater than brr[r]
**else**
**if**
(brr[r] > Pivot)
r++;
// If either arr[l] > Pivot
// or brr[r] < Pivot
**else**
{
swap(arr[l], brr[r]);
l--;
r++;
}
}
}
// Function to merge
// the two sorted array
**void**
Merge(``**int**
arr[],
**int**
N,
**int**
brr[],
**int**
M)
{
// Stores index of each element
// of the array arr[]
**int**
l = 0;
// Stores index of each element
// of the array brr[]
**int**
r = 0;
// Stores index of each element
// the final sorted array
**int**
index = -1;
// Stores the pivot element
**int**
Pivot = 0;
// Traverse both the array
**while**
(index < N && l < N && r < M) {
**if**
(arr[l] < brr[r]) {
Pivot = arr[l++];
}
**else**
{
Pivot = brr[r++];
}
index++;
}
// If pivot element is not found
// or index < N
**while**
(index < N && l < N) {
Pivot = arr[l++];
index++;
}
// If pivot element is not found
// or index < N
**while**
(index < N && r < M) {
Pivot = brr[r++];
index++;
}
// Place the first N elements of
// the sorted array into arr[]
// and the last M elements of
// the sorted array into brr[]
partition(arr, N, brr,
M, Pivot);
// Sort both the arrays
sort(arr, arr + N);
sort(brr, brr + M);
// Print the first N elements
// in sorted order
**for**
(``**int**
i = 0; i < N; i++)
cout << arr[i] <<
" "``;
// Print the last M elements
// in sorted order
**for**
(``**int**
i = 0; i < M; i++)
cout << brr[i] <<
" "``;
}
// Driver Code
**int**
main()
{
**int**
arr[] = { 1, 5, 9 };
**int**
brr[] = { 2, 4, 7, 10 };
**int**
N =
**sizeof**``(arr) /
**sizeof**``(arr[0]);
**int**
M =
**sizeof**``(brr) /
**sizeof**``(brr[0]);
Merge(arr, N, brr, M);
**return**
0;
}
Output:
1 2 4 5 7 9 10
Time Complexity:_ O((N + M)log(N + M))_
_Auxiliary Space: _O(1)
Efficient Approach: Refer to merge two sorted arrays to efficiently merge the two given arrays.
_Time Complexity: _O(N + M)
Auxiliary Space:_ O(N + M)_
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
#arrays #divide and conquer #mathematical #sorting #amazon #amdocs #array-merge #brocade #goldman sachs #juniper networks #linkedin #partition #quick sort #snapdeal
1598695200
Given a sorted array arr[] of N integers, The task is to find the multiple missing elements in the array between the ranges [arr[0], arr[N-1]].
Examples:
Input:_ arr[] = {6, 7, 10, 11, 13}_
_Output: _8 9 12
Explanation:
The elements of the array are present in the range of the maximum and minimum array element [6, 13]. Therefore, the total values will be {6, 7, 8, 9, 10, 11, 12, 13}.
The elements from the above range which are missing from the array are {8, 9, 12}.
Input:_ arr[] = {1, 2, 4, 6}_
_Output: _3 5
Naive Approach: The naive idea is to iterate over the difference between the consecutive pair of elements and the print all the numbers in this range if difference is non-zero. Below are the steps:
Below is the implementation of the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
// Function to find the missing elements
**void**
printMissingElements(``**int**
arr[],
**int**
N)
{
// Initialize diff
**int**
diff = arr[0] - 0;
**for**
(``**int**
i = 0; i < N; i++) {
// Check if diff and arr[i]-i
// both are equal or not
**if**
(arr[i] - i != diff) {
// Loop for consecutive
// missing elements
**while**
(diff < arr[i] - i) {
cout << i + diff <<
" "``;
diff++;
}
}
}
}
// Driver Code
**int**
main()
{
// Given array arr[]
**int**
arr[] = { 6, 7, 10, 11, 13 };
**int**
N =
**sizeof**``(arr) /
**sizeof**``(``**int**``);
// Function Call
printMissingElements(arr, N);
**return**
0;
}
Output:
8 9 12
Time Complexity:_ O(N2)_
Auxiliary Space:_ O(1)_
Efficient Approach: The idea is to use Hashing to optimize the above approach. Create a boolean array(say b[]) of size equal to the maximum element in the array and mark only those position in the array b[] which are present in the given array. Print all the index in the array b[] that are not marked.
Below are the steps:
Below is the implementation of the above approach:
// C++ program for the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
// Function to find the missing elements
**void**
printMissingElements(``**int**
arr[],
**int**
N)
{
// Initialize an array with zero
// of size equals to the maximum
// element in the array
**int**
b[arr[N - 1] + 1] = { 0 };
// Make b[i]=1 if i is present
// in the array
**for**
(``**int**
i = 0; i < N; i++) {
// If the element is present
// make b[arr[i]]=1
b[arr[i]] = 1;
}
// Print the indices where b[i]=0
**for**
(``**int**
i = arr[0]; i <= arr[N - 1]; i++) {
**if**
(b[i] == 0) {
cout << i <<
" "``;
}
}
}
// Driver Code
**int**
main()
{
// Given array arr[]
**int**
arr[] = { 6, 7, 10, 11, 13 };
**int**
N =
**sizeof**``(arr) /
**sizeof**``(``**int**``);
// Function Call
printMissingElements(arr, N);
**return**
0;
}
Output:
8 9 12
Time Complexity:_ O(M), where M is the maximum element of the array._
Auxiliary Space:_ O(M)_
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
#arrays #greedy #hash #mathematical #frequency-counting #hashtable
1598014800
Given an array of integers arr[] and a binary string str of length N, the task is to rearrange given array by swapping array elements from indices having the same character in the string, such that the number formed by the elements of the rearranged array as digits is the maximum possible.
Examples:
Input:_ arr[]={1, 3, 4, 2}, str=”0101”_
Output:_ 4 3 1 2_
Explanation:
Since arr[0] is less than arr[2], so swap them. Therefore the maximum possible number from the array is 4, 3, 1, 2.
Input:_ arr[] = { 1, 3, 456, 6, 7, 8 }, str = “101101”_
Output:_ 8 7 6 456 3 1_
Explanation:
Array elements present at 0-chractered indices: {3, 7}
Largest number that can be formed from the above two numbers is 73
Array elements present at 1-chractered indices: {1, 456, 6, 8}
Largest number that can be formed from the above two numbers is 864561
Therefore, maximum number that can be generated from the array is 87645631
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
**using**
**namespace**
std;
// Comparison Function to sort()
**int**
myCompare(``**int**
a,
**int**
b)
{
string X = to_string(a);
string Y = to_string(b);
// Append Y at the end of X
string XY = X.append(Y);
// Append X at the end of Y
string YX = Y.append(X);
// Compare and return greater
**return**
XY.compare(YX) < 0 ? 1 : 0;
}
// Function to return the rearranged
// array in the form of largest
// possible number that can be formed
**void**
findMaxArray(vector<``**int**``>& arr, string& str)
{
**int**
N = arr.size();
vector<``**int**``> Z, O, ans(N);
**for**
(``**int**
i = 0; i < N; i++) {
**if**
(str[i] ==
'0'``) {
Z.push_back(arr[i]);
}
**else**
{
O.push_back(arr[i]);
}
}
// Sort them in decreasing order
sort(Z.rbegin(), Z.rend(), myCompare);
sort(O.rbegin(), O.rend(), myCompare);
**int**
j = 0, k = 0;
// Generate the sorted array
**for**
(``**int**
i = 0; i < N; i++) {
**if**
(str[i] ==
'0'``) {
ans[i] = Z[j++];
}
**else**
{
ans[i] = O[k++];
}
}
**for**
(``**int**
i = 0; i < N; i++) {
cout << ans[i] <<
" "``;
}
}
// Driver Code
**int**
main()
{
vector<``**int**``> arr = { 1, 3, 456, 6, 7, 8 };
string str =
"101101"``;
findMaxArray(arr, str);
**return**
0;
}
Output:
8 7 6 456 3 1
Time Complexity:_ O(NlogN)_
Auxiliary Space:_ O(N)_
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
#arrays #mathematical #sorting #array-rearrange
1599645721
As Bubble Sort represents the basic foundations of sorting, we’ve tried to demystify this sorting algorithm.
#bubble-sort #sorting #sorting-algorithm #array #practice