🚀 Day 22 of #100DaysOfCode Problem: Rearrange Array Elements by Sign Today’s problem was about rearranging an array so that positive and negative numbers appear alternately, while maintaining their relative order. 🔍 Approach I used: Traverse the array once and separate positives and negatives into two arrays. Use two pointers to merge them back alternately into the original array. This keeps the logic simple and easy to understand. 💡 Key Learnings: Breaking a problem into smaller steps makes it easier to solve. Extra space can sometimes simplify implementation and improve clarity. Pointer-based merging is a powerful technique in array problems. ✅ Time & Space Complexity: Time: O(n) Space: O(n) #DSA #Java #Arrays #ProblemSolving #Consistency #LearningEveryDay #100DaysOfCode
Rearrange Array Elements by Sign in Java
More Relevant Posts
-
🚀 Day 21 of #100DaysOfCode 🧩 Problem: Find Median of Two Sorted Arrays (LeetCode #4, Hard) 💡 Concept: Given two sorted arrays, the goal is to find the median of the combined dataset after merging both arrays. 🛠 Approach (Merge + Sort): 1️⃣ Create a new array to store elements from both input arrays. 2️⃣ Copy all elements from nums1 and nums2 into the new array. 3️⃣ Sort the merged array using Bubble Sort to maintain order. 4️⃣ Determine the median: If total length is even, return the average of the two middle elements. If odd, return the middle element directly. 🔥 Performance: ✅ Accepted on LeetCode ⚡ Runtime: 52 ms 💾 Memory: 48.81 MB 📊 Complexity: ⏱ Time: O((m + n)²) — due to bubble sort 💾 Space: O(m + n) — extra array used for merging #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #Consistency #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
Day 7/100 – LeetCode Challenge 🚀 Problem: Remove Element Approach: Used two pointers Shifted non-matching elements to the front Returned the count of valid elements Time Complexity: O(n) Space Complexity: O(1) (in-place) Key takeaway: In-place array problems often reduce to index management, not extra data structures. #LeetCode #100DaysOfCode #DSA #Java #ProblemSolving #InterviewPrep #100DaysOfLeetCode
To view or add a comment, sign in
-
-
📅 Day 75 of #100DaysOfLeetCode 🧩 Problem: 3507. Minimum Pair Removal to Sort Array I 📊 Difficulty: Easy 🧠 Key Insight We are allowed to repeatedly merge the adjacent pair with the minimum sum (leftmost in case of ties). The goal is to make the array non-decreasing with the minimum number of such merges. Since each operation reduces the array size by 1, we can simulate the process directly. After every merge, we must re-check whether the array has become non-decreasing. ⚙️ Approach Convert the array into an ArrayList for easy removal and updates. While the array is not non-decreasing: Find the adjacent pair with the minimum sum (choose leftmost if multiple). Replace the pair with their sum. Increment the operation count. Stop as soon as the array becomes non-decreasing. ⏱ Complexity Time: O(n³) Space: O(n) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
Solved LeetCode 4 – Median of Two Sorted Arrays ✅ Yeah, I know. This is a Hard problem and the expected solution is O(log(min(n, m))) using binary search and partitioning. But today, I went with clarity first. I combined both arrays, sorted them, and directly computed the median. 📌 Result? ✔️ All test cases passed ✔️ Correct output ✔️ Clear logic Is it the optimal solution? ❌ Is it a valid one? ✅ And that’s the point. Sometimes the real learning is: Understanding why a brute-force solution works Then understanding why it’s not optimal And only then moving to the complex solution Hard problems aren’t solved in one jump. They’re solved in iterations. Binary search partitioning is next. But today, correctness comes first. #LeetCode #DSA #ProblemSolving #Java #CodingJourney #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 Day 147 | Frequency Counting Across Multiple Strings Today’s problem was a clean exercise in comparing character frequencies across multiple inputs. 🧩 Problem Solved: 1002. Find Common Characters 🔍 Approach: • Initialized a frequency array using the first word. • For each subsequent word, counted character frequencies and kept the minimum count for each character. • Constructed the result by adding each character as many times as its final minimum frequency. ✨ Key Insight: Using minimum frequency across all strings ensures only truly common characters are included. 📚 Topics: Hashing · Strings · Frequency Counting 💻 Platform: LeetCode #LeetCode #DSA #ProblemSolving #DailyCoding #Strings #Hashing #Java #Consistency
To view or add a comment, sign in
-
-
🔹 Day 97 – LeetCode Practice 📌 Problem: Divide Array Into Equal Pairs (LeetCode #2206) 📊 Difficulty: Easy 🧠 Problem Overview: You’re given an integer array containing 2n elements. The goal is to check whether the array can be divided into n pairs such that: Every element is used exactly once Both elements in each pair are equal ✅ Approach Used: Sorted the array to bring identical elements together. Traversed the array while counting occurrences of each number. Verified that every number appears an even number of times, ensuring valid pairs. 📈 Submission Results: Status: Accepted ✅ Runtime: 8 ms Memory Usage: 46.94 MB 💡 Reflection: This problem is a great reminder that sorting can simplify pairing logic significantly. Once the array is ordered, validating pairs becomes straightforward and efficient. #LeetCode #ProblemSolving #Arrays #Java #DSA #CodingPractice #Consistency
To view or add a comment, sign in
-
-
🚀 Day 1 / 100 | Search a 2D Array. ->Today’s problem helped me understand how a 2D matrix can be treated like a 1D sorted array to apply Binary Search efficiently. •Problem Solved: 74. Search a 2D Matrix. 📚Topics: Binary Search, 2D array indexing. 🔑 Key Insight: By mapping a single index to (row, col) using division and modulo, we can apply Binary Search efficiently without extra space. #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving
To view or add a comment, sign in
-
-
🧠 Day 76 of #100DaysOfLeetCode 📌 Problem: Combination Sum II 📊 Difficulty: Medium 💡 Key Insight: Sorting the array helps handle duplicates. While backtracking, duplicates are skipped only in the “not pick” path to ensure unique combinations. 🛠️ Approach: Sort the candidates array Use backtracking to explore combinations Each number can be used once → move to index + 1 Skip duplicate elements to avoid repeated combinations ⏱️ Complexity: Time: O(2^n) Space: O(n) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 49 of #100DaysOfCode Today’s challenge was a clean and elegant Two Pointer problem — 🔢 LeetCode: Two Sum II – Input Array Is Sorted 📌 Problem Summary You’re given a sorted array of integers and a target value. Your task is to find two numbers such that their sum equals the target and return their 1-based indices. ⚠️ Constraint: The array is already sorted, which opens the door to an optimized approach. 🧠 My Approach: Two Pointers Instead of using a HashMap, I leveraged the sorted property: Start one pointer at the beginning Another at the end Calculate the sum: If sum is too small → move left pointer forward If sum is too large → move right pointer backward If equal → solution found 🎯 This avoids extra space and keeps the solution super efficient. ⚙️ Complexity Analysis ⏱ Time: O(n) 💾 Space: O(1) Minimal memory, maximum efficiency 💡 🔥 Key Learning Always look for hidden constraints (like sorted input!) Two Pointer technique can replace HashMaps in many cases Clean logic often beats complex code ✅ Solution accepted with excellent runtime & memory performance Another fundamental pattern locked in 🔐 On to Day 50 — halfway milestone loading… 🚀🔥 #100DaysOfCode #LeetCode #Java #TwoPointers #Arrays #ProblemSolving #DSA #CodingJourney
To view or add a comment, sign in
-
-
🔹 Day 91 – LeetCode Practice 📌 Problem: Three Consecutive Odds (LeetCode #1550) 📊 Difficulty: Easy 🧠 Problem Overview: Given an integer array, determine whether there are three consecutive odd numbers appearing next to each other. Return true if such a sequence exists; otherwise, return false. ✅ My Approach: Traversed the array while keeping track of consecutive odd numbers. Increased a counter whenever an odd number appeared and reset it when an even number was found. As soon as the counter reached three, the condition was satisfied. 📈 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) 🚀 Submission Results: Status: Accepted ✅ Runtime: 0 ms (Beats 100%) 🚀 Memory: 44.18 MB (Beats 38.42%) 💡 Reflection: This problem highlights how a simple counter-based logic can efficiently solve pattern-detection tasks. Clean logic and early exits make solutions both fast and readable. #LeetCode #DSA #Java #ProblemSolving #CodingPractice #LearningJourney
To view or add a comment, sign in
-
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