Guest User

Untitled

a guest
Dec 13th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. func printCombination(arr: [Int], r: Int) {
  2. trips.removeAll()
  3. var data: [Int] = []
  4. for _ in 1...r
  5. {
  6. data.append(Int())
  7. }
  8. combinationUtil(arr: arr, r: r, index: 0, data: data, i: 0)
  9. }
  10.  
  11. func combinationUtil(arr: [Int], r: Int, index: Int, data: [Int], i: Int) {
  12. var data: [Int] = data
  13. if (index == r)
  14. {
  15. for j in 0..<r {
  16. array.append(data[j])
  17.  
  18. }
  19. return
  20. }
  21.  
  22. if (i >= arr.count) {
  23. return
  24. }
  25.  
  26. data[index] = arr[i]
  27. combinationUtil(arr: arr, r: r, index: index + 1, data: data, i: i + 1)
  28. combinationUtil(arr: arr, r: r, index: index, data: data, i: i + 1)
  29. }
  30. /* arr[] ---> Input Array
  31. r ---> Size of a combination to be printed
  32. index ---> Current index in data[]
  33. data[] ---> Temporary array to store current combination
  34. i ---> index of current element in arr[] */
Add Comment
Please, Sign In to add comment