Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • 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
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Chandu and his toy stack problem solution

YASH PAL, 31 July 2024
In this HackerEarth Chandu and his toy stack problem solution After setting up the area. Chandu wanted all his toys to be stacked there in that area. So that all of them are accessible easily. Currently, He is having N stacks of toys each with height H1, H2…Hn (assuming all toys are of same height).Chandu did not like the configuration much and want to change the height of each stack. To increase the height of a particular stack he can add some toys to it and to decrease the height he can take out some toys from that stack. Chandu, knows that he requires X units of effort for putting up an item onto the stack and Y units of effort for removing it. Help, chandu in setting up his toys.
HackerEarth Chandu and his toy stack problem solution

HackerEarth Chandu and his toy stack problem solution.

#include <bits/stdc++.h>

using namespace std;

long long start[100000 + 10];
long long finall[100000 + 10];

int main()
{

long long t , x , y, n ,ans;

cin >> t;

while(t--){

ans = 0;

cin >> n >> x >> y;

for(int i = 0; i < n; i++) cin >> start[i] >> finall[i];

sort(start, start + n);
sort(finall, finall + n);

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

if(finall[i] > start[i]) ans += (finall[i] - start[i]) * x;
else if (finall[i] < start[i]) ans += (start[i] - finall[i]) * y;
}

cout << ans << "n";
}


return 0;
}

Second solution

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{ int t,pen,n,x,y,i;
scanf("%d",&t);
while(t--)
{ pen=0;
scanf("%d%d%d",&n,&x,&y);
int a[n],b[n];
for(i=0;i<n;i++)

scanf("%d%d",&a[i],&b[i]);
sort(a,a+n);
sort(b,b+n);

for(i=0;i<n;i++)
{
if(a[i]==b[i])
continue;

else if(b[i]>a[i])
pen+=(b[i]-a[i])*x;

else
pen+=(a[i]-b[i])*y;
}

printf("%dn",pen);
}
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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