In this HackerEarth Descending Weights problem solution You 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.

You 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.


HackerEarth Descending Weights problem solution


HackerEarth Descending Weights problem solution.

#include<bits/stdc++.h>

using namespace std;

int main(){

ios_base::sync_with_stdio(0);

cin.tie(0);

int n,k;cin>>n>>k;

vector<pair<int,int>>v;

for(int i = 0; i<n; i++){

int x;cin>>x;

int y = x%k;

v.push_back({y,-x});

}

sort(v.rbegin(),v.rend());

for(int i = 0; i<v.size(); i++){

cout<<(-v[i].second)<<" ";

}

return 0;

}