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
Find Pivot Index in Array with Prefix Sum Logic
More Relevant Posts
-
📌 Median of Two Sorted Arrays Platform: LeetCode #4 Difficulty: Hard ⚙️ Approach • Apply binary search on the smaller array for efficiency • Define search space from 0 to n1 • Partition both arrays such that: – Left half contains (n1 + n2 + 1) / 2 elements – Right half contains remaining elements • Identify boundary elements: – l1, l2 → left side elements – r1, r2 → right side elements • Check valid partition: – l1 <= r2 and l2 <= r1 • If valid: – For even length → median = average of max(left) and min(right) – For odd length → median = max(left) • If not valid: – If l1 > r2, move left – Else move right 🧠 Logic Used • Binary Search on partition index • Dividing arrays into balanced halves • Handling edge cases using boundary values • Achieving optimal O(log(min(n1, n2))) time complexity 🔗 GitHub: https://lnkd.in/g_3x55n8 ✅ Day 36 Completed – Revised advanced binary search and partition-based problem. #100DaysOfCode #Java #DSA #ProblemSolving #CodingJourney #Algorithms #DataStructures #LeetCode #CodingPractice #CodeEveryDay #BinarySearch #ArrayProblems
To view or add a comment, sign in
-
-
Today I solved LeetCode 958 – Check Completeness of a Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, determine if it is a complete binary tree. A complete binary tree is defined as: All levels are completely filled except possibly the last level In the last level, nodes are as far left as possible 💡 Key Concepts Used: Binary Trees Breadth First Search (BFS) Queue data structure Tree properties 🧠 Approach: Use BFS (level order traversal) with a queue. Traverse nodes level by level. Once a null node is encountered: All following nodes must also be null. If a non-null node appears after a null, the tree is not complete. Otherwise, the tree satisfies completeness. ⏱ Time Complexity: O(N) — Each node is visited once. 📚 What I Learned: Understanding the properties of a complete binary tree. Using BFS to validate structural constraints. Handling null conditions carefully during traversal. Strengthening queue-based tree problem-solving. Building strong fundamentals in tree structures Consistency is key to mastering DSA #LeetCode #DSA #BinaryTree #CompleteBinaryTree #BFS #Queue #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
Day 48 of Daily DSA 🚀 Solved LeetCode 48: Rotate Image ✅ Problem: Given an n x n matrix, rotate the image by 90° clockwise — in-place (without using extra space). Approach: Used a two-step transformation: Transpose the matrix Reverse each row Steps: Traverse upper triangle and swap → matrix[i][j] ↔ matrix[j][i] For each row: Use two pointers (left, right) Swap elements to reverse the row Matrix gets rotated in-place ⏱ Complexity: • Time: O(n²) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.56 MB In-place transformations are powerful — no extra space, just smart manipulation 💡 #DSA #LeetCode #Java #Matrix #Arrays #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Continued again after my Mid Sem Test — getting back to problem solving LeetCode 416: Partition Equal Subset Sum Approach (1D DP – Subset Sum Optimization): 1. Compute total sum of the array. 2. If the sum is odd → cannot partition into equal subsets. 3. Convert the problem to finding a subset with sum = total / 2. 4. Use a 1D DP array where dp[j] indicates whether sum j is achievable. 5. Initialize dp[0] = true (empty subset always possible). 6. Iterate through elements and update DP backwards to avoid reusing the same element. Efficiently builds all possible subset sums up to the target. Time Complexity: O(n × target) Space Complexity: O(target) A classic DP problem showing how optimization turns exponential recursion into an efficient solution. #DSA #DynamicProgramming #LeetCode #CodingJourney #Java
To view or add a comment, sign in
-
-
Day 52/100 🚀 | #100DaysOfDSA Solved LeetCode 69 – Sqrt(x) today. The task was to compute the integer square root of a number without using built-in functions like sqrt(). Approach: • Applied Binary Search on the range [0, x]. • Calculated mid and checked if mid * mid == x. • If mid * mid < x, stored mid as a possible answer and searched in the right half. • Otherwise, moved to the left half. • Used (long) while multiplying to avoid integer overflow. Time Complexity: O(log n) Space Complexity: O(1) Key takeaway: Binary Search is not limited to sorted arrays — it can be applied to search spaces defined by mathematical conditions. Steady progress, one problem at a time. 💪 #100DaysOfDSA #LeetCode #DSA #Java #BinarySearch #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 75/100 🚀 | #100DaysOfDSA Solved LeetCode 378 – Kth Smallest Element in a Sorted Matrix today. The problem was to find the k-th smallest element in an n x n matrix where each row and column is sorted. Approach: Used Binary Search on the value space (not indices). • Defined the search range from the smallest element (matrix[0][0]) to the largest (matrix[n-1][n-1]) • For each mid, counted how many elements in the matrix are ≤ mid • If count < k → move right (left = mid + 1) • Else → move left (right = mid) For counting, used an efficient bottom-left traversal: • Started from bottom-left corner • If current element ≤ mid → all elements above are valid → add count and move right • Else → move up This avoids checking all elements and keeps it efficient. Time Complexity: O(n log(max - min)) Space Complexity: O(1) Key takeaway: Binary Search isn’t just for arrays — it can be applied on the answer space, especially when the matrix has sorted properties. #100DaysOfDSA #LeetCode #DSA #Java #BinarySearch #Matrix #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 32 / 90 — DSA Challenge Today’s problem was a very interesting one: Split Array Largest Sum. At first glance, it looks like a typical array partitioning problem, but the real insight is recognizing that this problem can be solved efficiently using Binary Search on the Answer. 🔹 Problem Idea Given an array of integers and a value "k", the goal is to split the array into "k" subarrays such that the largest sum among those subarrays is minimized. Example: nums = [1,2,3,4,5], k = 2 Optimal split → "[1,2,3]" and "[4,5]" Largest sum = 9 🔹 Key Insight Instead of trying every possible split, we: 1️⃣ Use Binary Search on the possible maximum subarray sum 2️⃣ Check if we can split the array into "k" parts with that limit 3️⃣ Adjust the search range accordingly This reduces the complexity from an exponential brute force approach to an efficient O(n log(sum)) solution. 🔹 Concepts Practiced Today ✔ Binary Search on Answer ✔ Greedy partition checking ✔ Optimization problems with arrays ✔ Writing clean feasibility functions ("isPossible") 🔹 Takeaway Many array problems that ask to minimize the maximum value can often be solved using Binary Search on the Answer combined with a greedy validation function. Consistency is the real algorithm here. 32 days down, 58 more to go. #Day32 #90DaysOfDSA #DSAChallenge #LeetCode #BinarySearch #Java #ProblemSolving #SoftwareEngineering #CodingJourney Vignesh Reddy Julakanti
To view or add a comment, sign in
-
-
🧠 Day 37 / 100 – DSA Practice Solved Multiply Strings on LeetCode ✖️🔢✅ 🔹 Problem: Multiply two non-negative numbers given as strings without using built-in big integer libraries. 🔹 Approach: Simulated the manual multiplication method: Multiply each digit of num1 with each digit of num2 Store results in an array Handle carry properly Build final string while skipping leading zeros 🔍 Key Insight: Using an array of size m + n helps manage positions just like pen-and-paper multiplication 🔹 Complexity: ⏱ Time → O(m × n) 📦 Space → O(m + n) 💯 Result: ✔️ All test cases passed ⚡ Runtime: 3 ms (Beats 85%) Loved implementing this classic math-based approach without using built-in shortcuts 🚀 #Day37 #100DaysOfCode #LeetCode #Java #DSA #Strings #Algorithms #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 70/100 🚀 | #100DaysOfDSA Solved LeetCode 75 – Sort Colors today. The problem was to sort an array containing only 0s, 1s, and 2s in-place without using any built-in sort. Approach: Used the Dutch National Flag Algorithm (3-pointer approach). • Maintained three pointers: left (for 0s), mid (current), and right (for 2s) • Traversed the array using mid If nums[mid] == 0 → swap with left, move both left and mid If nums[mid] == 1 → just move mid If nums[mid] == 2 → swap with right, move right only • Continued until mid > right This ensures all 0s, 1s, and 2s are placed correctly in a single pass. Time Complexity: O(n) Space Complexity: O(1) Key takeaway: Using multiple pointers to partition an array in one pass is a powerful technique for in-place sorting problems. #100DaysOfDSA #LeetCode #DSA #Java #Arrays #TwoPointers #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 15 / 85 Days of DSA Challenge Today’s problem was about applying range-based updates with a twist — stepping through the array using a custom interval (k) and applying modular multiplication. 💡 Key Learnings: Handling queries with non-contiguous updates (i += k pattern) Importance of modulo arithmetic to prevent overflow Final aggregation using bitwise XOR 🔍 Approach: For each query [l, r, k, v]: Start from index l Jump k steps each time Multiply elements by v (mod 1e9+7) Finally, compute XOR of the modified array. ⚡ This problem highlights how brute-force works but may need optimization for large constraints. Consistency > Perfection 💯 85 Days Challenge — Let’s go! 🔥 #DSA #CodingChallenge #Java #ProblemSolving #100DaysOfCode #LearningJourney
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development