-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRecursion-6
More file actions
206 lines (193 loc) · 7.88 KB
/
Recursion-6
File metadata and controls
206 lines (193 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
Q1 - Count all the possible paths on an m x n grid from top left (grid[0][0]) to bottom right (grid[m-1][n-1]) having constraints that from each cell you can either move only to right or down. (Easy)
Input m = 2, n = 3
Expected Output 3
import java.util.*;
import java.util.Scanner;
public class Test{
static int level = -1;
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the dimensions of the matrix: ");
int m = scn.nextInt();
int n = scn.nextInt();
int sum = 0;
int i = 0;
int j = 0;
System.out.println(NumberOfPaths(m , n , i , j));
}
public static int NumberOfPaths(int m , int n , int i , int j){
if(i >= m || j >= n)
return 0;
if(i == m - 1 && j == n - 1)
return 1;
return NumberOfPaths(m , n , i + 1 , j) + NumberOfPaths(m , n , i , j + 1);
}
}
Q2 - Given an array of integers, print a sum triangle from it such that the first level(the bottom
level in triangular fashion) has all array elements. From then, at each level, the number of
elements is one less than the previous level and elements at the level is the sum of
consecutive two elements in the previous level.
(Medium)
Input1 n = 5
arr = {1, 2, 3, 4, 5}
Output1 [48]
[20, 28]
[8, 12, 16]
[3, 5, 7, 9]
[1, 2, 3, 4, 5]
import java.util.*;
import java.util.Scanner;
public class Test{
static int level = -1;
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the length of array: ");
int n = scn.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of array: ");
for(int i = 0; i < n; i++){
arr[i] = scn.nextInt();
}
printTriangle(arr);
}
public static void printTriangle(int[] arr) {
if (arr.length < 1)
return;
// Creating new array which contains the sum of consecutive elements in the array passes as parameter.
int[] temp = new int[arr.length - 1];
for (int i = 0; i < arr.length - 1; i++) {
int x = arr[i] + arr[i + 1];
temp[i] = x;
}
// Make a recursive call and pass the newly created array
printTriangle(temp);
// Print current array in the end so that smaller arrays are printed first
System.out.println(Arrays.toString(arr));
}
}
Q3 - Given an array of size n, generate and print all possible combinations of r elements in array. (Hard)
Input}
n = 4
{1, 2, 3, 4}
r = 2
Expected Output}
{1, 2}
{1, 3}
{1, 4}
{2, 3}
{2, 4}
{3, 4}
import java.util.*;
import java.util.Scanner;
public class Test{
static int level = -1;
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the length of array: ");
int n = scn.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of array: ");
for(int i = 0; i < n; i++){
arr[i] = scn.nextInt();
}
System.out.println("Enter r: ");
int r = scn.nextInt();
printCombination(arr, n, r);
}
public static void printCombination(int[] arr, int n, int r) {
// A temporary array to store all combination one by one
int data[]=new int[r];
// Print all combination using temporary array 'data[]'
combination(arr, n, r, 0, data, 0);
}
public static void combination(int[] arr, int n, int r, int index, int[] data, int i) {
// Current combination is ready to be printed, print it
if (index == r) {
for (int j=0; j<r; j++)
System.out.print(data[j]+" ");
System.out.println("");
return;
}
// When no more elements are there to put in data[]
if (i >= n)
return;
// current is included, put next at next location
data[index] = arr[i];
combination(arr, n, r, index+1, data, i+1);
// current is excluded, replace it with next (Note that i+1 is passed, but index is not changed)
combination(arr, n, r, index, data, i+1);
}
}
Q4 - Given two sorted arrays A and B of length m and n respectively, generate all possible arrays
such that the first element is taken from A then from B then from A, and so on in increasing order
till the arrays are exhausted. The generated arrays should end with an element from B.
(Hard)
Input:
m = 3
n = 4
A = {10, 15, 25}
B = {1, 5, 20, 30}
Expected Output:
10 20
10 20 25 30
10 30
15 20
15 20 25 30
15 30
25 30
import java.util.*;
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter the length of both the arrays: ");
int m = scn.nextInt();
int n = scn.nextInt();
int[] A = new int[m];
int[] B = new int[n];
System.out.println("Enter the elements of first array: ");
for(int i = 0; i < m; i++){
A[i] = scn.nextInt();
}
System.out.println("Enter the elements of second array: ");
for(int i = 0; i < n; i++){
B[i] = scn.nextInt();
}
int[] C = new int[m + n];
generate(A, B, C, 0, 0, m, n, 0, true);
}
public static void generate(int[] A, int[] B, int[] C, int i, int j, int m, int n, int len, boolean flag){
if (flag) {
// Include valid element from A
// Print output if there is at least one 'B' in output array 'C'
if (len != 0) {
printArr(C, len + 1);
}
// Recur for all elements of A after current index
for (int k = i; k < m; k++) {
if (len == 0) { //this block works for the very first call to include the first element in the output array
C[len] = A[k];
// don't increment len as B is included
generate(A, B, C, k + 1, j, m, n, len, !flag);
}
else if (A[k] > C[len]) { // include valid element from A and recur
C[len + 1] = A[k];
generate(A, B, C, k + 1, j, m, n, len + 1, !flag);
}
}
}
else { //Include valid element from B and recur
for (int l = j; l < n; l++) {
if (B[l] > C[len]) {
C[len + 1] = B[l];
generate(A, B, C, i, l + 1, m, n, len + 1, !flag);
}
}
}
}
public static void printArr(int[] arr, int n) {
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println("");
}
}