Your solution fails for a number of reasons: 1. If your array is size 1 or 0. 2. The last digit in the array is found by arr[n-1] - [n-2] not arr[i+1]-arr[i] 3. Recursion here creates unnecessary overheard
How many times are you calling abc? Draw the recursion tree. On Tue, Apr 9, 2013 at 11:29 AM, rahul sharma <[email protected]>wrote: > A = {5, 3, 8, 9, 16} > After one iteration A = {3-5,8-3,9-8,16-9}={-2,5,1,7} > After second iteration A = {5-(-2),1-5,7-1} sum =7+(-4)+6=9 > Given an array, return sum after n iterations > > my sol/ > void abc(int arr[],n) > { > for(i=0;i<n;i++) > arr[i]=arr[i+1]-arr[i]; > abc(arr,n-1); > } > > > I wana ask that the complexity is o(n) or o(n)2......as loop is executed n > times..say n is 10...so fxn is called 10 times....i.e 10 n..and ignoring n > it comes out to be...n......but if we implemeted with 2 loops then > complexity is n2 ...and both sol are taking same no of iterations...please > tell whether complexity is n or n2 for above code....if it is n2 then how??? > > -- > 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]. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- 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]. For more options, visit https://groups.google.com/groups/opt_out.
