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

HackerEarth Profits by cars problem solution

YASH PAL, 31 July 202415 February 2026
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 HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

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

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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