Skip to content

Commit

Permalink
Add day23
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Jan 21, 2019
1 parent ba2a038 commit da1abd9
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 3 deletions.
12 changes: 9 additions & 3 deletions BONUS/README.md
Expand Up @@ -48,9 +48,15 @@ In case you just want to contribute a question and not code, there is no need to

- To be added

### [2. Recursion](./Recursion/README.md)

- To be added
### [2. Recursion](./Recursion/)

1. [WAP to calculate `a` raised to the power `n` using recursion](./Recursion/Power/)
2. [WAP to calculate the factorial of a given number usinig recursion](./Recursion/Factorial/)
3. [WAP to calculate the product of elements in the given array using recursion](./Recursion/ArrayProduct/)
4. [WAP to calculate the sum of elements in a given integer array using recursion](./Recursion/ArraySum/)
5. [WAP to find the nth number in Fibonacci Series using recursion](./Recursion/Fibonacci/)
6. [WAP to reverse a string using recursion](./Recursion/ReverseString/)
7. [WAP to reverse an integer array using recursion](./Recursion/ReverseArray/)

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

Expand Down
108 changes: 108 additions & 0 deletions BONUS/Recursion/ReverseArray/README.md
@@ -0,0 +1,108 @@
# Reverse Array

WAP to reverse an array using recursion

**Example**

```
input: [1, 2, 3, 4]
output: [4, 3, 2, 1]
```

## Solution

### [JavaScript Implementation (method 1)](./reverseArray.js)

```js
/**
* Reverse an array using recursion
* Method 1
* Implemented by MadhavBahlMD
* @date 18/01/2019
*/

function reverseArray (arr) {
if (arr.length === 1) return arr;
return reverseArray(arr.slice(1, arr.length)).concat([arr[0]]);
}

console.log (reverseArray([1, 2, 3, 4]));
```

### [JavaScript Implementation (method 2)](./reverseArray.js)

```js
/**
* Reverse an array using recursion
* Method 1
* Implemented by MadhavBahlMD
* @date 18/01/2019
*/

function reverseArray (arr, startIndex, endIndex) {
if (startIndex >= endIndex) return arr;

let temp = arr[startIndex];
arr[startIndex] = arr[endIndex];
arr[endIndex] = temp;

reverseArray (arr, startIndex+1, endIndex-1);
}

let arr = [1, 2, 3, 4];
console.log (`Original Array: ${arr}`);
reverseArray (arr, 0, arr.length-1);
console.log (`Reversed Array: ${arr}`);
```

### [Java Implementation](./ReverseArray.java)

```java
import java.util.Scanner;

/**
* Reverse an array using recursion
* @author MadhavBahlMD
* @date 18/01/2019
*/

public class ReverseArray {
public static void reverse (int[] arr, int startIndex, int endIndex) {
if (startIndex >= endIndex) return;

// Swap equidistant elements from start and end
int temp = arr[startIndex];
arr[startIndex] = arr[endIndex];
arr[endIndex] = temp;

reverse (arr, startIndex+1, endIndex-1);
}

public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("/* ===== Sum of Array elements using recursion ===== */");

// Input the array
System.out.print("\nEnter the number of elements in the array: ");
int n = input.nextInt();
int arr[] = new int[n];
for (int i=0; i<n; i++) {
System.out.print("Enter arr[" + i + "]: ");
arr[i] = input.nextInt();
}

// Print the original array
System.out.print("Original Array: ");
for (int i=0; i<arr.length; i++)
System.out.print(arr[i] + " ");

// Reverse the array
reverse(arr, 0, arr.length-1);

// Print the reversed array
System.out.print("\nReversed Array: ");
for (int i=0; i<arr.length; i++)
System.out.print(arr[i] + " ");
}
}
```
47 changes: 47 additions & 0 deletions BONUS/Recursion/ReverseArray/ReverseArray.java
@@ -0,0 +1,47 @@
import java.util.Scanner;

/**
* Reverse an array using recursion
* @author MadhavBahlMD
* @date 18/01/2019
*/

