Saving Vanshu and Ashu

Vanshu,Ashu and Kartik were on a Quest. Unbelievably they were encountered by another Kartik(Saxena), and the bonding

between both the kartik(s) happened. Kartik chose to go his own way with kartik to make something called kartik sandwich(hope you never have
to try that)leaving the other two.Shorty after kartik left Vanshu and Ashu met Dr. Strange who transported them to another dimension , but shortly after that he was killed by Thanos and Vanshu and Ashu got stuck in that dimension. Iron-Man feels bad for them and hence he has sent a machine to that dimension to bring them back to earth.

The machine works by transforming Vanshu and Ashu into their many mini versions then loading them into itself, transporting them to earth and add each of the mini versions to make them whole again.But as the machine was made in haste, there was a serious bug in the machine. The machine is producing extra mini versions of ashu and while loading
the mini versions it is leaving one version of Vanshu behind. Before the machine transports them to earth they have chance to make a change in the loading arrangement.
Now they will be back to earth safely and completely only if all the mini versions of Vanshu are loaded into the machine and while loading no two versions of vanshu are loaded together(you can leave mini versions of ashu behind).If they are no mini versions of ashu made you don't have to add the left version of vanshu.

Find whether they will make it to the earth safely or not.

Edit:

Just follow these two conditions for "Yes":

1. No two 'v' should be together.

2. You have to add just one more 'v' in the string by removing any  'a' such that rule 1 doesn't get violated.

3. If there s a single 'a' then the answer is "No", (Special case).

Example:

Input:  n = 5, s = "vaaav"
Output: Yes

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void saveVanshuAndAshu(int nstring s)
{
    int flag = 0;
    for (int i = 0i < n - 1i++)
    {
        if (s[i] == 'v' && s[i + 1] == 'v')
        {
            flag = 1;
            break;
        }
    }
    int cnt = 0;
    for (int i = 0i < ni++)
    {
        if (s[i] == 'a')
            cnt++;
    }
    if (cnt == 1)
        flag = 1;
    if (flag == 1)
        cout << "No\n";
    else if (n == 1 && s[0] == 'v')
        cout << "Yes\n";
    else
    {
        int f = 0;
        for (int i = 0i < ni++)
        {
            if (i == 0)
            {
                if (s[i] == 'a' && s[i + 1] == 'a')
                {
                    s[i] = 'v';
                    f = 1;
                    break;
                }
            }
            else if (i == n - 1)
            {
                if (s[i] == 'a' && s[i - 1] == 'a')
                {
                    s[i] = 'v';
                    f = 1;
                    break;
                }
            }
            else
            {
                if (s[i] == 'a' && s[i - 1] == 'a' && 
s[i + 1] == 'a')
                {
                    s[i] = 'v';
                    f = 1;
                    break;
                }
            }
        }
        if (f == 1)
            cout << "Yes\n";
        else
            cout << "No\n";
    }
}
int main()
{
    int n = 5;

    string s = "vaaav";

    saveVanshuAndAshu(ns);

    return 0;
}


No comments:

Post a Comment