Skip to content

Commit cafb343

Browse files
authored
Update AllCombinationsOfSizeK.js (TheAlgorithms#1530)
* Update AllCombinationsOfSizeK.js * Update AllCombinationsOfSizeK.js * Update AllCombinationsOfSizeK.test.js Changes made it the type of testing. Instead of testing the class now the program will test the function * Update AllCombinationsOfSizeK.js * Update AllCombinationsOfSizeK.js * Update AllCombinationsOfSizeK.js * Update AllCombinationsOfSizeK.test.js * Update AllCombinationsOfSizeK.test.js
1 parent 1cc5e61 commit cafb343

File tree

2 files changed

+28
-46
lines changed

2 files changed

+28
-46
lines changed
Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,28 @@
1-
/*
2-
Problem: Given two numbers, n and k, make all unique combinations of k numbers from 1 to n and in sorted order
3-
4-
What is combinations?
5-
- Combinations is selecting items from a collections without considering the order of selection
6-
7-
Example:
8-
- We have an apple, a banana, and a jackfruit
9-
- We have three objects, and need to choose two items, then combinations will be
10-
11-
1. Apple & Banana
12-
2. Apple & Jackfruit
13-
3. Banana & Jackfruit
14-
15-
To read more about combinations, you can visit the following link:
16-
- https://betterexplained.com/articles/easy-permutations-and-combinations/
17-
18-
Solution:
19-
- We will be using backtracking to solve this questions
20-
- Take one element, and make all them combinations for k-1 elements
21-
- Once we get all combinations of that element, pop it and do same for next element
22-
*/
23-
24-
class Combinations {
25-
constructor(n, k) {
26-
this.n = n
27-
this.k = k
28-
this.current = [] // will be used for storing current combination
29-
this.combinations = []
30-
}
31-
32-
findCombinations(high = this.n, total = this.k, low = 1) {
33-
if (total === 0) {
34-
this.combinations.push([...this.current])
35-
return this.combinations
1+
function generateCombinations(n, k) {
2+
let currentCombination = []
3+
let allCombinations = [] // will be used for storing all combinations
4+
let currentValue = 1
5+
6+
function findCombinations() {
7+
if (currentCombination.length === k) {
8+
// Add the array of size k to the allCombinations array
9+
allCombinations.push([...currentCombination])
10+
return
3611
}
37-
for (let i = low; i <= high; i++) {
38-
this.current.push(i)
39-
this.findCombinations(high, total - 1, i + 1)
40-
this.current.pop()
12+
if (currentValue > n) {
13+
// Check for exceeding the range
14+
return
4115
}
42-
return this.combinations
16+
currentCombination.push(currentValue++)
17+
findCombinations()
18+
currentCombination.pop()
19+
findCombinations()
20+
currentValue--
4321
}
22+
23+
findCombinations()
24+
25+
return allCombinations
4426
}
4527

46-
export { Combinations }
28+
export { generateCombinations }

Backtracking/tests/AllCombinationsOfSizeK.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { Combinations } from '../AllCombinationsOfSizeK'
1+
import { generateCombinations } from '../AllCombinationsOfSizeK'
22

33
describe('AllCombinationsOfSizeK', () => {
44
it('should return 3x2 matrix solution for n = 3 and k = 2', () => {
5-
const test1 = new Combinations(3, 2)
6-
expect(test1.findCombinations()).toEqual([
5+
const res = generateCombinations(3, 2)
6+
expect(res).toEqual([
77
[1, 2],
88
[1, 3],
99
[2, 3]
1010
])
1111
})
1212

1313
it('should return 6x2 matrix solution for n = 4 and k = 2', () => {
14-
const test2 = new Combinations(4, 2)
15-
expect(test2.findCombinations()).toEqual([
14+
const res = generateCombinations(4, 2)
15+
expect(res).toEqual([
1616
[1, 2],
1717
[1, 3],
1818
[1, 4],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy