solution at this link:
http://ideone.com/ifVIv 

for every position, (iteration)
maitain left, right for the sums,
keep adding elements towards the begenning to left, and towards the end to 
right, (based on the conditions in the code)

Complexity: outer forloop : O(n)
inner while loop O(n) 
total O(n^2) and O(1) space.

its currently printing all the position, & center is included to the left 
side,

Left : 3, Right: 9, Center: 6 
this reads as.. sum(elements at 3,4,5,6) == sum(elements at 7,8,9)

let me know if it needs more explanantion.




for(int i=0;i<len;i++){
                int left=0,right=0;
                int p1 = i;
                int p2 = i+1;
                left = left + a[p1];
                right = right + a[p2];
                while(p1>=0 && p2< len){
                        if( left == right){
                                printf("Left : %d, Right: %d, Center: %d 
\n",p1,p2,i);
                                break;
                                //return 0;
                        }
                        else if(left > right && p2 < len-1){
                                p2++;
                                right = right+ a[p2];
                        }
                        else if(left < right && p1 > 0){
                                p1--;
                                left = left + a[p1];
                        }
                        else{
                                //printf("Left : %d, Right: %d, Center: %d 
\n",p1,p2,i);
                                //printf("Not Possible\n");
                                break;
                                //return 0;
                        }
                }
                                        
        }


-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/GoJEA73v8dcJ.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to