Day 16: Solving “Find Pivot Index” – LeetCode (Java) Today, I explored an interesting problem called “Find Pivot Index” from LeetCode. It really helped me understand how to balance sums on both sides of an array element. Problem Statement: Given an array, find the index where the sum of all elements on the left is equal to the sum of all elements on the right. If no such index exists, return -1. Key Logic: For each element: Calculate leftSum = sum of elements before the index. Calculate rightSum = sum of elements after the index. Compare both. If they match → that’s your pivot index! 🔍 Example: Input: [1, 7, 3, 6, 5, 6] Output: 3 Explanation: Left sum = 11, Right sum = 11 Learning Moment: Concepts Used: Nested loops Prefix sums Edge case handling Logical debugging Next Step: I’ll now optimize this logic to a more efficient approach (using total sum & prefix sum) to make it run in O(n) time. #Java #LeetCode #DSA #ProblemSolving #LearningJourney #90DaysOfCode #BCA #CareerGrowth
Solving "Find Pivot Index" problem on LeetCode with Java
More Relevant Posts
-
🔹 Day 38 – LeetCode Practice Problem: Missing Number (LeetCode #268) 📌 Problem Statement: You are given an array nums containing n distinct numbers taken from the range [0, n]. Find the one number that is missing from the array. ✅ My Approach (Java): Calculated the expected sum using the formula total = \frac{n \times (n + 1)}{2} The missing number = total - actual sum. This method avoids sorting or extra space, keeping it optimal. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Results: Accepted ✅ Runtime: 0 ms (Beats 100%) 🚀 Memory: 45.51 MB (Beats 32.44%) 💡 Reflection: A great reminder that sometimes the most efficient solutions come from simple mathematical reasoning. Clean, elegant, and lightning-fast! #LeetCode #ProblemSolving #Java #DSA #CodingPractice #Learning
To view or add a comment, sign in
-
-
🔥 #100DaysOfDSA — Day 30/100 Topic: Reverse an Array in Java 🔁 💡 What I Did Today: Today, I explored how to reverse an array manually using the two-pointer approach. Instead of relying on built-in methods, I learned how to swap elements from both ends until the array is completely reversed. 🧠 Logic Used: Initialize two pointers: start = 0 (beginning of array) end = numbers.length - 1 (end of array) While start < end: Swap numbers[start] and numbers[end] Move pointers inward → start++ and end-- 📊 Example: Input → {2, 4, 6, 8, 10} Output → {10, 8, 6, 4, 2} ⚙️ Time Complexity: O(n) — each element is swapped once. O(1) space — done in-place without using extra arrays. ✨ Takeaway: Learning to reverse an array manually helps strengthen the concept of pointers, loops, and in-place operations. Sometimes the simplest logic gives the biggest "aha!" moment 💡 #100DaysOfCode #Day30 #Java #DSA #Arrays #ProblemSolving #CodingJourney #LearnInPublic #DeveloperLife #CodeNewbie #LogicBuilding
To view or add a comment, sign in
-
-
Day 50 of #75DaysDSAChallenge Problem: 7. Reverse Integer Difficulty: 🟠 Medium Platform: LeetCode 🧩 Problem Statement Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes it to go outside the signed 32-bit integer range [-2³¹, 2³¹ - 1], return 0. Example: Input: x = 123 Output: 321 💡 Approach 1️⃣ Extract the last digit using x % 10. 2️⃣ Remove the last digit using x / 10. 3️⃣ Add the digit to the reversed number. 4️⃣ Before updating, check for overflow conditions. 5️⃣ Continue until all digits are processed. #LeetCode #Java #DSA #CodingChallenge #75DaysDSAChallenge #ProblemSolving #TechLearning #CodingJourney
To view or add a comment, sign in
-
-
DSA Practice – Day 68 🚀 Problem: Subsets II (LeetCode 90) ✨ Brute Force Approach 1. Generate all possible subsets using recursion (like Subsets I). 2. Use a Set or HashSet to store unique subsets and avoid duplicates. 3. Convert the set back to a list before returning the result. Time Complexity: O(2ⁿ × n) (to generate all subsets) Space Complexity: O(2ⁿ × n) (storing all subsets) ✨ Optimal Approach (Backtracking with Duplicate Handling) We use backtracking, but with a twist — handle duplicates smartly. Steps: 1. Sort the array so duplicates come together. 2. Use a loop with a check if(i != ind && nums[i] == nums[i - 1]) continue; to skip duplicates. 3. Recursively generate subsets and backtrack after each recursive call. Time Complexity: O(2ⁿ × n) Space Complexity: O(n) 🌟 Key Idea Sorting + Skipping duplicates ensures we only generate unique subsets efficiently. #Day68 #Backtracking #DSA #Java #LeetCode #Placements
To view or add a comment, sign in
-
-
🚀Day40/100 - Problem of the day:- Maximum Number of Operations to Move Ones to the End. 🎯 Goal: Solved a LeetCode problem to find the maximum number of operations possible from a binary string using a simple one-pass logic in Java. 💡 Core Idea: The main idea is to iterate through the string once, keep a running count of '1's, and increment the result whenever the current character '0' is smaller than the previous one '1'. This approach ensures efficient comparison and counting without extra space. 📘 Key Takeaway: Sometimes, the most efficient solutions come from observing pattern transitions (like 1 → 0) rather than using complex data structures. Optimizing logic within a single loop can significantly improve runtime performance. 🧠 Time Complexity: O(n) — only one traversal of the string. 💾 Space Complexity: O(1) — uses only a few integer variables. #100DaysChallenge #Java #DSA #Day40 #ProblemSolving #CodingJourney #Leetcode
To view or add a comment, sign in
-
-
🐇🐢 Delete Middle Node of a Linked List (Optimized Way) For this, I used the Rabbit–Tortoise (Slow–Fast Pointer) method, which is much more optimized than the normal approach. The slow pointer moves one step at a time, while the fast pointer moves two steps — when the fast pointer reaches the end, the slow pointer points to the middle node, which I then deleted. 📘 Interesting Observation: In Java, when you remove a node, the garbage collector automatically frees up memory. But in C++, you need to manually delete the node using the delete keyword — otherwise, it can lead to memory leaks. That’s one of the biggest differences between languages that helps us understand how memory management works under the hood! ⏱️ Time Complexity: O(n/2) ≈ O(n) 💾 Space Complexity: O(1) ✨ Lesson learned: Every concept seems tough in the beginning, but once you dive deeper, it becomes easier and more enjoyable. Keep practicing — you’ll be amazed by your own growth! 🚀 #DSA #LinkedList #CPlusPlus #Java #MemoryManagement #ProblemSolving #SlowFastPointer #KeepLearning
To view or add a comment, sign in
-
-
🚀 Day 55 of #100DaysOfCode Challenge Problem: LeetCode #219 – Contains Duplicate II Language: Java ☕ Today I solved an interesting array problem that checks whether a duplicate element exists within a given distance k in the array. 💡 Logic: Use a HashMap to remember the last index of each number. If the same number appears again, check if the difference between indices ≤ k. If yes → return true, else keep checking. 💻 Code: import java.util.HashMap; public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) { return true; } map.put(nums[i], i); } return false; } } 🧠 Example: Input: [1,2,3,1], k = 3 Output: true ✅ (same number 1 appears within distance 3) ⚙️ Key Concepts: HashMap for quick lookup Difference check using indices Time Complexity → O(n) Space Complexity → O(n) 💬 Every day’s problem teaches me something new — today it was about using maps smartly to track elements efficiently. On to the next challenge! 💪 #Day55 #LeetCode #Java #100DaysOfCode #CodingJourney #ProblemSolving #HashMap #DSA
To view or add a comment, sign in
-
-
🚀 Day 55 of #100DaysOfCode Challenge Problem: LeetCode #219 – Contains Duplicate II Language: Java ☕ Today I solved an interesting array problem that checks whether a duplicate element exists within a given distance k in the array. 💡 Logic: Use a HashMap to remember the last index of each number. If the same number appears again, check if the difference between indices ≤ k. If yes → return true, else keep checking. 💻 Code: import java.util.HashMap; public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) { return true; } map.put(nums[i], i); } return false; } } 🧠 Example: Input: [1,2,3,1], k = 3 Output: true ✅ (same number 1 appears within distance 3) ⚙️ Key Concepts: HashMap for quick lookup Difference check using indices Time Complexity → O(n) Space Complexity → O(n) 💬 Every day’s problem teaches me something new — today it was about using maps smartly to track elements efficiently. On to the next challenge! 💪 #Day55 #LeetCode #Java #100DaysOfCode #CodingJourney #ProblemSolving #HashMap #DSA
To view or add a comment, sign in
-
-
🚀 Day 55 of #100DaysOfCode Challenge Problem: LeetCode #219 – Contains Duplicate II Language: Java ☕ Today I solved an interesting array problem that checks whether a duplicate element exists within a given distance k in the array. 💡 Logic: Use a HashMap to remember the last index of each number. If the same number appears again, check if the difference between indices ≤ k. If yes → return true, else keep checking. 💻 Code: import java.util.HashMap; public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i]) && i - map.get(nums[i]) <= k) { return true; } map.put(nums[i], i); } return false; } } 🧠 Example: Input: [1,2,3,1], k = 3 Output: true ✅ (same number 1 appears within distance 3) ⚙️ Key Concepts: HashMap for quick lookup Difference check using indices Time Complexity → O(n) Space Complexity → O(n) 💬 Every day’s problem teaches me something new — today it was about using maps smartly to track elements efficiently. On to the next challenge! 💪 #Day55 #LeetCode #Java #100DaysOfCode #CodingJourney #ProblemSolving #HashMap #DSA
To view or add a comment, sign in
-
-
🚀Day 99/100 #100DaysOfLeetCode 🔍Problem: Sum of Two Integers✅ 💻Language: Java 💡Approach: Instead of using the ‘+’ or ‘–’ operators, this problem leverages bit manipulation to perform addition. 🔸Use XOR (^) to calculate the sum without carry. 🔸Use AND (&) followed by a left shift (<< 1) to calculate the carry. 🔸Repeat until no carry remains. 📚Key Takeaways: 🔹Reinforced understanding of bitwise operations. 🔹Learned how addition can be simulated using logical operations. 🔹Improved understanding of low-level arithmetic computation. ⚡Performance: ⏱️Runtime: 0 ms (Beats 100.00%) 💾Memory: 40.88 MB (Beats 10.05%) #100DaysOfLeetCode #Java #BitManipulation #CodingChallenge #ProblemSolving #DSA #LeetCode #CodingJourney
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