-
Notifications
You must be signed in to change notification settings - Fork 5
/
FindPivotIndex.java
43 lines (34 loc) · 1.33 KB
/
FindPivotIndex.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
40
41
42
43
/*
Problem Statement:
Given an array of integers nums, write a method that returns the "pivot" index of this array.
We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
Problem Link:
Find Pivot Index: https://leetcode.com/problems/find-pivot-index/solution/
Solution:
https://github.com/sunnypatel165/leetcode-again/blob/master/solutions/FindAndReplacePattern.java
Author:
Sunny Patel
https://github.com/sunnypatel165
https://www.linkedin.com/in/sunnypatel165/
*/
class Solution {
public int pivotIndex(int[] nums) {
if(nums.length==0)
return -1;
int leftSum[] = new int[nums.length];
leftSum[0] = nums[0];
int rightSum[] = new int[nums.length];
rightSum[nums.length-1] = nums[nums.length-1];
for(int i= 1;i<nums.length;i++)
leftSum[i] = leftSum[i-1]+nums[i];
for(int i= nums.length-2;i>=0;i--)
rightSum[i] = rightSum[i+1]+nums[i];
for(int i=0;i<nums.length;i++){
if(leftSum[i]==rightSum[i])
return i;
}
return -1;
}
}