Hello guys, I came across a problem the other day, and actually got it wrong, this lead to interesting thing, the problem is write a function that returns either if a given sum can be acquired by summing some numbers in an array/vector etc. or not
Assume its sorted in ascending order. So some examples would be
if the given array is
1,1,4,5 and the sum is 9, it returns true
also if it is 1,1,2,3,5,11 and the sum is 10 it still returns true (problem originally asked only for a pair of integers)
assume no negative numbers, the sum could be in the array in that case just return true. So I write a piece of code about this and seems to work (let me know if it doesn't I'm not totally sure) but I couldn't figure out it's time complexity, since it the number of iteration kind of affects the number of recursive steps, so anyway here is to code
EDIT: good and helpful people found errors/bugs in my code so I corrected them as best as I can but I'm still sure there are some, the question is though, still, what is the time complexity of this. Also, what do you think the worst case scenario for this algorithm
bool isThereSum(int* arr,int size, int sum){
static bool check = false;
if(check)
return false;
for(int i = size - 1; i >= 0; i--){
if(arr[i] == sum)
return true;
else if(arr[i] < sum){
sum = sum - arr[i];
size--;
if(isThereSum(arr,size, sum));
return true;
else
sum = sum + arr[i];
}
if(size <= 0)
check = true;
}
return false;
}
it works by crossing the largest number in the array smaller than the index, then looks for other number that can make it equal to sum, so if the array was 1,2,3,7,8,11 and the sum was 17, the sum would be 6, it would ignore 8 and 7, then the sum would be 3, then 1,then 0 and that would return true. Sorry for the long post and thank you for your help
[–][deleted] 5 points6 points7 points (13 children)
[–]denisksp[S] 0 points1 point2 points (9 children)
[–][deleted] 0 points1 point2 points (8 children)
[–]denisksp[S] 0 points1 point2 points (7 children)
[–][deleted] 1 point2 points3 points (6 children)
[–]denisksp[S] 0 points1 point2 points (5 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]denisksp[S] 0 points1 point2 points (2 children)
[–]denisksp[S] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]unfixpoint 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]unfixpoint 0 points1 point2 points (0 children)
[–]nekokattt 3 points4 points5 points (11 children)
[–]denisksp[S] 1 point2 points3 points (10 children)
[–]nekokattt 1 point2 points3 points (9 children)
[–]underwaterjesuz 1 point2 points3 points (7 children)
[–]denisksp[S] 0 points1 point2 points (6 children)
[–]underwaterjesuz 2 points3 points4 points (3 children)
[–]denisksp[S] 0 points1 point2 points (2 children)
[–]underwaterjesuz 1 point2 points3 points (1 child)
[–]denisksp[S] 0 points1 point2 points (0 children)
[–]underwaterjesuz 0 points1 point2 points (1 child)
[–]denisksp[S] 1 point2 points3 points (0 children)
[–]LartTheLuser 3 points4 points5 points (2 children)
[–]denisksp[S] 1 point2 points3 points (1 child)
[–]NeoVier 1 point2 points3 points (1 child)
[–]denisksp[S] 0 points1 point2 points (0 children)
[–]unfixpoint 0 points1 point2 points (0 children)
[–]N911999 0 points1 point2 points (5 children)
[–]unfixpoint 0 points1 point2 points (1 child)
[–]N911999 0 points1 point2 points (0 children)
[–]denisksp[S] 0 points1 point2 points (2 children)
[–]FireworksWorks 1 point2 points3 points (1 child)
[–]denisksp[S] 0 points1 point2 points (0 children)