Sort in specific order | Practice | GeeksforGeeks

Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.
 
Example 1:
Input:
N = 7
Arr = {1, 2, 3, 5, 4, 7, 10}
Output:
7 5 3 1 2 4 10
Explanation:
Array elements 7 5 3 1 are odd
and sorted in descending order,
whereas 2 4 10 are even numbers
and sorted in ascending order.
 
Example 2:
Input:
N = 7
Arr = {0, 4, 5, 3, 7, 2, 1}
Output:
7 5 3 1 0 2 4
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function leftIndex() which takes the array Arr[] and its size N as inputs and modifies the array Arr[].

Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(N)
 
Constraints:
1 <= N <= 106
0 <= Ai <= 1018
0
codewithshoaib192 weeks ago
public void sortIt(long arr[], long n)   {       //code here.       int oddn=0;       int evenn=0;       for(long i:arr){           if(i%2!=0){               oddn++;           }           else{               evenn++;           }       }       long[]odd=new long[oddn];       long[]even=new long[evenn];       int j=0;       int k=0;       for(int i=0;i<n;i++){           if(arr[i]%2!=0){               odd[j]=arr[i];               j++;           }       }       for(int i=0;i<n;i++){           if(arr[i]%2==0){               even[k]=arr[i];               k++;           }       }       Arrays.sort(odd);       Arrays.sort(even);       reverse(odd);       for(int i=0;i<odd.length;i++){           arr[i]=odd[i];       }       for(int i=0;i<even.length;i++){           arr[(int) oddn+i]=even[i];       }   }   public static void reverse(long[] arr ){       int s=0, e=arr.length-1;       while(s<=e){           swap(arr,s,e);           s++;           e--;       }   }   public static void swap(long[] arr,int s,int e ){       long temp=arr[s];       arr[s]=arr[e];       arr[e]=temp;   }
0
codewithshoaib192 weeks ago
 public void sortIt(long arr[], long n)   {       //code here.       int oddn=0;       int evenn=0;       for(long i:arr){           if(i%2!=0){               oddn++;           }           else{               evenn++;           }       }       long[]odd=new long[oddn];       long[]even=new long[evenn];       int j=0;       int k=0;       for(int i=0;i<n;i++){           if(arr[i]%2!=0){               odd[j]=arr[i];               j++;           }       }       for(int i=0;i<n;i++){           if(arr[i]%2==0){               even[k]=arr[i];               k++;           }       }       Arrays.sort(odd);       Arrays.sort(even);       reverse(odd);       for(int i=0;i<odd.length;i++){           arr[i]=odd[i];       }       for(int i=0;i<even.length;i++){           arr[(int) oddn+i]=even[i];       }       // System.out.println(Arrays.toString(odd));       // System.out.println(Arrays.toString(even));       //  System.out.println(Arrays.toString(arr));   }   public static void reverse(long[] arr ){       int s=0, e=arr.length-1;       while(s<=e){           swap(arr,s,e);           s++;           e--;       }   }   public static void swap(long[] arr,int s,int e ){       long temp=arr[s];       arr[s]=arr[e];       arr[e]=temp;   }
0
mayank20212 weeks ago
C++ : 0.5/2.26 : without using any extra arrayvoid sortIt(long long arr[], long long n)   {              int i=0, j=n-1 ;              while(i<j)       {           if(!(arr[i]&1) && (arr[j]&1) ) swap(arr[i], arr[j]), i++, j--;           if(arr[i]&1) i++ ;                      if(!(arr[j]&1)) j--;                  }              if(arr[i]&1)         {            sort(arr+i+1, arr+n);            sort(arr, arr+i+1);            reverse(arr, arr+i+1);        }       else        {            sort(arr, arr+i);       sort(arr+i, arr+n);       reverse(arr, arr+i);        }                        }
0
yesbro2 weeks ago
class Solution
{
    
    public void sortIt(long arr[], long n)
    {
        //code here.
        ArrayList<Long> array1 = new ArrayList<Long>();
        ArrayList<Long> array2 = new ArrayList<Long>();
        Arrays.sort(arr);
        for(int i=0;i<n;i++){
            if(arr[i] % 2 == 0){
                array1.add(arr[i]);
            }
            else{
                array2.add(arr[i]);
            }
        }
        int x = 0;
        for(int i=array2.size()-1;i>=0;i--){
            arr[x++] = array2.get(i);
        }
        for(int i=0;i<array1.size();i++){
            arr[x++] = array1.get(i);
        }
    }
}
0
nithinkirthick3 weeks ago
Python 1.0/9.3
    def sortIt(self, arr, n):       #code here.       odd=[i for i in arr if i%2>0]       odd.sort(reverse=True)       even=[i for i in arr if i%2==0]       even.sort()       for i ,v in enumerate(list(odd+even)):           arr[i]=v
0
arvind16yadav4 weeks ago
/*JAVA CODE */
class Solution{      public void sortIt(long arr[], long n)   {       //code here.       Arrays.sort(arr);              long brr[] = new long[(int)n];              long left_counter = 0;       long right_counter = n-1;              for(long i = n-1; i>=0; i--){           if(arr[(int)i] % 2 == 0){               brr[(int)right_counter--] = arr[(int)i];           }           else{               brr[(int)left_counter++] = arr[(int)i];           }                  }              for(long i = 0; i < n; i++){           arr[(int)i] = brr[(int)i];       }                 }}
0
hasnainraza1998hr1 month ago
C++
void sortIt(long long arr[], long long n)   {       long long temp[n];       sort(arr,arr+n);       int j = 0,k = n-1;       for(int i=n-1;i>=0;i--){           if(arr[i]%2==0){               temp[k--]=arr[i];           }           else{               temp[j++]=arr[i];           }       }       for(int i=0;i<n;i++){           arr[i]=temp[i];       }   }
0
dangrio2 months ago
static bool compareOddEven(int a, int b){       if(a%2 == 0 && b%2==0) return a<b;       if(a%2 != 0 && b%2 != 0) return b<a;       if( a%2 != 0) return true;       else return false;   }
void sortIt(long long arr[], long long n)   {sort(arr,arr+n,compareOddEven);   }
 
0
dangrio2 months ago
Time Taken = 0.5s
 
for(int i=0;i<n;i++) if(arr[i]%2) arr[i] = (-1)*arr[i];       sort(arr,arr+n);       int i = 0;       while(arr[i]<0){           arr[i] = (-1)*arr[i];           i++;       }
0
dangrio2 months ago
Time Taken = 0.5s
 
long long left = 0, right = n-1, k=0;       while(left<right){           //find the first even number           while(arr[left]%2 != 0){               left++, k++;           }           //Finding the odd number from the right side           while(arr[right]%2 == 0) right--;           if(left<right) swap(arr[left], arr[right]);       }       sort(arr,arr+k,greater<long long>());       sort(arr+k,arr+n);
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.