Binary Digits | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Binary Digits

Please someone help me understand the logic behind this question. You are given a number A which contains only digits 0's and 1's. Your task is to make all digits same by just flipping one digit (i.e. 0 to 1 or 1 to 0 ) only. If it is possible to make all the digits same by just flipping one digit then print 'YES' else print 'NO'. Input Format: The first line contains a number made up of 0's and 1's. Output Format: Print 'YES' or 'NO' accordingly without quotes. Example: Input: 101 Output: YES Explanation: If you flip the middle digit from 0 to 1 then all the digits will become same. Hence output is YES.

20th Feb 2019, 2:15 PM
Nilutpol Kashyap
Nilutpol Kashyap - avatar
6 Answers
+ 3
You can just input number as a string and then count '0's and '1's Based on number of '0's and '1's you can find the answer Sometimes you don't need to use digits as digits
20th Feb 2019, 3:31 PM
Дмитрий Мазыло
Дмитрий Мазыло - avatar
+ 2
#python 2.7.15 s = str(input ()) ones, zeroes = 0, 0 for i in range(len(s)): ones += s[i]=='1' zeroes += s[i]=='0' if abs(ones-zeroes) != 1: print ("NO") else: print("YES")
21st Feb 2019, 1:56 AM
Дмитрий Мазыло
Дмитрий Мазыло - avatar
+ 1
Actually, all is explained in the task But anyway You are given a string of 0s and 1s The task is to find, if there's only one 0 in the string of 1s or only one 1 in the string of 0s Maybe the answer "YES" can be if the whole string is 1s or 0s But it is said "by flipping ONE digit" Note that "flipping" in this case can be seen as: a = not a
20th Feb 2019, 3:18 PM
Дмитрий Мазыло
Дмитрий Мазыло - avatar
0
Дмитрий Мазыло do I need to convert the input number to list? because we cannot edit elements of the string.
20th Feb 2019, 3:20 PM
Nilutpol Kashyap
Nilutpol Kashyap - avatar
0
Дмитрий Мазыло I have written the code as instructed by you. it runs well but for some cases it does not. for example, it works for inputs like 10 and 1011. but it shows wrong output for 0011. can u please help me.
20th Feb 2019, 3:49 PM
Nilutpol Kashyap
Nilutpol Kashyap - avatar
0
From where shall we learn binary digits
31st May 2020, 10:20 AM
Vaibhav Someshwar Kanade
Vaibhav Someshwar Kanade - avatar