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.
Example:
Input: r = 5, k = 1
Output: 1
Approach
C++
#include <bits/stdc++.h>using namespace std;string fun(long long n){string res = "";while (n){if (n & 1)res += '1';elseres += '0';n = n / 2;}return res;}long long coolness(string s){string pat = "101";long long n = s.size();if (n < 3)return 0;long long cnt = 0;long long m = 3;for (long long i = 0; i <= n - m; i++){long long flag = 0;for (long long j = 0; j < m; j++){if (s[i + j] != pat[j]){flag = 1;break;}}if (flag == 0)cnt++;}return cnt;}int main(){long long cool[100001] = {0};for (long long i = 1; i <= 100000; i++){string x = fun(i);cool[i] = coolness(x);}long long r = 5, k = 1;long long ans = 0;for (long long i = 1; i <= r; i++){if (cool[i] >= k)ans++;}cout << ans << "\n";return 0;}
Read Interview Questions
Exception Handling Interview Questions
DBMS Interview Questions Set -1
DBMS Interview Questions Set -2
JPA Interview Questions Set -1
Spring Boot Interview Questions Set 1
Spring Boot Interview Questions Set 2
No comments:
Post a Comment