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 Naruto’s Punishment problem solution

YASH PAL, 31 July 202415 February 2026
In this HackerEarth Naruto’s Punishment problem solution Naruto went to watch a movie with his friends which turned out to be very boring. He decided to punish his friends with a problem for forcing him to watch such a boring movie.
 
There exists an array A consisting of N numbers. Output the number of subsets of the array A whose sum is greater than or equal to K.
 
Naruto’s friends being quite weak in coding were not able to solve this problem. Can you solve it for them and save them from Naruto’s Rasengan?
 
 
HackerEarth Naruto's Punishment problem solution

 

 

HackerEarth Naruto’s Punishment problem solution.

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <math.h>

#define ll long long int
#define maxN 100000
#define maxVal 100000000
#define minVal -100000000
#define mod 1000000007LL

#define gcd(a,b) __gcd(a,b)

using namespace std;

ll n,k,f;
ll a[45];
vector<ll> g[2];

void compute(ll t,ll i,ll s)
{
if(i==f)
{
g[t].push_back(s);
return;
}
compute(t,i+1,s+a[i]);
compute(t,i+1,s);
}

ll searchb()
{
ll s,e,m;
s=0;
e=(ll)g[1].size();
while((e-s)>1)
{
m=s+(e-s)/2;
if(g[1][m]>=f)
e=m;
else
s=m+1;
}
if(g[1][s]>=f)
return s;
if(g[1][e]>=f)
return e;
return (ll)g[1].size();
}

int main()
{

ll i,j,ans=0;
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
f=n/2;
compute(0,0,0);
sort(g[0].begin(),g[0].end());
f=n;
compute(1,n/2,0);
sort(g[1].begin(),g[1].end());
cin>>k;
for(i=0;i<(ll)g[0].size();i++)
{
if(g[0][i]>=k)
ans+=(ll)g[1].size();
else
{
f=k-g[0][i];
j=searchb();
ans+=((ll)g[1].size()-j);
}
}
cout<<ans;
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