public class ReverseArray {
public static void reverse (int[] arr, int startIndex, int endIndex) {
if (startIndex >= endIndex) return;

// Swap equidistant elements from start and end
int temp = arr[startIndex];
arr[startIndex] = arr[endIndex];
arr[endIndex] = temp;

reverse (arr, startIndex+1, endIndex-1);
}

public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("/* ===== Sum of Array elements using recursion ===== */");

// Input the array
System.out.print("\nEnter the number of elements in the array: ");
int n = input.nextInt();
int arr[] = new int[n];
for (int i=0; i<n; i++) {
System.out.print("Enter arr[" + i + "]: ");
arr[i] = input.nextInt();
}

// Print the original array
System.out.print("Original Array: ");
for (int i=0; i<arr.length; i++)
System.out.print(arr[i] + " ");

// Reverse the array
reverse(arr, 0, arr.length-1);

// Print the reversed array
System.out.print("\nReversed Array: ");
for (int i=0; i<arr.length; i++)
System.out.print(arr[i] + " ");
}
}
13 changes: 13 additions & 0 deletions BONUS/Recursion/ReverseArray/reverseArray.js
@@ -0,0 +1,13 @@
/**
* Reverse an array using recursion
* Method 1
* Implemented by MadhavBahlMD
* @date 18/01/2019
*/

function reverseArray (arr) {
if (arr.length === 1) return arr;
return reverseArray(arr.slice(1, arr.length)).concat([arr[0]]);
}

console.log (reverseArray([1, 2, 3, 4]));
21 changes: 21 additions & 0 deletions BONUS/Recursion/ReverseArray/reverseArray2.js
@@ -0,0 +1,21 @@
/**
* Reverse an array using recursion
* Method 1
* Implemented by MadhavBahlMD
* @date 18/01/2019
*/

function reverseArray (arr, startIndex, endIndex) {
if (startIndex >= endIndex) return arr;

let temp = arr[startIndex];
arr[startIndex] = arr[endIndex];
arr[endIndex] = temp;

reverseArray (arr, startIndex+1, endIndex-1);
}

let arr = [1, 2, 3, 4];
console.log (`Original Array: ${arr}`);
reverseArray (arr, 0, arr.length-1);
console.log (`Reversed Array: ${arr}`);
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -46,6 +46,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
20. [Day 20 -- Array Partition](./day20) -- [http://codetoexpress.tech/dc/day20/](http://codetoexpress.tech/dc/day20/)
21. [Day 21 -- Pair Sum N and Max Subarray Sum](./day21) -- [http://codetoexpress.tech/dc/day21/](http://codetoexpress.tech/dc/day21/)
22. [Day 22 -- Common Elements Search](./day22) -- [http://codetoexpress.tech/dc/day22/](http://codetoexpress.tech/dc/day22/)
23. [Day 23 -- Combination Sum](./day23) -- [http://codetoexpress.tech/dc/day23/](http://codetoexpress.tech/dc/day23/)

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

Expand Down Expand Up @@ -93,6 +94,15 @@ In case you just want to contribute a question and not code, there is no need to
7. [**Linked Lists**](./BONUS/LinkedLists/README.md)
8. [**Trees**](./BONUS/Trees/README.md)
9. [**Graphs**](./BONUS/Graphs/README.md)
10. [**Misc Questions**](./BONUS/Misc/README.md)

## EXTRA - Past Interview Questions

Here are some of the questions asked previously in major companies like Google, Amazon, Facebook, Microsoft etc.

```
To Be Added
```

## Timeline

Expand Down
1 change: 1 addition & 0 deletions day23/JavaScript/combinationSum.js
@@ -0,0 +1 @@
// To Be Added
41 changes: 41 additions & 0 deletions day23/README.md
@@ -0,0 +1,41 @@
![cover](./cover.png)

# Day 23 - Array Series Part 6: Combination Sum

**Question Source -- [Geeks4Geeks](https://www.geeksforgeeks.org/combinational-sum/)**

**Question** -- Given a set of integers, and a number, find all unique combinations in the integer array whose sum equals the given number.

**Example**

```
input: arr = [2,3,6,7], num = 7
output: Solution set -
[
[7],
[2,2,3]
]
input: arr = [2, 4, 6, 8], num = 8
output: Solution set -
[
[2, 2, 2, 2]
[2, 2, 4]
[2, 6]
[4, 4]
[8]
]
```

![ques](./ques.png)

## Solution

## JavaScript Implementation

### [Soluion](./JavaScript/combinationSum.js)

```
to be added
```

Binary file added day23/cover.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added day23/ques.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit da1abd9

Please sign in to comment.