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
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Group Photo problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Group Photo problem solution, You and your friends want to take group photos. The process of taking photos can be described as follows:
 
On the photo, each photographed friend occupies a rectangle of pixels: the ith of them occupies the rectangle of width wi pixels and height hi pixels. On the group photo everybody stands in a line, thus the minimum pixel size of the photo including all the photographed friends is W * H, where W is the total sum of all widths and H is the maximum height among the heights of all the photographed friends.
 
The friends made n photos – the jth (1 <= j <= n) photo had everybody except for the jth friend as he was the photographer.
Print the minimum size of each made photo in pixels.
 
 
HackerEarth Group Photo problem solution

 

 

HackerEarth Group Photo problem solution.

#include<bits/stdc++.h>
using namespace std ;
#define pb push_back
#define mp make_pair
#define infile() freopen("sample.txt","r",stdin);
#define output() freopen("sampleout.txt","w",stdout);
#define ll long long
#define sc(t); scanf("%d",&t);
#define scl(t); scanf("%lld",&t);
#define sc2(n,m); scanf("%d%d",&n,&m);
#define scl2(n,m); scanf("%lld%lld",&n,&m);
#define debug(); printf("tusharn");
#define N 200005
#define mod 1000000007
#define printi(n) printf("%d",n);
vector < ll > v ;
int n ;
int a[N] ;
int w[N],h[N];
int main()
{
int i , j , t ;
sc(n) ;
multiset < int > s ;
multiset < int > :: iterator it ;
ll sum = 0LL;
for(i=1;i<=n;i++)
{
scanf("%d %d",&w[i],&h[i]) ;
sum = (ll)(sum + w[i]) ;
s.insert(h[i]) ;
}
for(i = 1 ; i <= n ; i++ )
{
ll tmp = (ll)(sum - w[i]) ;
it = s.find(h[i]) ;
s.erase(it) ;
it = s.end() ;
it-- ;
ll ans = (ll)(tmp * (*it)) ;
printf("%lld ",ans);
s.insert(h[i]);
}
return 0 ;
}
 

Second solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int w[200005],h[200005],cpy[200005];
int sum=0;
assert(n>=1 && n<=2e5);
for(int i=0;i<n;i++)
{
cin>>w[i]>>h[i];
sum+=w[i];
cpy[i]=h[i];
assert(w[i]>=1 && w[i]<=10);assert(h[i]>=1 && h[i]<=1e3);
}
if(n==1){cout<<"0n";return 0;}
sort(cpy,cpy+n);
for(int i=0;i<n;i++)
{
int wgt=sum-w[i];
int hgt=(cpy[n-1]==h[i])?cpy[n-2]:cpy[n-1];
cout<<wgt*hgt<<" ";
}
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 *

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