Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
ou are given a binary string SS of length NN. Your task is to check if there exists a substring of SS which is the binary representation of a prime number.
Formally, check if there exist integers LL and RR such that 1≤L≤R≤N1≤L≤R≤N, and the substring SLSL+1SL+2…SRSLSL+1SL+2…SR, when treated as a binary integer, is prime.
Print "Yes"
if such a substring exists, and "No"
if it doesn’t.
For each test case, output a single line containing one string — "Yes"
or "No"
, the answer to the problem.
You may print each character of the answer in uppercase or lowercase (for example, the strings “yEs”, “yes”, “Yes” and “YES” will all be treated as identical).
Subtask 1 (30 points):
Subtask 2 (70 points):
3
1
111
101101
No
Yes
Yes
Test case 11: There is only one substring, namely "1"
, and it isn’t prime.
Test case 22: The substrings of the given string are {{"1"
, "11"
, "111"
, "1"
, "11"
, "1"
}}. Of these, "111"
which represents 77 in binary, and "11"
which represents 33 in binary, are primes.
Test case 33: One of the substrings of the string is "1101"
, which is the binary representation of 1313 — a prime.
#include <iostream>
using namespace std;
int main() {
int T; cin>>T;
while(T--){
string S ; long N ; cin>>S;
N=S.length();
int flag=0;
if(N==1){
cout<<"NO\n";
}
else{
for(int i = 0 ; i <= N-2 ; i++){
if( S[i]=='1' && ( S[i+1]=='1' || S[i+1]=='0') ){
// if( S[i]=='1'){
cout<<"YES\n";
flag=1;
break;
}
}
if (flag==0) {
cout<<"NO\n";
}
}
}
return 0;
}
# cook your dish here
t=int(input())
for i in range(t):
s=input()
x=s.find("11")
y=s.find("10")
if(x!=-1 or y!=-1):
print("Yes")
else:
print("No")# cook your dish here
In our experience, we suggest you solve this Prime in a binary string CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
Prime in a binary string Problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Prime in a binary string CodeChef Solution.
I hope this Prime in a binary string CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More CodeChef Solutions >>
Olympics Ranking CodeChef Solution
Problem Difficulties CodeChef Solution
Chef and Bulb Invention CodeChef Solution