Header Ads Widget

Very cool numbers | practice problem solution hakerearth

Program:-

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.

Input:
The first line contains an integer T, the number of test cases.
The next T lines contains two space-separated integers, R and K.
Output:
Output T lines, the answer for each test case.
Constraints:
1<=T<=100
1<=R<=105
1<=K<=100

SAMPLE INPUT
 
1
5 1
SAMPLE OUTPUT
 
1
Time Limit:5.0 sec(s) for each input file.
Memory Limit:256 MB
Source Limit:
Solution:-

#include<stdio.h>
int very_cool(int n,int k) // funtion definition
{
long long int a[1000000],i=0,j;
int sum=0;
while(n>0)
{
a[i]=n%2;
n=n/2;
i++;
}
for( j=i-1;j>=2;j--)
{
if(a[j]==1&&a[j-1]==0&&a[j-2]==1)
sum++;
}
if(sum>=k)
return 1;
else
return 0;
}
void main()
{
int t,r,k,s,total_no=0;
scanf("%d",&t);
for(int i=0;i<t;i++)
{
scanf("%d%d",&r,&k);
for(int j=5;j<=r;j++)
{
s=very_cool(j,k); // function calling
total_no=total_no+s;
}
printf("%d\n",total_no);
total_no=0;
}
}

Recommended post:-

Hackerearth Problems:-

Hackerrank Problems:-
Data structure:-

Key points:-



,cool phone numbers to call,
,cool numbers copy and paste,
,cool random number generator,
,dollar bill serial number lookup,
,cool looking numbers,
,interesting numbers,
,fancy serial lookup,
,cool number font generator,