Rearrange Array Alternately | Practice | GeeksforGeeks

Given a sorted array of positive integers. Your task is to rearrange  the array elements alternatively i.e first element should be max value, second should be min value, third should be second max, fourth should be second min and so on.
Example 1:
Input:
N = 6
arr[] = {1,2,3,4,5,6}
Output: 6 1 5 2 4 3
Explanation: Max element = 6, min = 1, 
second max = 5, second min = 2, and 
so on... Modified array is : 6 1 5 2 4 3.
Example 2:
Input:
N = 11
arr[]={10,20,30,40,50,60,70,80,90,100,110}
Output:110 10 100 20 90 30 80 40 70 50 60
Explanation: Max element = 110, min = 10, 
second max = 100, second min = 20, and 
so on... Modified array is : 
110 10 100 20 90 30 80 40 70 50 60.

Your Task:
The task is to complete the function rearrange() which rearranges elements as explained above. Printing of the modified array will be handled by driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 106
1 <= arr[i] <= 107
0
mihirdhabe1 day ago
Easy C++ Solution
void rearrange(long long *arr, int n)    {         // Your code here    vector<int>v;    for(int i=0;i<n;i++)    {        v.push_back(arr[i]);    }    int i=0;    int j=1;       while(i<n)       {           arr[i]=v[v.size()-j];           i+=2;           j++;       }       i=1;       j=0;       while(i<n)       {           arr[i]=v[j];           i+=2;           j++;       }        }
0
bhavikkasundra551 week ago
if(n==1)return ;    vector<long long>v;    long long left=0,right=n-1;    int count=0;    while(left<=right){       // cout<<arr[right]<<" ";       v.push_back(arr[right]);        right--;       // cout<<arr[left]<<" ";       v.push_back(arr[left]);        left++;    }    for(long long i=0;i<n;i++){       arr[i] = v[i];    }
0
rishabh21cse1 week ago
best c++ sol ---------------------
 void rearrange(long long *arr, int n)    {         // Your code here    int i=0;    int j = n-1;        vector<int> v;    while(i<=j){        if(i==j){            v.push_back(arr[j]);        }        v.push_back(arr[j]);        v.push_back(arr[i]);        i++;        j--;    }              for(int i=0;i<v.size();i++){           arr[i]=v[i];       }   }
+1
daniilb2 weeks ago
    void rearrange(long long *arr, int n)    {       // this approach is based on calculating the destination position       // for a given element, moving it there, and repeating this        // procedure for the just replaced element until all elements        // are placed in their required positions.       // To track that an element has been moved we change its sign              //find mid point that will go to the end       int mid = (n-1)/2;           for (int i = 0; i < n; ++i) {        if (arr[i] > 0) {            int i_curr = i;            int saved = arr[i_curr];                        while (1) {                                // compute destination of the current element                int i_next;                                if (i_curr == mid) {                    i_next = n-1;                } else if (i_curr < mid) {                    i_next = 2*i_curr+1;                } else {                    i_next = 2*(n-1-i_curr);                }                                if (arr[i_next] < 0) { // found a loop                    break;                } else {                    // move the current element to its destination                    // but save value at the destination beforehand                    int tmp = arr[i_next];                    arr[i_next] = -saved;                    saved = tmp;                    i_curr = i_next;                }            }        }    }        // flip all numbers back to being positive    for (int i = 0; i < n; ++i) {        arr[i] = -arr[i];    }        }
-1
sachinsharma291112 weeks ago
class Solution:   ##Complete this function   #Function to rearrange  the array elements alternately.   def rearrange(self,arr, n):        ##Your code here       max_index=n-1       min_index=0       maximum=arr[n-1]+1       for i in range(n):           if (i%2==0):               arr[i]=(arr[max_index]%maximum)*maximum+arr[i]               max_index=max_index-1           else:               arr[i]=(arr[min_index]%maximum)*maximum+arr[i]               min_index=min_index+1       for i in range(n):           arr[i]=arr[i]//maximum
0
mayank180919992 weeks ago
   void rearrange(long long *arr, int n) 
    { 
    	
    	// Your code here
    	int i=0;
    	int j=n-1;
    	vector<int>nums;
    	while(i<=j){
    	    if(i==j){
    	     nums.push_back(arr[j]);
    	    }
    	    nums.push_back(arr[j]);
    	    nums.push_back(arr[i]);
    	    i++;
    	    j--;
    	}
    	for(int i=0;i<nums.size();i++){
    	    arr[i]=nums[i];
    	}
    	 
    }
0
user_990i2 weeks ago
void rearrange(long long *arr, int n)    {        long long prime=1000000007;       //prime*rearranged+original       long long high=n-1,low=0;       for(int i=0;i<n;i++){           long long l=(arr[low]%prime);           long long h=(arr[high]%prime);           if(i%2==0){              arr[i]=h*prime+arr[i];               high--;                         }                      else{               arr[i]=l*prime+arr[i];              low++;           }                  }       for(int i=0;i<n;i++){           arr[i]=arr[i]/prime;       }                   }};
0
itskiran
This comment was deleted.
0
souvik404d
This comment was deleted.
0
hk63017359392 weeks ago
JAVA SOLUTION..............  
public static void rearrange(long arr[], int n){              ArrayList<Long> o1 = new ArrayList<>();       for(int i=0;i<n;i++){           o1.add(arr[n-1-i]);           o1.add(arr[i]);       }       for(int i=0;i<n;i++){           arr[i]=o1.get(i);       }          }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.