Skip to content

Commit da1abd9

Browse files
committed
Add day23
1 parent ba2a038 commit da1abd9

File tree

10 files changed

+250
-3
lines changed

10 files changed

+250
-3
lines changed

BONUS/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@ In case you just want to contribute a question and not code, there is no need to
4848

4949
- To be added
5050

51-
### [2. Recursion](./Recursion/README.md)
52-
53-
- To be added
51+
### [2. Recursion](./Recursion/)
52+
53+
1. [WAP to calculate `a` raised to the power `n` using recursion](./Recursion/Power/)
54+
2. [WAP to calculate the factorial of a given number usinig recursion](./Recursion/Factorial/)
55+
3. [WAP to calculate the product of elements in the given array using recursion](./Recursion/ArrayProduct/)
56+
4. [WAP to calculate the sum of elements in a given integer array using recursion](./Recursion/ArraySum/)
57+
5. [WAP to find the nth number in Fibonacci Series using recursion](./Recursion/Fibonacci/)
58+
6. [WAP to reverse a string using recursion](./Recursion/ReverseString/)
59+
7. [WAP to reverse an integer array using recursion](./Recursion/ReverseArray/)
5460

5561
### [3. Array](./Arrays/README.md)
5662

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Reverse Array
2+
3+
WAP to reverse an array using recursion
4+
5+
**Example**
6+
7+
```
8+
input: [1, 2, 3, 4]
9+
output: [4, 3, 2, 1]
10+
```
11+
12+
## Solution
13+
14+
### [JavaScript Implementation (method 1)](./reverseArray.js)
15+
16+
```js
17+
/**
18+
* Reverse an array using recursion
19+
* Method 1
20+
* Implemented by MadhavBahlMD
21+
* @date 18/01/2019
22+
*/
23+
24+
function reverseArray (arr) {
25+
if (arr.length === 1) return arr;
26+
return reverseArray(arr.slice(1, arr.length)).concat([arr[0]]);
27+
}
28+
29+
console.log (reverseArray([1, 2, 3, 4]));
30+
```
31+
32+
### [JavaScript Implementation (method 2)](./reverseArray.js)
33+
34+
```js
35+
/**
36+
* Reverse an array using recursion
37+
* Method 1
38+
* Implemented by MadhavBahlMD
39+
* @date 18/01/2019
40+
*/
41+
42+
function reverseArray (arr, startIndex, endIndex) {
43+
if (startIndex >= endIndex) return arr;
44+
45+
let temp = arr[startIndex];
46+
arr[startIndex] = arr[endIndex];
47+
arr[endIndex] = temp;
48+
49+
reverseArray (arr, startIndex+1, endIndex-1);
50+
}
51+
52+
let arr = [1, 2, 3, 4];
53+
console.log (`Original Array: ${arr}`);
54+
reverseArray (arr, 0, arr.length-1);
55+
console.log (`Reversed Array: ${arr}`);
56+
```
57+
58+
### [Java Implementation](./ReverseArray.java)
59+
60+
```java
61+
import java.util.Scanner;
62+
63+
/**
64+
* Reverse an array using recursion
65+
* @author MadhavBahlMD
66+
* @date 18/01/2019
67+
*/
68+
69+
public class ReverseArray {
70+
public static void reverse (int[] arr, int startIndex, int endIndex) {
71+
if (startIndex >= endIndex) return;
72+
73+
// Swap equidistant elements from start and end
74+
int temp = arr[startIndex];
75+
arr[startIndex] = arr[endIndex];
76+
arr[endIndex] = temp;
77+
78+
reverse (arr, startIndex+1, endIndex-1);
79+
}
80+
81+
public static void main(String[] args) {
82+
Scanner input = new Scanner (System.in);
83+
System.out.println("/* ===== Sum of Array elements using recursion ===== */");
84+
85+
// Input the array
86+
System.out.print("\nEnter the number of elements in the array: ");
87+
int n = input.nextInt();
88+
int arr[] = new int[n];
89+
for (int i=0; i<n; i++) {
90+
System.out.print("Enter arr[" + i + "]: ");
91+
arr[i] = input.nextInt();
92+
}
93+
94+
// Print the original array
95+
System.out.print("Original Array: ");
96+
for (int i=0; i<arr.length; i++)
97+
System.out.print(arr[i] + " ");
98+
99+
// Reverse the array
100+
reverse(arr, 0, arr.length-1);
101+
102+
// Print the reversed array
103+
System.out.print("\nReversed Array: ");
104+
for (int i=0; i<arr.length; i++)
105+
System.out.print(arr[i] + " ");
106+
}
107+
}
108+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Reverse an array using recursion
5+
* @author MadhavBahlMD
6+
* @date 18/01/2019
7+
*/
8+
9+
public class ReverseArray {
10+
public static void reverse (int[] arr, int startIndex, int endIndex) {
11+
if (startIndex >= endIndex) return;
12+
13+
// Swap equidistant elements from start and end
14+
int temp = arr[startIndex];
15+
arr[startIndex] = arr[endIndex];
16+
arr[endIndex] = temp;
17+
18+
reverse (arr, startIndex+1, endIndex-1);
19+
}
20+
21+
public static void main(String[] args) {
22+
Scanner input = new Scanner (System.in);
23+
System.out.println("/* ===== Sum of Array elements using recursion ===== */");
24+
25+
// Input the array
26+
System.out.print("\nEnter the number of elements in the array: ");
27+
int n = input.nextInt();
28+
int arr[] = new int[n];
29+
for (int i=0; i<n; i++) {
30+
System.out.print("Enter arr[" + i + "]: ");
31+
arr[i] = input.nextInt();
32+
}
33+
34+
// Print the original array
35+
System.out.print("Original Array: ");
36+
for (int i=0; i<arr.length; i++)
37+
System.out.print(arr[i] + " ");
38+
39+
// Reverse the array
40+
reverse(arr, 0, arr.length-1);
41+
42+
// Print the reversed array
43+
System.out.print("\nReversed Array: ");
44+
for (int i=0; i<arr.length; i++)
45+
System.out.print(arr[i] + " ");
46+
}
47+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Reverse an array using recursion
3+
* Method 1
4+
* Implemented by MadhavBahlMD
5+
* @date 18/01/2019
6+
*/
7+
8+
function reverseArray (arr) {
9+
if (arr.length === 1) return arr;
10+
return reverseArray(arr.slice(1, arr.length)).concat([arr[0]]);
11+
}
12+
13+
console.log (reverseArray([1, 2, 3, 4]));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Reverse an array using recursion
3+
* Method 1
4+
* Implemented by MadhavBahlMD
5+
* @date 18/01/2019
6+
*/
7+
8+
function reverseArray (arr, startIndex, endIndex) {
9+
if (startIndex >= endIndex) return arr;
10+
11+
let temp = arr[startIndex];
12+
arr[startIndex] = arr[endIndex];
13+
arr[endIndex] = temp;
14+
15+
reverseArray (arr, startIndex+1, endIndex-1);
16+
}
17+
18+
let arr = [1, 2, 3, 4];
19+
console.log (`Original Array: ${arr}`);
20+
reverseArray (arr, 0, arr.length-1);
21+
console.log (`Reversed Array: ${arr}`);

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
4646
20. [Day 20 -- Array Partition](./day20) -- [http://codetoexpress.tech/dc/day20/](http://codetoexpress.tech/dc/day20/)
4747
21. [Day 21 -- Pair Sum N and Max Subarray Sum](./day21) -- [http://codetoexpress.tech/dc/day21/](http://codetoexpress.tech/dc/day21/)
4848
22. [Day 22 -- Common Elements Search](./day22) -- [http://codetoexpress.tech/dc/day22/](http://codetoexpress.tech/dc/day22/)
49+
23. [Day 23 -- Combination Sum](./day23) -- [http://codetoexpress.tech/dc/day23/](http://codetoexpress.tech/dc/day23/)
4950

5051
## [More Problems](./BONUS/README.md)
5152

@@ -93,6 +94,15 @@ In case you just want to contribute a question and not code, there is no need to
9394
7. [**Linked Lists**](./BONUS/LinkedLists/README.md)
9495
8. [**Trees**](./BONUS/Trees/README.md)
9596
9. [**Graphs**](./BONUS/Graphs/README.md)
97+
10. [**Misc Questions**](./BONUS/Misc/README.md)
98+
99+
## EXTRA - Past Interview Questions
100+
101+
Here are some of the questions asked previously in major companies like Google, Amazon, Facebook, Microsoft etc.
102+
103+
```
104+
To Be Added
105+
```
96106

97107
## Timeline
98108

day23/JavaScript/combinationSum.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// To Be Added

day23/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
![cover](./cover.png)
2+
3+
# Day 23 - Array Series Part 6: Combination Sum
4+
5+
**Question Source -- [Geeks4Geeks](https://www.geeksforgeeks.org/combinational-sum/)**
6+
7+
**Question** -- Given a set of integers, and a number, find all unique combinations in the integer array whose sum equals the given number.
8+
9+
**Example**
10+
11+
```
12+
input: arr = [2,3,6,7], num = 7
13+
output: Solution set -
14+
[
15+
[7],
16+
[2,2,3]
17+
]
18+
19+
input: arr = [2, 4, 6, 8], num = 8
20+
output: Solution set -
21+
[
22+
[2, 2, 2, 2]
23+
[2, 2, 4]
24+
[2, 6]
25+
[4, 4]
26+
[8]
27+
]
28+
```
29+
30+
![ques](./ques.png)
31+
32+
## Solution
33+
34+
## JavaScript Implementation
35+
36+
### [Soluion](./JavaScript/combinationSum.js)
37+
38+
```
39+
to be added
40+
```
41+

day23/cover.png

141 KB
Loading

day23/ques.png

1.14 MB
Loading

0 commit comments

Comments
 (0)