Menu Close

Sort the array in non-decreasing order and print out the original indices…[Cprogm]FTC

Amrish is a brilliant student who has a huge interest in coding. So his friend Dev gave an array A of non-negative integers of size m to Amrish and also you. 

So your task is to sort the array in non-decreasing order and print out the original indices of the new sorted array.

Assume the input array as A = { 4, 5, 3, 7, 1 }. ( indices = { 0, 1, 2, 3, 4 } )

After sorting the new array becomes A = { 1, 3, 4, 5, 7 }. ( indices = { 4, 2, 0, 1, 3 } )

The required output should be “4 2 0 1 3”  (Without Quotes), these are the original indices.
NOTE: The indexing of the array starts with 0.

Input:
The first line of input consists of the size of the array

The next line consists of the array of size m


Output:
Output consists of a single line of integers

#include <stdio.h>
int main()
{
  int n,a[100],b[100],i,j,t;
  scanf("%d",&n);
  n=n+1;
  for(i=0;i<n-1;i++)
  {
  scanf("%d",&a[i]);
  b[i]=a[i];
  }
  for(i=0;i<n-1;i++)
  {
  for(j=i+1;j<n-1;j++)
  {
  if(a[i]>a[j])
  {
  t=a[i];
  a[i]=a[j];
  a[j]=t;
  }}}
  for(i=0;i<n-1;i++)
  {
  for(j=0;j<n-1;j++)
  if(a[i]==b[j]) printf("%d ",j);
  }
return 0;
}


INPUT_1:
7
18  45  98  30  12  85  6

OUTPUT:
6  4  0  3  1  5  2


INPUT_2:
8
8  2  7  9  17  36  22  90

OUTPUT:
1  2  0  3  4  6  5  7


INPUT_3:
5
5  4  2  1  3

OUTPUT:
3  2  4  1  0


INPUT_4:
10
11  55  99  5  2  0  1  5  6  78

OUTPUT:
5  6  4  3  7  3  7  8  0  1  9  2


ILLUSTRATION

Executed using gcc