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 Chocolates and Boxes problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Chocolates and Boxes, problem-solution Alice has N boxes, and each box has a certain non-zero number of chocolates. These boxes are numbered from 1 to N.
 
Alice is planning to go to wonderland. She wants to carry exactly K number of chocolates and she can carry only 2 boxes. So she wants to know the number of ways in which she can select 2 boxes such that the total number of chocolates in them is K.
 
 
HackerEarth Chocolates and Boxes problem solution

 

 

HackerEarth Chocolates and Boxes problem solution.

int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int arr[300000];
rep(i,n)
cin>>arr[i];
int k;
cin>>k;
int mul[300000];
for(int i=0;i<k;i++)
mul[i]=0;
long long int ways=0;
for(int i=0;i<n;i++)
{
if(arr[i]<k)
mul[arr[i]%k]++;
}
for(int i=1;i<=(k/2);i++)
{
if(2*i == k && mul[i]>=2)
ways+=(mul[i]*(mul[i]-1)/2);
else
ways+=mul[i]*mul[k-i];
}
cout<<ways<<endl;
}
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