The collection of number is given in 2d array is [4,5,3,2] and how can we set algorithm to get sum = 10 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 16

The collection of number is given in 2d array is [4,5,3,2] and how can we set algorithm to get sum = 10 ?

From using array such as [4,5,3,2] => sum = 10 ... Take one another collection also let it be [9,7,2,3] => sum =10

19th May 2019, 4:29 AM
Prince Raj
Prince Raj - avatar
8 Answers
+ 14
If I understand your question then this is the code which gives you the sum of the 2D table https://code.sololearn.com/Wlhxs4y3Lddp/?ref=app
19th May 2019, 5:09 AM
Why So Serious ?
Why So Serious ? - avatar
+ 12
No that algorithm wouldn't valid here that was sum of total collection of number in array but here i want sum = 10 exactly using no. of array which i provide if you applied that algorithm here the answer you will get 14 but i want sum = 10 exactly
19th May 2019, 6:27 AM
Prince Raj
Prince Raj - avatar
+ 12
In next example we will have to address only 7 & 3 with their index no. to get sum =10 😄 on the methodology of choose the best fu*k the rest😂 Denise Roßberg
19th May 2019, 1:01 PM
Prince Raj
Prince Raj - avatar
+ 10
Can be called Choe i think
19th May 2019, 3:04 PM
Prince Raj
Prince Raj - avatar
+ 5
With a pair it is easy: Inside a loop you take the first value --> if list contains 10 - first value --> pair found else: try next value in your list More than two nums: I think to calc the full sum is not a bad idea. If full sum < 10 --> no sum can be found else: e.g. 4 + 5 + 3 + 2 = 14 14 - 10 = 4 --> inside a loop you can search: if(list contains full sum - 10) --> remove this element In your next example full sum is 21. You don't have an 11 in your array. So you need to search if you have 2 or more nums in your list which sum is 11 (here: 9 + 2)
19th May 2019, 12:52 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
In python: L=[4,5,3,2] Sum=0 for i in L: Sum=Sum+(i-1) print(sum)
4th Jul 2019, 8:00 AM
ANJALI SAHU
+ 1
hm, this question seems oddly familiar to something ive seen before.. what was it again? https://www.sololearn.com/learn/4755/
19th May 2019, 2:48 PM
Choe
Choe - avatar
+ 1
you can use the knapsack algorithm .. Google it ..for the given sack of 10 you can use either the greedy method or the dynamic method to add the weights into the sack weights here are your array elements to the sack until its full
25th May 2019, 2:51 PM
Shreenidhi Bharadwaj Hk
Shreenidhi Bharadwaj Hk - avatar