Computer Science, asked by saimithraakuthota028, 2 months ago

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 19101, 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: Two space-separated integers, R and K.

Output:

Print Single integer representing the number of Very Cool integers between 1 and R.

Constraints:

1<=R<=105

1<=K<=100

Sample Input

21 2

Import java.util.

10

12 13

14

25

Bufferm

Sample Output​

Answers

Answered by kumark54321
0

Answer:

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.

Explanation:

Given :

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

Program :

#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;

   }

}

Similar questions