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
![](http://www.fcukthecode.com/wp-content/uploads/2022/01/Screenshot-from-2022-01-16-20-05-56.png)
Morae Q!
- Find the length of the array’s longest increasing sub-sequence.
- Arrange numbers in a circle so that any two neighbouring numbers differ by 1.
- Find partial names and count the total numbers.
- Find the minimized sum of non-deleted elements of the array after the end of the game.
- Find the maximum number of good sleeping times optimally.
- Sort the elements of the array in the order in which they are required.
- Find the minimum number of flats, monkey needs to visit to catch ninjas.
- Convert the square matrix to matrix in Z form.
- Shift the K elements of each row to right of the matrix.
- Find maximum possible number of students in a balanced team with skills.
- Find the Maximum number of pairs of points you can match with each other.
- Calculate the number of non-empty good subarrays of given array.
- Transform the binary string A into the string B using finite operations.
- Find the number of potion must the character take to jump the hurdles.
- Count the total number of vowels and consonants.
- Return all elements of the matrix in spiral order.
- Return all palindromic paths of the matrix.
- Write the code to change the display in reverse order using pointer.
- Find the number of days the expedition can last.
- Find the minimum size of the sub-segment to make the array pairwise distinct.