Page cover

1-D Array

Practice Questions

void 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

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++];
    }
}

Approach 1: Using STL Algorithm

Approach 2:

Q9: Even elements appear first

Last updated

Was this helpful?