Type the Question

Saturday, February 16, 2019

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

No comments:

Post a Comment

Question Name:TOWER OF HANOI

#include < bits / stdc ++. h > #define lli long long using namespace std ; lli dp [ 202 ]; int main () { int t , n ; ...