Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func printCombination(arr: [Int], r: Int) {
- trips.removeAll()
- var data: [Int] = []
- for _ in 1...r
- {
- data.append(Int())
- }
- combinationUtil(arr: arr, r: r, index: 0, data: data, i: 0)
- }
- func combinationUtil(arr: [Int], r: Int, index: Int, data: [Int], i: Int) {
- var data: [Int] = data
- if (index == r)
- {
- for j in 0..<r {
- array.append(data[j])
- }
- return
- }
- if (i >= arr.count) {
- return
- }
- data[index] = arr[i]
- combinationUtil(arr: arr, r: r, index: index + 1, data: data, i: i + 1)
- combinationUtil(arr: arr, r: r, index: index, data: data, i: i + 1)
- }
- /* arr[] ---> Input Array
- r ---> Size of a combination to be printed
- index ---> Current index in data[]
- data[] ---> Temporary array to store current combination
- i ---> index of current element in arr[] */
Add Comment
Please, Sign In to add comment