-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKSubsets.swift
More file actions
executable file
·88 lines (77 loc) · 3.22 KB
/
KSubsets.swift
File metadata and controls
executable file
·88 lines (77 loc) · 3.22 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
//
// Created by Daniel Heredia on 2/27/18.
// Copyright © 2018 Daniel Heredia. All rights reserved.
//
// K-Subsets
// Subset sum problem is to find subset of elements that are selected from a given set whose
// sum adds up to a given number K. We are considering the set contains non-negative values.
// It is assumed that the input set is unique (no duplicates are presented).
import Foundation
func getKSubsets(elements:[Int], k: Int, subset: inout [Int], index: Int, currentSum: Int, resultSubsets: inout [[Int]]) {
// Check if the sum of the current subset is equal to k
if currentSum == k {
// A valid subset was found, save it
resultSubsets.append(subset)
// Discard the last element added and continue with the next one
// to see if there are more subsets with the first part of the
// found subset
if index < elements.count {
let removedElement = subset.removeLast()
let newCurrentSum = currentSum - removedElement
// Since the elements are sorted if the next element exceeds
// k, any further element will exceed it too.
if newCurrentSum + elements[index] <= k {
getKSubsets(elements: elements,
k: k,
subset: &subset,
index: index,
currentSum: newCurrentSum,
resultSubsets: &resultSubsets)
}
}
} else {
// Return if the index is out of scope
if index >= elements.count{
return
}
// Loop the array to try to add each element as the next element
// in the subset
for i in index..<elements.count {
let element = elements[i]
let newCurrentSum = currentSum + element
if newCurrentSum <= k {
//If the current sum is less than k
//the current element is added as element of the subset
// and the function is called again with this new subset and sum
subset.append(element)
getKSubsets(elements: elements,
k: k, subset: &subset,
index: i + 1,
currentSum: newCurrentSum,
resultSubsets: &resultSubsets)
// Remove the inserted element if necessary
if subset.last == element {
subset.removeLast()
}
} else {
// Since the elements are sorted, if the addition of the next element exceeds
// k, any further element will exceed it too.
return
}
}
}
}
func getKSubsets(elements:[Int], k: Int) -> [[Int]] {
var resultSubsets = [[Int]]()
let sortedElements = elements.sorted()
var subset = [Int]()
getKSubsets(elements: sortedElements, k: k, subset: &subset, index: 0, currentSum: 0, resultSubsets: &resultSubsets)
return resultSubsets
}
var elements = [39, 22, 15, 14, 1, 29]
var k = 30
let resultSubsets = getKSubsets(elements: elements, k: k)
print("K subsets for \(k):")
for subset in resultSubsets {
print("\(subset)")
}