🔥 𝗗𝗮𝘆 𝟴𝟲/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟳𝟮𝟰. 𝗙𝗶𝗻𝗱 𝗣𝗶𝘃𝗼𝘁 𝗜𝗻𝗱𝗲𝘅 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Deceptively simple — and a perfect introduction to the prefix sum pattern. 𝗧𝗵𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺: Find the index where the sum of all elements to the left equals the sum of all elements to the right. 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗣𝗿𝗲𝗳𝗶𝘅 𝗦𝘂𝗺: ✅ Compute the total sum in one pass ✅ Track leftTotal as we iterate ✅ rightTotal = total - leftTotal - nums[i] ✅ If leftTotal == rightTotal → pivot found! No extra arrays. No nested loops. Just one pre-computation and one clean scan. 𝗧𝗵𝗲 𝗸𝗲𝘆 𝗶𝗻𝘀𝗶𝗴𝗵𝘁: Instead of recalculating both sides at every index, derive the right sum from what you already know — total and left. That drops it from O(n²) to O(n) instantly. 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(n) — two passes 📦 Space: O(1) This pattern — precompute total, derive the other side on the fly — shows up everywhere: product arrays, equilibrium points, range queries. A must-know! 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/g_E5Ahfe 14 more days. The finish line is in sight! 💪 #LeetCode #Day86of100 #100DaysOfCode #Java #DSA #PrefixSum #Arrays #CodingChallenge #Programming

To view or add a comment, sign in

Explore content categories