-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPivotArray.java
39 lines (35 loc) · 980 Bytes
/
PivotArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Scanner;
/*
Name: Find Pivot Index
Source: LeetCode
Link: https://leetcode.com/problems/find-pivot-index/
Statement: The pivot index as the index where the sum of all the numbers to the left of the index is equal to the
sum of all the numbers to the right of the index.
*/
public class PivotArray {
public static void main(String [] args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int [] nums = new int [n];
int lftSum, rgtSum;
lftSum = rgtSum = 0;
int index = -1;
for(int i =0; i<n; i++)
{
nums[i]= scanner.nextInt();
rgtSum += nums[i];
}
for(int i =0; i<n; i++)
{
rgtSum-= nums[i];
if(lftSum == rgtSum)
{
index = i;
break;
}
lftSum+= nums[i];
}
System.out.println(index);
}
}