Question Name:Sort me this way !

#include <stdio.h>
int main()
{
int avi,b[10],i,j,tem,o,d[10],c=0;
  o=0;
  scanf("%d",&avi);
  for(i=0;i<avi;i++) { 
    scanf("%d",&b[i]);
  }  for(i=0;i<avi;i++) 
    { 
      if(b[i]<0) { d[o]=b[i]; b[i]=b[i]-d[o]*2;o++;c++;}
  }
    for(i=0;i<avi;i++) 
    { 
  for(j=i+1;j<avi;j++)
  { 
    
if(b[i]>b[j]) 
{ 
  tem=b[j];
  b[j]=b[i];
  b[i]=tem;
} 
  } 
    } 
    for(i=0;i<avi;i++) {
      for(j=0;j<c;j++) { 
      if(b[i]+d[j]==0) 
    { 
      b[i]=d[j];
    } 
    } 
    }
    for(i=0;i<avi;i++) { 
        
      printf("%d ",b[i]);
    } 

  
	return 0;
}
  • Problem Description
    Given an array A consisting of integers of size N, you need to sort this array in non-decreasing order on the basis of the absolute value of the integers in the array. Print the sorted array to output then.

    Input:

    The first line consists of a single integer N, the number of elements in the array. The next line consists of N space separated elements. No two elements in the array will have same absolute value.

    Output:

    You need to print the absolute sorted array. See the sample output for clarification.

    Constraints:

    1<=N<=10^5

    -10^9<= A[i] <= 10^9 A[i] is the ith element of the array.
  • Test Case 1
    Input (stdin)8
    5 6 1 2 8 9 3 4
    Expected Output1 2 3 4 5 6 8 9
  • Test Case 2
    Input (stdin)10
    9 -10 -11 20 1 2 -3 4 -5 6
    Expected Output1 2 -3 4 -5 6 9 -10 -11 20

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro