Problem: For number X, let it’s Coolness is defined as the number of “101“s occurring in it’s Binary representation.
For example the number 21 has a coolness of 2 since it’s binary representation is 10101 and the string”101” occurs twice in this representation.
A number is Very Cool if it’s coolness is greater than or equal to K. Please output the very cool integers between 1 and R.
Input format: The first line contains a single integer denotes the number of testcases T. The next T lines contains two space separated integers R and K.
(NOTE: The actual problem didn’t have the number of testcases as input. But for your better convenience we have worked with testcases. Omit the testcase input line to get the actual format.)
Output format: Print the single integer representing the number of Very Cool integers between 1 and R.
Constraints:
1<=R<=105
1<=K<=100
Sample Input:
2
102 1
21 2
Sample Output:
48
1
Solution: We strongly recommend you to try the problem first before moving to the solution.
C++ (STL)
/* C++ code for Very Cool number */
/* www.codewindow.in */
#include <bits/stdc++.h>
using namespace std;
// Function for converting decimal to binary and return as string
string dec_to_binary(int n) {
string s;
for(int i=15; i>=0; --i) { // Supposing sizeof integer as 16 bits
int temp=n>>i;
if(temp & 1)
s+='1';
else
s+='0';
}
return s;
}
// Solution Function
void solve() {
int n, k, r, ans=0, cool;
cin >> r >> k;
for(int num=1; num<=r; ++num) {
cool=0;
string s=dec_to_binary(num);
for(int i=0; i<s.length(); ++i) {
string temp=s.substr(i, 3); // Substring of size 3 starting from i
if(temp=="101")
++cool;
}
if(cool>=k) // If (coolness>=k) then count it
++ans;
}
cout << ans << "\n";
}
// Driver code
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
cin >> t; // Input the testcases
while(t--)
solve();
return 0;
}
Input:
2
102 1
21 2
Output:
48
1
Follow Us
You Missed
- Unlocking Innovation and Diversity: Accenture HackDiva Empowers Women in Tech with Cutting-Edge Solutions – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- Software Engineer Positions at Siemens Company: Apply Now – codewindow.in
- Cloud Engineer II Opportunities at Insight Company: Apply Now – codewindow.in
- Shape Your Career: Assistant Engineer Opportunities at Jindal Company – codewindow.in
- Shape Your Future: Executive Opportunities at Jindal Company – cdewindow.in
- Associate Engineer, Software Development at Ingram: Apply Now – codewindow.in
- Jade Company’s UI/UX Development Engineer Opportunities – Apply Now – codewindow.in
- Transform Your Career with S&P Global: Apply for the Software Development Engineer Role and Lead the Future of Financial Technology Innovation – codewindow.in
- Unlock Your Potential at Accenture as an Associate Software Engineer – Elevate Your Career with Innovation and Excellence – codewindow.in
- Accelerate Your Career: Join NVIDIA’s Elite Software Engineering Internship Program and Shape the Future of Technology – codewindow.in
- C Programming Interview Questions – codewindow.in
- Lead the Way in Analytics: Specialist Position at Razorpay – codewindow.in
- Innovate with Cyient: Junior Software Engineer Wanted – codewindow.in
- Innovate with Volvo: Associate Software Engineer Wanted – codewindow.in
- Lead the Tech Revolution: Full Stack Developer at Unisys – codewindow.in
- Software Engineer at ABB: Unlock Innovation and Shape the Future – codewindow.in
- IBM Associate Systems Engineer Job: Boost Your Career with a Leading Technology Giant – codewindow.in
- Make Your Mark in Android Development: Join Concentrix – codewindow.in
- Infosys is Growing: Field Services Developer Role Now Open – codewindow.in
- Start Your IT Career Journey with Amazon: IT Services Support Associate I Opportunity – codewindow.in
- Shape the Future of Web: Front-End Software Engineer Opportunity at Google Cloud – codewindow.in
- Barclays QA Team Expands: QA Analyst Role Now Open- codewindow.in
- Eurofins QA Team Grows: Test Engineer Role Now Open – codewindow.in
- Exciting Opportunity: Java Spring Boot Senior Developer Role at Infosys – codewindow.in
- Unlock Your Potential at Nokia: Software Engineer Opportunities Await – codewindow.in
- Join Microsoft’s World-Class Team as a Software Engineer and Shape the Future of Technology – codewindow.in
- Virtusa is Seeking Talented React JS Developers to Drive Digital Excellence – codewindow.in
- Join IBM Dynamic Team as a Full Stack Developer and Shape the Future – codewindow.in
- EY Welcomes Aspiring AI/ML Interns to Unlock the Future of -codewindow.in
- Exciting Opportunity: Project Engineer at Rockwell Automation- codewindow.in