i has an array that contains only 2 types of numbers (x and x-1), for example: - {5,5,4,4,5,5,5}, and I am assigned a range of 12-14 (inclusive). I already know that the length of the array is constant 7, and I also know how many elements of each type are in the array (count)
now I need to find if there is any combination of elements in the array whose sum falls into this range.
All I need is the number of elements in the subset, the sum of which falls into this range.
I solved this problem using brute force as follows, but it is very effective.
here count is the number x-1 in the array
for(int i=0;i<=7-count;i++){ for(int j=0;j<=count;j++){ if(x*(i)+(x-1)*j>=min && x*(i)+(x-1)*j<=max){ output1=i+j; } } }
can anyone tell me if there is a better way to solve this
example: -
the given array is {5,5,4,4,5,5,5}, and the given range is 12-14.
therefore, I would choose a subset {5,5,4} whose sum is 14, and therefore the answer to the number of elements in the subset will be 3. {5,4,4} can also be selected in this solution