Q. 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 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.
Solution:
Outputs:
Enter a binary number: 1101
YES
Enter a binary number: 1001
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.
Solution:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A = input("Enter a binary number: ") | |
count0 = 0 | |
count1 = 0 | |
for x in A: | |
if x == '0': | |
count0 += 1 | |
elif x == '1': | |
count1 += 1 | |
if count0==1 or count1==1: | |
print('YES') | |
else: | |
print('NO') |
Outputs:
Enter a binary number: 1101
YES
Enter a binary number: 1001
NO
.
No comments:
Post a Comment