2
votes

I need some hints to find an algorithm that returns 5 elements whose sum is closest or equal to a given number.

Those elements are numbers greater than 0 and shall be "used" only 1 time each when trying to get the given number.

Let's say we got an array {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} and the number we're trying to get 21. It should returns {2, 3, 4, 5, 7}.

Any help is greatly appreciated!

2
Hint: You can solve this using recursion with memoization.kasavbere
Start with 3-SUM (en.wikipedia.org/wiki/3SUM) and advance from there, in the mean time lose any hopes (if you had any) about linear-time algorithms.mmgp
What is possible max value for provided "21". In Problem may exists negative values?Толя
I'm not sure if I understand your question. There is no max value concerning the number we're searching, it could be 21 or more (anything while it's a positive value). But the array won't have any negative values.Ticko

2 Answers

1
votes
ok n loops all begin with counts = 0 (in this case n = 5 )
all loops end at MainArraySize (in this case MainArraySize = 10)

int RequiredSum = ValueOfRequiredSum;
int CurrentSum = 0;
int CurrentClosestSum = 0;
int[] Finalindexesrequired = int[5]{0,0,0,0,0};

//U might want to add Duplicates 
duplicate_count ++;
int[5][] FinalindexesRequiredDuplicates= new int[5][];


for(int loopcount1 = 0, loopcount1++, loopcount < MainArraySize-1)
{
for(int loopcount2 = 0, loopcount2++, loopcount < MainArraySize-1)
{..
..
..
for(int loopcount5 = 0, loopcount5++, loopcount < MainArraySize-1)
{

-------------------------------------
Now this is all inside the 5th loop
//Process logic here
//Looping for first time
if(CurrentSum = 0)
{Currentsum = MainArray[loopcount1] + MainArray[loopcount2] + .... + MainArray[loopcount5]
CurrentClosestSum = CurrentSum
FinalindexesRequired[0] = loopcount1;
FinalindexesRequired[1] = loopcount2;
..
..
FinalindexesRequired[4] = loopcount2;
}

Currentsum = MainArray[loopcount1] + MainArray[loopcount2] + .... + MainArray[loopcount5]


if((RequiredSum - CurrentSum) < (RequiredSum - CurrentClosestSum))
{
//Am gonna change the indexes because the currentsum ITERATION is closer
FinalindexesRequired[0] = loopcount1;
FinalindexesRequired[1] = loopcount2;
..
..
FinalindexesRequired[4] = loopcount2;

//If u wanted the duplicates also, since u came to a fresher ITERATION
Reset the duplicatecount to 0 and remove all duplicates because they aint valid anymore
}

//What u Might want to Add is this
if((Requiredsum - CurrentSum) = (RequiredSum - CurrentCosestSum))
{
//Hey we got Duplicates
duplicate_count ++;
FinalindexesRequiredDuplicates[0][duplicate_count] = loopcount1;
FinalindexesRequiredDuplicates[1][duplicate_count] = loopcount1;
..
..
FinalindexesRequiredDuplicates[5][duplicate_count] = loopcount1;
}

}



-------------------------------- End of 5th loop
}}}}}



//FINALLY AFTER EXITING I THINK U HAVE UR ANSWER IN

MAINARRAY[FINALINDEXESREQUIRED[0]]
MAINARRAY[FINALINDEXESREQUIRED[1]]
MAINARRAY[FINALINDEXESREQUIRED[2]]
MAINARRAY[FINALINDEXESREQUIRED[3]]
MAINARRAY[FINALINDEXESREQUIRED[4]]


//IN CASE OF DUPLICATES U HAVE UR ANSWER IN
set 1:
MAINARRAY[FINALINDEXESREQUIRED[0]]
MAINARRAY[FINALINDEXESREQUIRED[1]]
MAINARRAY[FINALINDEXESREQUIRED[2]]
MAINARRAY[FINALINDEXESREQUIRED[3]]
MAINARRAY[FINALINDEXESREQUIRED[4]]

other sets:
MAINARRAY[FinalindexesRequiredDuplicates[0][0...n]]
MAINARRAY[FinalindexesRequiredDuplicates[1][0...n]]
MAINARRAY[FinalindexesRequiredDuplicates[2][0...n]]
MAINARRAY[FinalindexesRequiredDuplicates[3][0...n]]
MAINARRAY[FinalindexesRequiredDuplicates[4][0...n]]
0
votes

if the target is 21, why shouldn't it return {2,3,4,5,7} instead of {2,3,4,5,6}? very confusing...

if my understanding is correct, this problem can be solved by DP. it's very similar to knap-sack problem. the time complexity is O(n*S) where n is the size of array and S is your target