
1-D Array
Practice Questions
size = sizeof(arr)/sizeof(arr[0]);Q1: Reverse Array
Q1: Reverse Arrayvoid ReverseArray(vector<int>& arr, int m){
int s = m + 1; // Condition for reversing whole array s = 0;
int e = arr.size() - 1;
while(s <= e){
swap(arr[s], arr[e]);
s++;
e--;
}
}Q2: Copying two sorted arrays into 3rd Array
Q2: Copying two sorted arrays into 3rd Array void merge(vector<int>& arr1, int m, vector<int>& arr2, int n, vector<int>& arr3){
int i = 0, j = 0, k = 0;
while(i < m && j < n){
if(arr1[i] < arr2[j]){
arr3[k++] = arr1[i++];
}
else {
arr3[k++] = arr2[j++];
}
}
// copy the remaining elements into the arr3
while(i<m){
arr3[k++] = arr1[i++];
}
while(j<n){
arr3[k++] = arr3[j++];
}
}Q4: Move Zeroes
Q4: Move ZeroesApproach 1: Using STL Algorithm
Approach 2:
Case-I True
Sorted -> nuns[ ] = {1, 2, 3, 4, 5}
Case-II True
Sorted-Rotated -> nums[ ] = {3, 4, 5, 1, 2}
Case-III True
Same Element -> nums[ ] = {1, 1, 1, 1}
Case-IV False
Unsorted -> nums[ ] = {2, 1, 3, 4} Must use this I/P
Q8: TwDo Sum
Q8: TwDo SumQ9: Even elements appear first
Q9: Even elements appear firstLast updated
Was this helpful?