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

Stats:

CreatedApr 16, 2016
PublishedApr 16, 2016
Warriors Trained1316
Total Skips47
Total Code Submissions2027
Total Times Completed328
Python Completions192
JavaScript Completions71
Ruby Completions50
C# Completions22
Rust Completions22
Total Stars58
% of votes with a positive feedback rating77% of 101
Total "Very Satisfied" Votes65
Total "Somewhat Satisfied" Votes26
Total "Not Satisfied" Votes10
Total Rank Assessments9
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • raulbc777 Avatar
  • omegahm Avatar
  • suic Avatar
  • FArekkusu Avatar
  • bidouille Avatar
  • hobovsky Avatar
  • akar-0 Avatar
  • dfhwze Avatar
  • saudiGuy Avatar
Ad