Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba2a038
commit da1abd9
Showing
10 changed files
with
250 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] + " "); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] + " "); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// To Be Added |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` | ||
|
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.