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 Profits by cars problem solution

YASH PAL, 31 July 2024
In this HackerEarth Profits by cars problem solution, You are a car seller. You have N cars and the profit for each of the cars is given by an array P. The profit of cars are P1, P2, P3, …, PN. Since you got a huge profit in the last month so you decide to get (N – 1) more sets of such cars. You already have one car. Now, you have N^2 cars. Basically, there are N number of cars of each profit such as N cars for profit P1, N cars of profit P2, and so on up to N cars of profit PN.
You can perform the following operations any number of times:
If the last car is sold for profit P, then you can sell a car for profit Pc > P.
Note: You can select a car of any profit in the first operation as there are no cars that are sold earlier.
Find out the maximum profit that you can make.
For example, N = 4 and prices are P1, P2, P3, P4. Since N is 4, therefore you can have four sets of cars and the prices are P1, P2, P3, P4, P1, P2, P3, P4, P1, P2, P3, P4, P1, P2, P3, P4.
HackerEarth Profits by cars problem solution

HackerEarth Profits by cars problem solution.

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007
#define test ll t; cin>>t; while(t--)
typedef long long int ll;
int main() {
FIO;
//test
{
ll n;
cin>>n;
ll x; set<ll>s;
for(int i=0;i<n;i++){
cin>>x;
s.insert(x);
}
ll ans=0;
for(auto it:s){
ans+=it;
}
cout<<ans<<endl;
}
return 0;
}

Second solution

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
n = unique(a, a + n) - a;
cout << accumulate(a, a + n, 0ll) << 'n';
}
}
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