Tuesday 31 July 2018

Number Challenge
Mike set off with great zeal to the "Kracker Jack Fun Fair 2017". There were numerous activities in the fair, though Mike being a math expert, liked to participate in the Number Challenge.

Mike was given a string D of numbers containing only digits 0's and 1's. His challenge was to make the number to have all the digits same. For that, he should change exactly one digit, i.e. from 0 to 1 or from 1 to 0. If it is possible to make all digits equal (either all 0's or all 1's) by flipping exactly 1 digit then he has to output "Yes", else print "No" (without quotes).

Write a program to help Mike win over his challenge.


Input Format:
First and only input contains a string D of numbers made of only digits 1's and 0's.

Output Format:
Output “Yes" or a "No", depending on whether its possible to make it all 0s or 1s or not.
Refer sample input and output for formatting specifications.


Sample Input1:
101
Sample Output1:
Yes

Sample Input2:
11
Sample Output2:
No


Solution:
#include<stdio.h>
#include<string.h>
int main()
{
    int sum=0,i,l;
    char d[50];
    scanf("%s",d);
    l=strlen(d);
    for(i=0;i<l;i++)
    if(d[i]=='1')
    sum=sum+1;
    if(sum==1||(sum==(l-1)))
    printf("Yes");
    else
    printf("No");
    return 0;
} 

2 comments:

Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...