class Solution { public: int pivotIndex(vector<int>& nums) { int n=nums.size(); int ap[n], aa[n]; ap[0]=0; aa[n-1]=0; int sum=nums[0]; for(int i=1;i<n;i++) { ap[i]=sum; sum+=nums[i]; } int s=nums[n-1]; for(int i=n-2;i>=0;i--) { aa[i]=s; s+=nums[i]; } int ans=-1; for(int i=0;i<n;i++) { if(ap[i]==aa[i]) { ans=i; break; } } return ans; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter