Play with an array | Practice | GeeksforGeeks

Given an unsorted array arr of size N, rearrange the elements of array such that number at the odd index is greater than the number at the previous even index. The task is to complete the function formatArray() which will return formatted array.
NOTE:
If the array formed is according to rules print 1, else 0.
Example 1:
Input:
n = 5
arr[] = {5, 4, 3, 2, 1}

Output:
1
Explanation:
The given array after modification
will be as such: 4 5 2 3 1.


Example 2:
Input:
n = 4
arr[] = {4, 3, 2, 1}

Output:
â€‹1
User task:
Since this is a functional problem you don't have to worry about the input, you just have to complete the function formatArray(), which accepts array arr and its size  
Constraints:
1 <= T <= 100
1 <= N <= 100
1 <= arr[i] <= 100
 
0
mayank180919991 week ago
vector<int> formatArray(vector<int> a,int n)
{
//add code here.
       for(int i=0;i<n-1;i=i+2){
           if(a[i]>a[i+1]){
               swap(a[i],a[i+1]);
           }
       }
 return a;
}
-1
akashmarkad22102 months ago
public int[] formatArray(int[] a,int n)   {       // add code here.       Arrays.sort(a);       return a;}
0
patelshivanshu20172 months ago
class GfG{public int[] formatArray(int[] a,int n)       {       // add code here.       for(int i=1;i<n;i+=2){           if(a[i]<a[i-1]){               int temp=a[i];               a[i]=a[i-1];               a[i-1]=temp;           }       }       return a;}}
0
swapniltayal4222 months ago
def formatArray(arr,n):   # add code here   for i in range(1, n ,2):       if (arr[i-1] > arr[i]):           arr[i-1], arr[i] = arr[i], arr[i-1]   return arr
0
akasksingh0802 months ago
JAVA: 
 
class GfG{public int[] formatArray(int[] a,int n)       {       // add code here.              for(int i=1;i<n;i=i+2)       {           if(a[i]<a[i-1])           {              swap(a,i,i-1);           }       }       return a;} public static void swap(int[] a, int x, int y) {     int temp = a[x];     a[x]=a[y];     a[y]=temp; }
}
0
nnamansingh3 months ago
Python code:
def formatArray(a,n):   # add code here   for i in range(1,n,2):       if (a[i-1] > a[i]):           a[i-1],a[i]=a[i],a[i-1]   return a
0
shailendrarana8913 months ago
Java Code
------------
 int temp;       for (int i = 1; i < n ; i = (i + 2)) {           if (a[i] < a[i - 1]) {               temp = a[i];               a[i] = a[i - 1];               a[i - 1] = temp;           }       }              return a;
0
bhavyarajwanshi993 months ago
for(int i=1;i<n;i=(i+2)){         if(a[i]<a[i-1])          swap(a[i-1],a[i]);             }   return a;
0
abhinavsinghbiz3 months ago
//Very naive to do this problem is to simply sort the given //unsorted array!
I have a question, can anybody tell me that why bubble sort do not work in this problem but Insertion sort does!?
Bubble sort gives Run Time error but Insertion sort does not.(Both having same TC:O(N))
Btw here is my Insertion sort that I used to solve this problem.
vector<int> formatArray(vector<int> A,int n){   int key, j;   for (int i = 1; i <= n-1; i++)   {       key = A[i];       j = i-1;       while(j>=0 && A[j] > key){           A[j+1] = A[j];           j--;       }       A[j+1] = key;   }   return A;}
+1
abhinavsinghbiz3 months ago
O(N),C++ Code with 0.0 time taken!
vector<int> formatArray(vector<int> a,int n){   for(int i=1;i<n;i=i+2){       /*Traversing the odd index only*/       if(!(a[i]>a[i-1])){           int temp=a[i];           a[i]=a[i-1];           a[i-1]=temp;       }   }   return a;}
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.