Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

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

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes