void reverseArray(vector<int> &arr , int m) {
int s = m+1;
int e = arr.size() - 1;
int a;
while(s<=e){
a = arr[s];
arr[s]=arr[e];
arr[e]=a;
s++;
e--;
}
}
Problem of the day



Given an array/list 'ARR' of integers and a position ‘M’. You have to reverse the array after that position.
Example:
We have an array ARR = {1, 2, 3, 4, 5, 6} and M = 3 , considering 0
based indexing so the subarray {5, 6} will be reversed and our
output array will be {1, 2, 3, 4, 6, 5}.
The very first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of every test case contains one integer ‘N’ where ‘N’ denotes the number of elements and an integer ‘M’ which denotes after which position the array has to be reversed.
The second line of every test case contains ‘N’ space-separated integers which denote the elements of input of array/list.
Output format:
For each test case, return the required array.
Output for each test case is printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Consider 0-based indexing of the array.
1 <= T <= 10
0 <= M <= N <= 5*10^4
-10^9 <= ARR[i] <= 10^9
Time Limit: 1 sec
2
6 3
1 2 3 4 5 6
5 2
10 9 8 7 6
1 2 3 4 6 5
10 9 8 6 7
For the first test case,
Considering 0-based indexing we have M = 3 so the
subarray[M+1 … N-1] has to be reversed.
Therefore the required output will be {1, 2, 3, 4, 6, 5}.
For the second test case,
Considering 0-based indexing we have M = 2 so the
subarray[M+1 … N-1] has to be reversed.
Therefore the required output will be {10, 9, 8, 6, 7}.
2
7 3
1 4 5 6 6 7 7
9 3
10 4 5 2 3 6 1 3 6
1 4 5 6 7 7 6
10 4 5 2 6 3 1 6 3
1. Try to think by creating another array
2. Try to think which elements are beign swapped.
Approach:
Algorithm:
O(N), where ‘N’ is the number of elements in the array.
Traversing over (M) elements will take O(M) time. Again traversing the array in the reverse direction over (N-M-1) elements will take (N-M-1) time. Thus total time complexity will be O(N).
O(N)
Another array is created for reversing the elements.
Interview problems
best code in cpp , faster than 100%
void reverseArray(vector<int> &arr , int m) {
int s = m+1;
int e = arr.size() - 1;
int a;
while(s<=e){
a = arr[s];
arr[s]=arr[e];
arr[e]=a;
s++;
e--;
}
}
Interview problems
simple way in c/c++
void reverseArray(vector<int> &arr , int m) {
int s = m+1,e=arr.size()-1;
while(s<=e)
{
swap(arr[s],arr[e]);
s++;e--;
}
Interview problems
(TWO POINTER)16ms run time better than 99% solution most basic example
void reverseArray(vector<int> &arr , int m) {
int start = m+1;
int end = arr.size();
int mid = (start + end)/2;
while( start < mid){
swap(arr[start],arr[end-1]);
start++;
end--;
}
}Interview problems
void reverseArray(vector<int> &arr , int m) { int n = sizeof(arr)/sizeof(arr[0]); int b[n]; for(int i =0;i<n;i++){ if(i<=m){b[i]=arr[i];} else { b[n+m-i] = arr[i]; } } for(int j=0;j<n;j++){ cout << b[j]; } }
what is wrong in my approach
Interview problems
Use built-in STL function
#include<algorithm>
void reverseArray(vector<int> &arr , int m) {
// Write your code here
reverse(arr.begin()+m+1,arr.end());
}
Interview problems
java code
import java.util.* ;
import java.io.*;
import java.util.ArrayList;
public class Solution
{
public static void reverseArray(ArrayList<Integer> arr, int m)
{
// Write your code here.
int left = m+1;
int right = arr.size()-1;
while(left<right){
int temp =arr.get(left);
arr.set(left,arr.get(right));
arr.set(right,temp);
left++;
right--;
}
}
}
Interview problems
Using two pointer concept
void reverseArray(vector<int> &arr , int m) {
// Write your code here
int n=arr.size();
int i=m+1,j=n-1;
while(i<=j){
swap(arr[i],arr[j]);
i++;
j--;
}
} Interview problems
Reverse an array using two pointer approach
void reverseArray(vector<int> &arr , int m) {
// Write your code here
int start = m+1;
int end=arr.size()-1;
while(start<end){
int temp = arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
}
}Interview problems
Reverse The Array
import java.util.* ;
import java.io.*;
import java.util.ArrayList;
public class Solution
{
public static void reverseArray(ArrayList<Integer> arr, int m)
{
// Write your code here.
int start=m+1;
int end=arr.size()-1;
while(start<end){
int temp=arr.get(start);
arr.set(start,arr.get(end));
arr.set(end,temp);
start++;
end--;
}
}
}
Interview problems
easy💯 || Simple 🧠 || java || O(n)
import java.util.* ;
import java.io.*;
import java.util.ArrayList;
public class Solution
{
public static void reverseArray(ArrayList<Integer> arr, int m)
{
// Write your code here.
int left=m+1;
int right=arr.size()-1;
while(left<right)
{
int temp=arr.get(left);
arr.set(left,arr.get(right));
arr.set(right,temp);
left++;
right--;
}
}
}