Intelligent Girl

Soumika has a string S and its starting index is 1. The string S consists of characters from 

19. As she is very intelligent, she wants to test his brother Vinay Tendulkar. She asked her brother Vinay Tendulkar to count the number of even-numbered characters ( i.e 2,4,6,8 ) for every index i (1i|S|). For index i, the result should be calculated from i to the end of the string. As Vinay doesn't know about programming, he wants you to help him find the solution.

Example:

Input: s = "574674546476"
Output: 7 7 7 6 5 5 4 4 3 2 1 1 

Approach

C++

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

void intelligent(string s)
{

    int n = s.size();
    int dp[n];
    if (s[n - 1] == '2' || s[n - 1] == '4' ||
        s[n - 1] == '6' || s[n - 1] == '8')
        dp[n - 1] = 1;
    else
        dp[n - 1] = 0;
    for (int i = n - 2i >= 0i--)
    {
        if (s[i] == '2' || s[i] == '4' ||
            s[i] == '6' || s[i] == '8')
            dp[i] = 1 + dp[i + 1];
        else
            dp[i] = dp[i + 1];
    }
    for (int i = 0i < ni++)
        cout << dp[i<< " ";
}
int main()
{
    string s = "574674546476";

    intelligent(s);

    return 0;
}


No comments:

Post a Comment