public:
// Function to find equilibrium point in the array.
// a: input array
// n: size of array
int equilibriumPoint(long long a[], int n) {
long long p=0;
for(int i=0;i<n;i++)
{
p+=a[i];
}
long long a1[n];
long long a2[n];
a1[0]=-1;
a2[n-1]=-1;
int p1=0;
int p2=0;
for(int i=1;i<n;i++)
{
p1+=a[i-1];
a1[i]=p1;
}
for(int i=n-2;i>=0;i--)
{
p2+=a[i+1];
a2[i]=p2;
}
for(int i=0;i<n;i++)
{
if(a1[i]==a2[i])
{
return i+1;
}
}
return -1;
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