HackerEarth Descending Weights problem solution

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.


using namespace std;

int main(){



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


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

int x;cin>>x;

int y = x%k;




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

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


return 0;


Post a Comment