0% found this document useful (0 votes)
2 views3 pages

Recursion Easy Interview Problems Java

The document provides Java implementations of various recursive algorithms, including printing numbers, calculating factorials, Fibonacci series, and more. It also covers operations like summing and multiplying digits, checking for palindromes, reversing arrays, binary search, and finding GCD. Each function is accompanied by its time complexity where applicable.

Uploaded by

shaktisaali30
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Recursion Easy Interview Problems Java

The document provides Java implementations of various recursive algorithms, including printing numbers, calculating factorials, Fibonacci series, and more. It also covers operations like summing and multiplying digits, checking for palindromes, reversing arrays, binary search, and finding GCD. Each function is accompanied by its time complexity where applicable.

Uploaded by

shaktisaali30
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Recursion - Easy Interview Problems (Java)

Print 1 to N
static void print(int n){
if(n==0) return;
print(n-1);
[Link](n);
}

Print N to 1
static void print(int n){
if(n==0) return;
[Link](n);
print(n-1);
}

Factorial
static int fact(int n){
if(n==0) return 1;
return n*fact(n-1);
}

Fibonacci
static int fib(int n){
if(n<=1) return n;
return fib(n-1)+fib(n-2);
}
// Time: O(2^n), Space: O(n)

Sum of Digits
static int sum(int n){
if(n==0) return 0;
return n%10 + sum(n/10);
}

Product of Digits
static int product(int n){
if(n<10) return n;
return (n%10)*product(n/10);
}

Count Digits
static int count(int n){
if(n==0) return 0;
return 1 + count(n/10);
}

Reverse Number
static int reverse(int n,int rev){
if(n==0) return rev;
return reverse(n/10, rev*10 + n%10);
}

Palindrome
static boolean palindrome(String s,int i){
if(i>=[Link]()/2) return true;
if([Link](i)!=[Link]([Link]()-i-1)) return false;
return palindrome(s,i+1);
}

Reverse Array
static void reverse(int[] arr,int l,int r){
if(l>=r) return;
int temp=arr[l];
arr[l]=arr[r];
arr[r]=temp;
reverse(arr,l+1,r-1);
}

Binary Search
static int bs(int[] arr,int l,int r,int target){
if(l>r) return -1;
int mid=l+(r-l)/2;
if(arr[mid]==target) return mid;
if(target<arr[mid]) return bs(arr,l,mid-1,target);
return bs(arr,mid+1,r,target);
}

Power
static long power(long x,long n){
if(n==0) return 1;
long half=power(x,n/2);
if(n%2==0) return half*half;
return x*half*half;
}
// Time: O(log n)

Check Sorted Array


static boolean isSorted(int[] arr,int i){
if(i==[Link]-1) return true;
if(arr[i]>arr[i+1]) return false;
return isSorted(arr,i+1);
}

GCD using Recursion


static int gcd(int a,int b){
if(b==0) return a;
return gcd(b,a%b);
}
// Time: O(log(min(a,b)))

You might also like