Skip to content

Commit 3c308db

Browse files
committed
Recursion 1 or 2
1 parent b1435e4 commit 3c308db

14 files changed

+580
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Calculate Power
2+
Send Feedback
3+
Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to return the answer.
4+
Do this recursively.
5+
Input format :
6+
Two integers x and n (separated by space)
7+
Output Format :
8+
x^n (i.e. x raise to the power n)
9+
Constraints :
10+
0 <= x <= 30
11+
0 <= n <= 30
12+
Sample Input 1 :
13+
3 4
14+
Sample Output 1 :
15+
81
16+
Sample Input 2 :
17+
2 5
18+
Sample Output 2 :
19+
32
20+
21+
22+
/////////////---------------------<<<<<<<<<<<<<<<<<<<<.>>>>>>>>>>>>>>>>>>>>>////////
23+
public class Solution {
24+
25+
public static int power(int x, int n) {
26+
if (n == 0)
27+
return 1;
28+
int result = x * power(x, n - 1);
29+
return result;
30+
}
31+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Check Number in Array
2+
Send Feedback
3+
Given an array of length N and an integer x, you need to find if x is present in the array or not. Return true or false.
4+
Do this recursively.
5+
Input Format :
6+
Line 1 : An Integer N i.e. size of array
7+
Line 2 : N integers which are elements of the array, separated by spaces
8+
Line 3 : Integer x
9+
Output Format :
10+
'true' or 'false'
11+
Constraints :
12+
1 <= N <= 10^3
13+
Sample Input 1 :
14+
3
15+
9 8 10
16+
8
17+
Sample Output 1 :
18+
true
19+
Sample Input 2 :
20+
3
21+
9 8 10
22+
2
23+
Sample Output 2 :
24+
false
25+
26+
27+
/////////////////////----------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>
28+
public class Solution {
29+
30+
public static boolean checkNumber(int input[], int x) {
31+
if(input.length == 0)
32+
return false;
33+
34+
if(input.length == 1)
35+
{
36+
if(input[0] == x)
37+
return true;
38+
else
39+
return false;
40+
}
41+
42+
if(input[0] == x)
43+
return true;
44+
45+
int []smallArray = new int[input.length - 1];
46+
for(int i = 1; i < input.length; i++)
47+
smallArray[i - 1] = input[i];
48+
49+
boolean checkValue = checkNumber(smallArray, x);
50+
return checkValue;
51+
52+
}
53+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
First Index Of a Number in an Array - Question
2+
Send Feedback
3+
Given an array of length N and an integer x, you need to find and return the first index of integer x present in the array. Return -1 if it is not present in the array.
4+
First index means, the index of first occurrence of x in the input array.
5+
Do this recursively. Indexing in the array starts from 0.
6+
Input Format :
7+
Line 1 : An Integer N i.e. size of array
8+
Line 2 : N integers which are elements of the array, separated by spaces
9+
Line 3 : Integer x
10+
Output Format :
11+
first index or -1
12+
Constraints :
13+
1 <= N <= 10^3
14+
Sample Input :
15+
4
16+
9 8 10 8
17+
8
18+
Sample Output :
19+
1
20+
21+
///////////////////////______________----------------------->>>>>>>>>>>>>>>>>>>>
22+
23+
24+
public class Solution {
25+
26+
public static int firstIndex(int input[], int x) {
27+
return firstIndex(input, x, 0);
28+
}
29+
30+
public static int firstIndex(int arr[], int x , int startIndex) {
31+
// Base case 1
32+
if (startIndex == arr.length) {
33+
return -1;
34+
}
35+
36+
// Base case 2
37+
if (arr[startIndex] == x) {
38+
return startIndex;
39+
}
40+
41+
// Recursive call
42+
int smallAns = firstIndex(arr, x, startIndex + 1);
43+
44+
return smallAns;
45+
}
46+
47+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Last Index Of a Number in an Array - Question
2+
Send Feedback
3+
Given an array of length N and an integer x, you need to find and return the last index of integer x present in the array. Return -1 if it is not present in the array.
4+
Last index means - if x is present multiple times in the array, return the index at which x comes last in the array.
5+
You should start traversing your array from 0, not from (N - 1).
6+
Do this recursively. Indexing in the array starts from 0.
7+
Input Format :
8+
Line 1 : An Integer N i.e. size of array
9+
Line 2 : N integers which are elements of the array, separated by spaces
10+
Line 3 : Integer x
11+
Output Format :
12+
last index or -1
13+
Constraints :
14+
1 <= N <= 10^3
15+
Sample Input :
16+
4
17+
9 8 10 8
18+
8
19+
Sample Output :
20+
3
21+
22+
/////////////////------------------------>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
23+
24+
25+
public class Solution {
26+
27+
public static int lastIndex(int input[], int x) {
28+
return lastIndex(input, x, input.length - 1);
29+
}
30+
31+
public static int lastIndex(int arr[], int x, int lastIdx) {
32+
if(lastIdx <= 0)
33+
return -1;
34+
35+
if(arr[lastIdx] == x)
36+
return lastIdx;
37+
38+
int smallAns = lastIndex(arr, x, lastIdx - 1);
39+
40+
return smallAns;
41+
}
42+
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Number of Digits
2+
Send Feedback
3+
Given the number 'n', find out and return the number of digits present in a number .
4+
Input Format :
5+
Integer n
6+
Output Format :
7+
Count of digits
8+
Constraints :
9+
1 <= n <= 10^6
10+
Sample Input 1 :
11+
156
12+
Sample Output 1 :
13+
3
14+
Sample Input 2 :
15+
7
16+
Sample Output 2 :
17+
1
18+
19+
//////////////////---------------<<<<<<<<<<<<<<<>>>>>>>>>>>/////////////
20+
public class Solution {
21+
static int c = 0;
22+
public static int count(int n){
23+
//Write your code here
24+
if(n > 0) {
25+
c++;
26+
count(n/10);
27+
}
28+
return c;
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Print First N Natural Numbers - Code
2+
Send Feedback
3+
Given the number 'n', write a code to print numbers from 1 to n in increasing order recursively.
4+
Input Format :
5+
Integer n
6+
Output Format :
7+
Numbers from 1 to n (separated by space)
8+
Constraints :
9+
1 <= n <= 10000
10+
Sample Input 1 :
11+
6
12+
Sample Output 1 :
13+
1 2 3 4 5 6
14+
Sample Input 2 :
15+
4
16+
Sample Output 2 :
17+
1 2 3 4
18+
19+
20+
/////////////////-----------------<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>------------------
21+
22+
public class Solution {
23+
24+
public static void print(int n){
25+
//Write your code here
26+
if (n > 0) {
27+
print(n-1);
28+
System.out.print(n + " ");
29+
}
30+
}
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Sum Of Array
2+
Send Feedback
3+
Given an array of length N, you need to find and return the sum of all elements of the array.
4+
Do this recursively.
5+
Input Format :
6+
Line 1 : An Integer N i.e. size of array
7+
Line 2 : N integers which are elements of the array, separated by spaces
8+
Output Format :
9+
Sum
10+
Constraints :
11+
1 <= N <= 10^3
12+
Sample Input 1 :
13+
3
14+
9 8 9
15+
Sample Output 1 :
16+
26
17+
Sample Input 2 :
18+
3
19+
4 2 1
20+
Sample Output 2 :
21+
7
22+
23+
24+
//////////////////---------------------------->>>>>>>>>>>>>>>>>>>>>>>>>
25+
26+
public class Solution {
27+
28+
public static int sum(int input[]) {
29+
if(input.length == 0)
30+
return -1;
31+
32+
if(input.length == 1)
33+
return input[0];
34+
35+
int []smallArray = new int[input.length - 1];
36+
for(int i = 1; i < input.length; i++)
37+
smallArray[i-1] = input[i];
38+
39+
int total = sum(smallArray);
40+
total+=input[0];
41+
return total;
42+
}
43+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public static int binarySearch(int a[],int si,int ei,int x){
2+
if(si>ei){
3+
return -1;
4+
}
5+
int midIndex=(si+ei)/2;
6+
if(a[midIndex]==x){
7+
return midIndex;
8+
}else if(a[midIndex]<x){
9+
return binarySearch(a,midIndex+1,ei,x);
10+
}else{
11+
return binarySearch(a,si,midIndex-1,x);
12+
}
13+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Merge Sort - Problem Statement
2+
Send Feedback
3+
Sort an array A using Merge Sort.
4+
Change in the input array itself. So no need to return or print anything.
5+
Input format :
6+
Line 1 : Integer n i.e. Array size
7+
Line 2 : Array elements (separated by space)
8+
Output format :
9+
Array elements in increasing order (separated by space)
10+
Constraints :
11+
1 <= n <= 10^3
12+
Sample Input 1 :
13+
6
14+
2 6 8 5 4 3
15+
Sample Output 1 :
16+
2 3 4 5 6 8
17+
Sample Input 2 :
18+
5
19+
2 1 5 2 3
20+
Sample Output 2 :
21+
1 2 2 3 5
22+
23+
24+
////////////////////////////---------------------------------->>>>>>>>>>>>>>>>>>>>
25+
26+
public class solution {
27+
28+
public static void merge(int s1[], int s2[], int d[])
29+
{
30+
int i,j,k;
31+
i = j = k = 0;
32+
while(i < s1.length && j < s2.length)
33+
{
34+
if(s1[i] <= s2[j])
35+
d[k++] = s1[i++];
36+
else
37+
d[k++] = s2[j++];
38+
}
39+
40+
if(i < s1.length) {
41+
while(i < s1.length)
42+
d[k++] = s1[i++];
43+
}
44+
45+
if(j < s2.length) {
46+
while(j < s2.length)
47+
d[k++] = s2[j++];
48+
}
49+
}
50+
51+
public static void mergeSort(int[] input){
52+
// Write your code here
53+
if(input.length <= 1)
54+
return;
55+
56+
int mid = input.length / 2;
57+
int b[] = new int[mid];
58+
int c[] = new int[input.length - b.length];
59+
60+
for(int i = 0; i < mid; i++)
61+
b[i] = input[i];
62+
63+
for(int i = mid; i < input.length; i++)
64+
c[i - mid] = input[i];
65+
66+
mergeSort(b);
67+
mergeSort(c);
68+
merge(b,c,input);
69+
70+
}
71+
}

0 commit comments

Comments
 (0)