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 Very Cool Numbers problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Very Cool Numbers problem solution For a number X, let its “Coolness” be defined as the number of “101”s occurring in its binary representation. For example, the number 21 has Coolness 2, since its binary representation is 101012, and the string “101” occurs twice in this representation.
 
A number is defined as Very Cool if its Coolness is greater than or equal to K. Please, output the number of Very Cool integers between 1 and R.
 
 
HackerEarth Very Cool Numbers problem solution

 

 

HackerEarth Very Cool Numbers problem solution.

#include <bits/stdc++.h>
using namespace std;
int counter[100005];
int main()
{
for (int g=1; g<=100000; g++)
{
for (int y=0; y<20; y++)
{
if (((1<<y)&g) && (!((1<<(y+1))&g)) && ((1<<(y+2))&g))counter[g]++;
}
}
int T; scanf("%d", &T);
for (int g=0; g<T; g++)
{
int R,K;scanf("%d %d", &R, &K);
int ans=0;
for (int y=1; y<=R; y++)
{
ans+=(counter[y]>=K);
}
printf("%dn", ans);
}
return 0;
}
 
 

Second solution

#include <cstdio>
#include <iostream>
#include <cassert>

using namespace std;

#define MAXN 100000

int ar[MAXN+1];

int val(int i){
int state=0;
int ans=0;
while(i){
if(i&1){
if(state==1)
state = 1;
else if(state==2)
state = 3;
else if(state==0)
state = 1;
}else{
if(state==1)
state = 2;
else
state = 0;
}
if(state==3){
ans++;
state=1;
}
i = i/2;
}
return ans;
}

void init(){
for(int i=1;i<=MAXN;i++){
ar[i] = val(i);
}
}

int main(){
int T, R, K;
init();
cin >> T;
assert(T>0 and T<=100);
while(T--){
cin >> R >> K;
assert(R>0 and R<=100000);
assert(K>0 and K<=100);
int ans = 0;
for(int i=1;i<=R;i++)
if (ar[i]>=K)
ans++;
cout << ans << endl;
}
}
 
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