Created
September 16, 2023 11:15
-
-
Save ritik-agrawal/0ca414acac209900ac0e043f56b7ae19 to your computer and use it in GitHub Desktop.
Find the pivot index. Index where left sum = right sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int pivotIndex(int[] nums) { | |
var len = nums.length; | |
var l = new int[len]; | |
var r = new int[len]; | |
var ls = 0; | |
var rs = 0; | |
var s = 0; | |
var e = len-1; | |
for (int i = 0; i < len; i++){ | |
l[s] = ls; | |
r[e] = rs; | |
ls+= nums[s]; | |
rs+= nums[e]; | |
s++; | |
e--; | |
} | |
var ret = -1; | |
for (int i = 0; i < len; i++){ | |
if (l[i] == r[i]){ | |
ret = i; | |
break; | |
} | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Leet code
Link of the question here
Achievement
The code written beats 98% of total submissions in terms of runtime with runtime as 1 ms and also beats 99% in terms of space complexity using 42 MB.