Soumika has a string S and its starting index is 1. The string S consists of characters from
. 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 ) for every index i . 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;elsedp[n - 1] = 0;for (int i = n - 2; i >= 0; i--){if (s[i] == '2' || s[i] == '4' ||s[i] == '6' || s[i] == '8')dp[i] = 1 + dp[i + 1];elsedp[i] = dp[i + 1];}for (int i = 0; i < n; i++)cout << dp[i] << " ";}int main(){string s = "574674546476";intelligent(s);return 0;}
No comments:
Post a Comment