Little Jhool and psychic powers

Little Jhool always wanted to have some psychic powers so that he could show off his skills, and magic to people and impress them. (Especially, his girlfriend Big Jhool!) But, in spite of all his efforts, hard work, dedication, Googling, watching youtube videos he couldn't garner any psychic abilities!

He knew everyone was making fun of him, so to stop all of that - he came up with a smart plan. Anyone who came to him to know about their future, he asked them to write a binary number for him - and then, he smartly told them if the future for that person had good in-store or bad.

The algorithm which Little Jhool follows is, as follows:

  • If the binary number written by the person has six consecutive 0s, or 1s, his future is bad.
  • Otherwise, he says that their future is good.

Example:

Input:  s = "0001111110"
Output: Sorry, sorry!

Approach

C++

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s = "0001111110";

    int i = 0;
    int n = s.size();
    int flag = 0;
    while (i < n - 1 && flag == 0)
    {
        int cnt = 1;
        while (i < n - 1 && s[i] == s[i + 1])
        {
            cnt++;
            if (cnt == 6)
            {

                flag = 1;
                break;
            }
            i++;
        }
        i++;
    }
    if (flag)
        cout << "Sorry, sorry!\n";
    else
        cout << "Good luck!\n";
    return 0;
}


No comments:

Post a Comment