Find Pivot Index in Array with Prefix Sum Logic

Day 31 of Daily DSA 🚀 Solved LeetCode 724: Find Pivot Index ✅ Problem: Given an array of integers, find the pivot index — where the sum of all numbers strictly to the left equals the sum of all numbers strictly to the right. Rules: * If the index is on the left edge → left sum is 0 * If the index is on the right edge → right sum is 0 * Return the leftmost pivot index, or -1 if none exists Approach: Used Prefix Sum logic to efficiently track left and right sums in a single pass. Steps: 1. Calculate the total sum of the array (rSum) 2. Iterate through the array, reducing rSum by current element 3. If rSum equals lSum → pivot index found! 4. Add current element to lSum and continue ⏱ Complexity: • Time: O(n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 1 ms (Beats 98.65%) ⚡ • Memory: 47.65 MB A clean example of how prefix sums eliminate the need for nested loops and make array problems lightning fast. #DSA #LeetCode #Java #PrefixSum #Arrays #CodingJourney #ProblemSolving

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories