Skip to content

Commit cac62e5

Browse files
Leetcode 724
1 parent 6b860b3 commit cac62e5

1 file changed

Lines changed: 66 additions & 68 deletions

File tree

Leetcode/Leetcode_724.py

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,93 @@
11
"""
22
📌 Pivot Index (LeetCode 724) - GitHub Ready Notes
33
4-
Author: Your Name
5-
64
--------------------------------------------------
75
🧠 Problem Statement:
86
Given an integer array nums, return the pivot index.
97
10-
The pivot index is the index where:
11-
👉 Sum of elements on the LEFT == Sum of elements on the RIGHT
8+
A pivot index is an index where:
9+
👉 Sum of all elements to the LEFT of the index
10+
equals
11+
👉 Sum of all elements to the RIGHT of the index.
1212
1313
If no such index exists, return -1.
1414
1515
--------------------------------------------------
16-
🚀 Approach: Prefix Sum Technique
16+
🚀 Approach: Prefix Sum / Running Sum
17+
18+
Instead of calculating left and right sums for every index
19+
(which would take O(n²) time), we use:
20+
21+
1. total_sum = sum(nums)
22+
2. left_sum = running sum of elements to the left
23+
24+
At each index i:
25+
26+
right_sum = total_sum - left_sum - nums[i]
27+
28+
Why?
29+
30+
total_sum = left_sum + nums[i] + right_sum
1731
18-
Instead of recalculating left and right sums every time (which is slow),
19-
we use a running sum (prefix sum).
32+
Rearranging:
2033
21-
Key Idea:
22-
- total_sum = sum of all elements
23-
- left_sum = keeps increasing as we iterate
34+
right_sum = total_sum - left_sum - nums[i]
2435
25-
At any index i:
26-
Right sum = total_sum - left_sum - nums[i]
36+
If:
2737
28-
Condition:
29-
👉 left_sum == total_sum - left_sum - nums[i]
38+
left_sum == right_sum
39+
40+
then i is the pivot index.
3041
3142
--------------------------------------------------
32-
Step-by-Step Dry Run
43+
✅ Dry Run
3344
34-
Example:
3545
nums = [1, 7, 3, 6, 5, 6]
3646
37-
Step 1:
3847
total_sum = 28
3948
left_sum = 0
4049
41-
Index 0:
50+
i = 0
4251
left = 0
4352
right = 28 - 0 - 1 = 27 ❌
4453
45-
Index 1:
54+
i = 1
4655
left = 1
4756
right = 28 - 1 - 7 = 20 ❌
4857
49-
Index 2:
58+
i = 2
5059
left = 8
5160
right = 28 - 8 - 3 = 17 ❌
5261
53-
Index 3:
62+
i = 3
5463
left = 11
5564
right = 28 - 11 - 6 = 11 ✅
5665
5766
🎯 Pivot Index = 3
5867
68+
--------------------------------------------------
69+
⏱️ Time Complexity:
70+
O(n)
71+
- Single traversal of the array
72+
73+
🧠 Space Complexity:
74+
O(1)
75+
- Only a few variables are used
76+
77+
--------------------------------------------------
78+
⚠️ Common Mistakes:
79+
❌ Updating left_sum before checking the condition
80+
❌ Returning the pivot value instead of its index
81+
❌ Forgetting to subtract nums[i] when computing right_sum
82+
❌ Skipping the last index during iteration
83+
84+
--------------------------------------------------
85+
🔥 Key Takeaways:
86+
✔ Use total sum + running left sum
87+
✔ Compute right sum in O(1)
88+
✔ Check balance before updating left_sum
89+
✔ Optimal solution runs in O(n) time
90+
5991
--------------------------------------------------
6092
💻 Implementation
6193
"""
@@ -64,59 +96,25 @@
6496

6597
class Solution:
6698
def pivotIndex(self, nums: List[int]) -> int:
99+
# Calculate total sum of the array
67100
total_sum = sum(nums)
101+
102+
# Running sum of elements to the left of current index
68103
left_sum = 0
69104

70-
# ✅ Using FOR loop (recommended)
105+
# Traverse each index and compare left and right sums
71106
for i in range(len(nums)):
72-
if left_sum == total_sum - left_sum - nums[i]:
73-
return i
74-
left_sum += nums[i]
75-
76-
return -1
77107

108+
# Right sum = total sum - left sum - current element
109+
right_sum = total_sum - left_sum - nums[i]
78110

79-
# ---------------------------------------------
80-
# ✅ Alternative: Using WHILE loop
81-
# ---------------------------------------------
82-
83-
class SolutionWhile:
84-
def pivotIndex(self, nums: List[int]) -> int:
85-
total_sum = sum(nums)
86-
left_sum = 0
87-
i = 0
88-
89-
while i < len(nums):
90-
if left_sum == total_sum - left_sum - nums[i]:
111+
# If left and right sums are equal,
112+
# current index is the pivot index
113+
if left_sum == right_sum:
91114
return i
92-
left_sum += nums[i]
93-
i += 1
94-
95-
return -1
96-
97-
98-
"""
99-
--------------------------------------------------
100-
⏱️ Time Complexity:
101-
O(n)
102-
- We traverse the array once
103-
104-
🧠 Space Complexity:
105-
O(1)
106-
- No extra space used (only variables)
107-
108-
--------------------------------------------------
109-
⚠️ Common Mistakes:
110-
❌ Removing duplicates (changes positions)
111-
❌ Returning value instead of index
112-
❌ Including current element in left sum before checking
113-
❌ Wrong loop condition (like while i > len(nums))
114-
115-
--------------------------------------------------
116-
🔥 Key Takeaways:
117-
✔ Always preserve array structure
118-
✔ Use prefix sum for efficiency
119-
✔ Think in terms of LEFT and RIGHT balance
120115

116+
# Update left sum for the next iteration
117+
left_sum += nums[i]
121118

122-
"""
119+
# No pivot index found
120+
return -1

0 commit comments

Comments
 (0)