The original question is
Smallest subarray with sum greater than a given value Given an array of integers and a number x, find the smallest sub array with sum greater than the given value. at http://www.geeksforgeeks.org/minimum-length-subarray-sum-greater-given-value/
but if the question was only to find the size of the smallest subarray, can we use divide and conquer method?
Algorithm: Size_of_min_subarray(1,n) =
min of { Size_of_min_subarray(1,n/2), Size_of_min_subarray(n/2,n),Size_of_array_containing mid_element(s) }
Size_of_array_containing_mid_element Is caluculated by:
Take the mid element(s) , initialise sum=mid element/sum of mid element and num_elements=1/2 based on n is odd or even .if sum<=given value,check if number to its right or left is greater.Add that greater element to the sum and increament num_elements. repeat until sum> given value.
Illustration:
1,4,45,6,0,19 and threshold=51
min_size=min{ min_size{1,4,45} , min_size{6,0,19} , min_containing_45and6 }
min_containing_45and6:
sum=mid elements/sum of mid elements=45+6=51<=51,num_elements=2. Since 4>0 add 4 to sum ,sum=55 and increament num_elements. num_elements=3.
min_size=min{ not possible , not possible , 3} =3.
Is this algorithm correct?And i think its complexity if O(logn),is that right?
Edit: i realized that the algo i am using to find Size_of_array_containin_mid_elemnts is wrong.can anyone suggest an algo for finding this value?