6 kyu
Search The 0 Sums Combinations in an Array
192 of 328raulbc777
Description:
You are given an array of positive and negative integers and a number n
and n > 1
. The array may have elements that occurs more than once.
Find all the combinations of n elements of the array that their sum are 0.
arr = [1, -1, 2, 3, -2]
n = 3
find_zero_sum_groups(arr, n) == [-2, -1, 3] # -2 - 1 + 3 = 0
The function should ouput every combination or group in increasing order.
We may have more than one group:
arr = [1, -1, 2, 3, -2, 4, 5, -3 ]
n = 3
find_zero_sum_groups(arr, n) == [[-3, -2, 5], [-3, -1, 4], [-3, 1, 2], [-2, -1, 3]]
In the case above the function should output a sorted 2D array.
The function will not give a group twice, or more, only once.
arr = [1, -1, 2, 3, -2, 4, 5, -3, -3, -1, 2, 1, 4, 5, -3 ]
n = 3
find_zero_sum_groups(arr, n) == [[-3, -2, 5], [-3, -1, 4], [-3, 1, 2], [-2, -1, 3]]
If there are no combinations with sum equals to 0, the function will output an alerting message.
arr = [1, 1, 2, 3]
n = 2
find_zero_sum_groups(arr, n) == "No combinations"
If the function receives an empty array will output an specific alert:
arr = []
n = 2
find_zero_sum_groups(arr, n) == "No elements to combine"
As you have seen the solutions may have a value occurring only once. Enjoy it!
Fundamentals
Data Structures
Algorithms
Mathematics
Logic
Similar Kata:
Stats:
Created | Apr 16, 2016 |
Published | Apr 16, 2016 |
Warriors Trained | 1316 |
Total Skips | 47 |
Total Code Submissions | 2027 |
Total Times Completed | 328 |
Python Completions | 192 |
JavaScript Completions | 71 |
Ruby Completions | 50 |
C# Completions | 22 |
Rust Completions | 22 |
Total Stars | 58 |
% of votes with a positive feedback rating | 77% of 101 |
Total "Very Satisfied" Votes | 65 |
Total "Somewhat Satisfied" Votes | 26 |
Total "Not Satisfied" Votes | 10 |
Total Rank Assessments | 9 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 7 kyu |