Menu Close

Each element in this array is said to have a Special Weight…[Cprogm]FTC

Ramesh have been given an array A of size N and an integer K. This array consists of N integers ranging from 1 to 10^7. Each element in this array is said to have a Special Weight.

The special weight of an element a[ i ] is a[ i ] % k.

Ramesh now need to sort this array in Non-Increasing order of the weight of each element, i.e the element with the highest weight should appear first, then the element with the second highest weight and so on. In case two elements have the same weight, the one with the lower value should appear in the output first.

Input:
The first line consists of two integers N and K. The next line consists of N space separated integers denoting the elements of array A.

Output:
Print N space separated integers denoting the elements of the array in the order in which they are required.

#include <stdio.h>
#include <stdlib.h>
void count(int a[],int n, int k){
  int *f,*temp,i;
  temp=(int*)malloc(n*sizeof(int));
  f=(int*)calloc(k,sizeof(int));
  for(i=0;i<n;i++)
  f[a[i]%k]++;
  for(i=k-2;i>=0;i--)
  f[i]=f[i]+f[i+1];
  for(i=n-1;i>=0;i--){
  temp[f[a[i]%k]-1]=a[i];
  f[a[i]%k]--;}
  for(i=0;i<n;i++)
  printf("%d ",temp[i]);
}
void sort(int a[],int n,int k,int m){
  int *temp,*f,i;
  f=(int*)calloc(m+1,sizeof(int));
  temp=(int*)malloc(n*sizeof(int));
  for(i=0;i<n;i++)
  f[a[i]]++;
  for(i=1;i<=m;i++)
  f[i]=f[i]+f[i-1];
  for(i=n-1;i>=0;i--){
  temp[f[a[i]]-1]=a[i];
  f[a[i]]--;
  }
  count(temp,n,k);
}
int main()
{
  int n,k,i,*a,max=0;
  scanf("%d %d",&n,&k);
   a=(int*)malloc(n*sizeof(int));
   for(i=0;i<n;i++){
  scanf("%d",&a[i]);
  if(max<a[i])
  max=a[i];
   }
   sort(a,n,k,max);
return 0;}


INPUT_1:
6  2
7  1  2  3  4  5

OUTPUT:
1  3  5  7  2  4


INPUT_2:
7  3
7  1  2  3  4  5  8

OUTPUT:
2  5  8  1  4  7  3


INPUT_3:
5  2
1  2  3  4  5

OUTPUT:
1  3  5  2  4


ILLUSTRATION

Executed using gcc