Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Descending Weights problem solution

YASH PAL, 31 July 202411 February 2026
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;

}
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes