void findSum(int[] arr, int startPos, int finPos, int reqiredSum) //
recursion procedure
{
int sum =0;
// calculating sum of current(subarray) segment of array. imo single
element is also a subarray so here we have i <= finPos in case when
startPos == finPos
for(int i = startPos, i <= finPos; i++ )
{
sum +=arr[i];
}
if (sum == reqiredSum) console.WtiteLine("start {0} fin {1} ",startPos,
finPos);
if (finPos - startPos >= 1)
{
if (startPos < arr.length) findSum(arr, startPos+1, finPos,
reqiredSum);
if (finPos >0) findSum(arr, startPos, finPos-1, reqiredSum);
}
}
void main()
{
int[] arr = new int[]{1, 3, 1, 7, -4, -11, 3, 4, 8};
reqiredSum =12;
findSum(arr, 0, arr.length -1, reqiredSum);
}
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].