File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ Title: Check AB
3+ Problem statement
4+ Suppose you have a string, S, made up of only 'a's and 'b's. Write a recursive function that checks
5+ if the string was generated using the following rules:
6+ a. The string begins with an 'a'
7+ b. Each 'a' is followed by nothing or an 'a' or "bb"
8+ c. Each "bb" is followed by nothing or an 'a'
9+ If all the rules are followed by the given string, return true otherwise return false.
10+
11+ Detailed explanation ( Input/output format, Notes, Images )
12+ Input format :
13+ String S
14+
15+ Output format :
16+ 'true' or 'false'
17+
18+ Constraints :
19+ 1 <= |S| <= 1000
20+ where |S| represents length of string S.
21+
22+ Sample Input 1 :
23+ abb
24+
25+ Sample Output 1 :
26+ true
27+
28+ Sample Input 2 :
29+ abababa
30+
31+ Sample Output 2 :
32+ false
33+
34+ Explanation for Sample Input 2
35+ In the above example, a is not followed by either "a" or "bb", instead it's followed by "b" which
36+ results in false to be returned.
37+ */
38+
39+ #include < iostream>
40+ using namespace std ;
41+
42+ bool CheckAB (char input[]) {
43+ if (input[0 ] == ' \0 ' ) {
44+ return true ;
45+ }
46+
47+ if (input[0 ] != ' a' ) {
48+ return false ;
49+ }
50+
51+ int count = 0 ;
52+ while (input[count] != ' \0 ' ) {
53+ if (count >= 3 ) {
54+ break ;
55+ }
56+ count++;
57+ }
58+
59+ if (count >= 3 && input[0 ] == ' a' && input[1 ] == ' b' && input[2 ] == ' b' ) {
60+ return CheckAB (&input[3 ]);
61+ } else {
62+ return CheckAB (&input[1 ]);
63+ }
64+ }
65+
66+ int main () {
67+ char input[100 ];
68+ bool ans;
69+ cin >> input;
70+ ans = CheckAB (input);
71+ if (ans) {
72+ cout << " true" << endl;
73+ } else {
74+ cout << " false" << endl;
75+ }
76+ }
Original file line number Diff line number Diff line change 1+ /*
2+ Title: Check Palindrome (Recursive)
3+
4+ Problem statement
5+ Determine if a given string ‘S’ is a palindrome using recursion. Return a Boolean value of true if
6+ it is a palindrome and false if it is not.
7+ Note: You are not required to print anything, just implement the function.
8+ Example:
9+ Input: s = "racecar"
10+ Output: true
11+ Explanation: "racecar" is a palindrome.
12+
13+ Detailed explanation ( Input/output format, Notes, Images )
14+ Input Format:
15+ The first and only line of the input contains string S.
16+
17+ Output format:
18+ Return a boolean value True or False.
19+
20+ Sample Input 1:
21+ abbba
22+
23+ Sample Output 1:
24+ true
25+
26+ Explanation Of Sample Input 1 :
27+ “abbba” is a palindrome
28+
29+ Sample Input 2:
30+ abcd
31+
32+ Sample Output 2:
33+ false
34+
35+ Explanation Of Sample Input 2 :
36+ “abcd” is not a palindrome.
37+
38+ Constraints:
39+ 0 <= |S| <= 10^6
40+ where |S| represents the length of string S.
41+ */
42+
43+ #include < iostream>
44+ #include < string>
45+ using namespace std ;
46+
47+ bool IsPalindrome (string t) {
48+ if (t.length () == 0 ) {
49+ return true ;
50+ }
51+
52+ if (t.at (0 ) != t.at (t.length () - 1 )) {
53+ return false ;
54+ }
55+
56+ return IsPalindrome (t.substr (1 , t.length () - 2 ));
57+ }
58+
59+ class Runner {
60+ string t;
61+
62+ public:
63+ void takeInput () {
64+ cin >> t;
65+ }
66+
67+ void execute () {
68+ auto ans = IsPalindrome (t);
69+ }
70+
71+ void executeAndPrintOutput () {
72+ auto ans = IsPalindrome (t);
73+ ans ? cout << " true" : cout << " false" ;
74+ cout << endl;
75+ }
76+ };
77+
78+ int main () {
79+ Runner runner;
80+ runner.takeInput ();
81+ runner.executeAndPrintOutput ();
82+ return 0 ;
83+ }
Original file line number Diff line number Diff line change 1+ /*
2+ Title: Count Zeros
3+
4+ Problem statement
5+ Given an integer N, count and return the number of zeros that are present in the given integer
6+ using recursion.
7+
8+ Detailed explanation ( Input/output format, Notes, Images )
9+ Input Format :
10+ Integer N
11+
12+ Output Format :
13+ Number of zeros in N
14+
15+ Constraints :
16+ 0 <= N <= 10^9
17+
18+ Sample Input 1 :
19+ 0
20+
21+ Sample Output 1 :
22+ 1
23+
24+ Sample Input 2 :
25+ 00010204
26+
27+ Sample Output 2 :
28+ 2
29+
30+ Explanation for Sample Output 2 :
31+ Even though "00010204" has 5 zeros, the output would still be 2 because when you convert it to an
32+ integer, it becomes 10204.
33+
34+ Sample Input 3 :
35+ 708000
36+
37+ Sample Output 3 :
38+ 4
39+ */
40+
41+ #include < iostream>
42+ using namespace std ;
43+
44+ int CountZeros (int n) {
45+ if (n == 0 ) {
46+ return 0 ;
47+ }
48+ int small_output = CountZeros (n/10 );
49+ int output = n % 10 == 0 ? 1 + small_output : small_output;
50+ return output;
51+ }
52+
53+ int main () {
54+ int n;
55+ cin >> n;
56+ cout << CountZeros (n) << endl;
57+ }
Original file line number Diff line number Diff line change 1+ /*
2+ Title: Geometric Sum
3+
4+ Problem statement
5+ Given k, find the geometric sum i.e.
6+ 1 + 1/2 + 1/4 + 1/8 + ... + 1/(2^k)
7+ Note: using recursion.
8+
9+ Detailed explanation ( Input/output format, Notes, Images )
10+ Input format :
11+ Integer k
12+
13+ Output format :
14+ Geometric sum (upto 5 decimal places)
15+
16+ Constraints :
17+ 0 <= k <= 1000
18+
19+ Sample Input 1 :
20+ 3
21+
22+ Sample Output 1 :
23+ 1.87500
24+
25+ Sample Input 2 :
26+ 4
27+
28+ Sample Output 2 :
29+ 1.93750
30+
31+ Explanation for Sample Input 1:
32+ 1+ 1/(2^1) + 1/(2^2) + 1/(2^3) = 1.87500
33+ */
34+
35+ #include < iostream>
36+ #include < math.h>
37+ #include < iomanip>
38+ using namespace std ;
39+
40+ float GeometricSum (int k) {
41+ if (k == 0 ) {
42+ return 1 ;
43+ }
44+
45+ return 1 / pow (2 , k) + GeometricSum (k - 1 );
46+ }
47+
48+ int main () {
49+ int k;
50+ cin >> k;
51+ cout << fixed << setprecision (5 );
52+ cout << GeometricSum (k) << endl;
53+ }
Original file line number Diff line number Diff line change 1+ /*
2+ Title: Multiplication(Recursive)
3+
4+ Problem statement
5+ Given two integers M & N, calculate and return their multiplication using recursion. You can only
6+ use subtraction and addition for your calculation. No other operators are allowed.
7+
8+ Detailed explanation ( Input/output format, Notes, Images )
9+ Input format :
10+ Line 1 : Integer M
11+ Line 2 : Integer N
12+
13+ Output format :
14+ M x N
15+
16+ Constraints :
17+ 0 <= M <= 1000
18+ 0 <= N <= 1000
19+
20+ Sample Input 1 :
21+ 3
22+ 5
23+
24+ Sample Output 1 :
25+ 15
26+
27+ Sample Input 2 :
28+ 4
29+ 0
30+
31+ Sample Output 2 :
32+ 0
33+ */
34+
35+ #include < iostream>
36+ using namespace std ;
37+
38+ int MultiplyNumbers (int m, int n) {
39+ if (n == 0 ) {
40+ return 0 ;
41+ }
42+
43+ return MultiplyNumbers (m, n-1 ) + m;
44+ }
45+
46+ int main () {
47+ int m, n;
48+ cin >> m >> n;
49+ cout << MultiplyNumbers (m, n) << endl;
50+ }
Original file line number Diff line number Diff line change 1+ /*
2+ Title: Pair Star
3+
4+ Problem statement
5+ Given a string S, compute recursively a new string where identical chars that are adjacent in the
6+ original string are separated from each other by a "*".
7+
8+ Detailed explanation ( Input/output format, Notes, Images )
9+ Input format :
10+ String S
11+
12+ Output format :
13+ Modified string
14+
15+ Constraints :
16+ 0 <= |S| <= 1000
17+ where |S| represents length of string S.
18+
19+ Sample Input 1 :
20+ hello
21+
22+ Sample Output 1:
23+ hel*lo
24+
25+ Sample Input 2 :
26+ aaaa
27+
28+ Sample Output 2 :
29+ a*a*a*a
30+ */
31+
32+ #include < iostream>
33+ using namespace std ;
34+
35+ void PairStar (char input[]) {
36+ if (input[0 ] == ' \0 ' ) {
37+ return ;
38+ }
39+
40+ PairStar (&input[1 ]);
41+
42+ if (input[0 ] == input[1 ]) {
43+ int count = 0 ;
44+ while (input[count] != ' \0 ' ) {
45+ count++;
46+ }
47+
48+ while (count > 0 ) {
49+ input[count+1 ] = input[count];
50+ count--;
51+ }
52+
53+ input[1 ] = ' *' ;
54+ }
55+
56+ return ;
57+ }
58+
59+ int main () {
60+ char input[100 ];
61+ cin.getline (input, 100 );
62+ PairStar (input);
63+ cout << input << endl;
64+ }
You can’t perform that action at this time.
0 commit comments