Skip to content

Instantly share code, notes, and snippets.

@ritik-agrawal
Created September 16, 2023 11:15
Show Gist options
  • Save ritik-agrawal/0ca414acac209900ac0e043f56b7ae19 to your computer and use it in GitHub Desktop.
Save ritik-agrawal/0ca414acac209900ac0e043f56b7ae19 to your computer and use it in GitHub Desktop.
Find the pivot index. Index where left sum = right sum
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;
}
}
@ritik-agrawal
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment