DEV Community

JAIKUMAR DEWANGAN
JAIKUMAR DEWANGAN

Posted on

ERP Recursion Code

1. Given an array A[ ], we need to find the sum of its elements using Tail Recursion Method. We generally want to achieve tail recursion (a recursive function where recursive call is the last thing that function does) so that compilers can optimize the code. Basically, if recursive call is the last statement, the compiler does not need to save the state of parent call.
Example:
Input1 : A[] = {1, 8, 9}
Output1 : 18

Input2 : A[] = {2, 55, 1, 7}
Output2 : 65

code


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

 int recursive_sum( int arr[] , int i , int n ,  int sum)
 {
   if(i==n)
   {
    return sum; 
   }
   sum = sum + arr[i];
   recursive_sum(arr , i+1 , n , sum); 
 }

int main()
{

   int arr[] = {1,2,3,4,5}; 
 int sum =0 ;
 int  i=0;
 int n = sizeof(arr) /sizeof(arr[0]); 

 int ans = recursive_sum(arr , i ,n,  sum); 

 cout<<"the sum is "<<ans; 

    return 0; 
}
Enter fullscreen mode Exit fullscreen mode

=====================================================================

2 . Many problems of recursion revolve around strings and its substrings. We have got one such problem for you. You are given a string S, you have to find the count of all contiguous substrings starting and ending with the same character.
Example
Input1: prepby
Output1: 7
Explanation: In the string, all the substrings that start and end with the same character are p, r, e, p, b, y, prep.
Input2: abcde
Output: 5
Explanation: In the string, all the substrings that start and end with the same character are a, b, c, d, e.

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

int recursive_sum_string(string s, int i, int count) {

    if (s[i] == '\0') {
        return count; 
    }


    if (s[i] != ' ') {
        count++;
    }


    return recursive_sum_string(s, i + 1, count);
}

int main() {
    string s = "jai kumar"; 
    int i = 0; 
    int count = 0; 

    int ans = recursive_sum_string(s, i, count);

    cout << "Here is the number of characters (excluding spaces): " << ans << endl;

    return 0; 
}

Enter fullscreen mode Exit fullscreen mode

=====================================================================

  1. Count Set-bits of number using Recursion Given a number N. The task is to find the number of set bits in its binary representation using recursion. Examples: Input1 : 21 Output1 : 3 Explanation: 21 represented as 10101 in binary representation

Input2 : 16
Output2 : 1
Explanation:16 represented as 10000 in binary representation

#include<iostream> 

using namespace std ; 

int  setBit ( int n , int count )
{

  if(n==0)
  return count;

 if(n%2==1)
  count++; 

 setBit(n/2 , count) ; 

}

int main(){
   int n = 0  ; 
   int cnt=0;
int ans =  setBit ( n , cnt ); 

cout<<" no. of set bits "<< ans ;

    return 0 ; 
}
Enter fullscreen mode Exit fullscreen mode

==========================================================

  1. Write a code to count digit K in a number N using Recursion Test Case: Input: 122622 2 Output: 4
#include<iostream>
using namespace std;

int count_digit_k(int n, int target, int cnt) {
    if (n == 0) {
        return cnt;
    }
    if (n % 10 == target) {
        cnt++;  
    }


    return count_digit_k(n / 10, target, cnt);
}

int main() {
    int n = 221212112;  
    int t = 1;           
    int cnt = 0;       

    cnt = count_digit_k(n, t, cnt);


    cout << "Count of target " << t << " is: " << cnt << endl;

    return 0; 
}

Enter fullscreen mode Exit fullscreen mode

Write a code to find the GCD of two numbers using recursion.
Test Data :
Input: 10 50
Output: 10

The problem is to generate the power set of a given string in lexicographical order.

Power set P(S) of a set S is the set of all subsets of S. 
For example S={a,b,c} then P(s)={{},{a},{b},{c},{a,b},{a,c},{b,c},{a,b,c}}. Print without empty set.

Example
Input:cab
Output: 
a
ab
abc
ac
b
bc
c
Enter fullscreen mode Exit fullscreen mode

You have a list arr of all integers in the range [1, n] sorted in a strictly increasing order. Apply the following algorithm on arr:
Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.
Repeat the previous step again, but this time from right to left, remove the rightmost number and every other number from the remaining numbers.
Keep repeating the steps again, alternating left to right and right to left, until a single number remains.
Given the integer n, return the last number that remains in arr.

Example:
Input: n = 9
Output: 6
Explanation:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr = [2, 4, 6, 8]
arr = [2, 6]
arr = [6]

Write a recursive code for findingthe sum of all the numbers until N reaches 1 after performing the following two operations:

  1. If N is even, then N = N/2
  2. If N is odd, then N = 3*N+1 Input: N = 5 Output: 36 Explanation: The sequence will be [5, 16, 8, 4, 2, 1] 5 is odd so N becomes 5*3 + 1 = 16 sum becomes 5 + 16 = 21 16 is even so N becomes 16/2 = 8 sum becomes 21 + 8 = 29 8 is even so N becomes 8/2 = 4 sum becomes 29 + 4 = 33 4 is even so N becomes 4/2 = 2 sum becomes 33 + 2 = 35 2 is even so N becomes 2/2 = 1 sum becomes 35 + 1 = 36 N becomes 1 , so return sum = 36

Write a code to get the largest element of an array using recursion.
Test Case:
Input: 5
5 10 15 20 25
Output: 25

Shubh solved lots of problems of Binary Number and her friends know she loves binary numbers. Her friend asked a question about this topic. She gave two number N and I and she told her on the 1st row write a 0, and in the 2nd row use just previous row and replace each 0 with 01 and each 1 with 10 in the current row.Now She asked to print Ith index value in the Nth row.
Note: Each row index start from 1.

Input Format
The first line contains two integers N and I.

Output Format
Print Ith index value in the Nth row.
Example:
Input : N = 5, I = 13
Output : 0

Explanation :
1st row 0
2nd row 01
3rd row 0110
4th row 01101001
5th row 0110100110010110
Therefore, 0 is the 13th bit in the 5th row

Top comments (0)