🚀 Day 30 / 100 | First Missing Positive -Intuition: The smallest missing positive will always be in range 1 to n+1. Place each number at its correct index (1 at index 0, 2 at index 1 so on). First index where value ≠ index+1 gives the missing number. -Approach : O(n) Find length of array n Traverse array and place each element at correct position -> index = value − 1 Ignore negative numbers, zero, and numbers > n After placement, traverse array again If arr[i] ≠ i+1 -> return i+1 If all elements are correct -> return n+1 -Complexity: Time Complexity : O(n) Space Complexity : O(1) #100DaysOfCode #Java #DSA #Array #LeetCode
Find Missing Positive in Array in O(n) Time
More Relevant Posts
-
🚀 Day 24 of #100DaysOfCode Solved 167. Two Sum II – Input Array Is Sorted on LeetCode 🔢👉👈 🧠 Key insight: Because the array is already sorted, we can avoid hash maps and use a two-pointer approach to find the target sum efficiently. ⚙️ Approach: 🔹Initialize two pointers at the start and end of the array 🔹Calculate the sum of values at both pointers 🔹If the sum is too large → move the right pointer left 🔹If the sum is too small → move the left pointer right 🔹Stop when the target is found ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #TwoPointers #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 DSA Consistency – Day 54 Solved “Check if Binary String Has at Most One Segment of Ones” on LeetCode. The task is to determine whether a binary string contains at most one contiguous block of 1s. Example: 1100111 → ❌ False (two segments of 1s) 111000 → ✅ True (only one segment) 🔹 Approach 1: Traverse the String Idea: While traversing the string, once we encounter a 0, no 1 should appear afterward. If 1 appears again, it means a second segment of ones has started. 🔹 Approach 2: String Pattern Check A binary string with only one segment of 1s should never contain "01" followed by another 1. So, we simply check for the pattern "01". Both the approaches have same complexities: Time Complexity: O(n) Space Complexity: O(1) #DSA #LeetCode #Java #ProblemSolving #CodingJourney #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 39 of #100DaysOfCode Solved 80. Remove Duplicates from Sorted Array II on LeetCode 🔢 🧠 Key Insight: The array is already sorted, and we need to ensure that each element appears at most twice, modifying the array in-place. ⚙️ Approach: 🔹Maintain a pointer i representing the position to place the next valid element 🔹Start iterating from index 2 🔹For each element nums[j], compare it with nums[i] 🔹If they are different, place the element at nums[i + 2] and move the pointer forward This ensures that no element appears more than twice while maintaining the sorted order. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Arrays #TwoPointers #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 37 / 100 | Smallest Pair With Different Frequencies -Intuition: -This problem is based on frequency counting and greedy selection. -So the idea is to count the frequency of each number and then check pairs in increasing order. -Approach: O(n log n) -First, store the frequency of each number using HashMap. -Then, store all distinct numbers in a list. -Sort the list so we can get the smallest values first. -Now, check every pair (x, y) where x < y: If freq(x) != freq(y), return that pair immediately. -Since the list is sorted, the first valid pair will be the answer. -Complexity: Time Complexity: O(n log n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode
To view or add a comment, sign in
-
-
Day 68/100 – LeetCode Challenge ✅ Problem: #169 Majority Element Difficulty: Easy Language: Java Approach: Boyer-Moore Voting Algorithm Time Complexity: O(n) Space Complexity: O(1) Key Insight: Majority element appears more than n/2 times. Pair each occurrence of candidate with a different element to cancel out. The surviving candidate after pairing is the majority element. Solution Brief: Phase 1: Find potential candidate: Initialize ele with first element, cnt=1 For each element: if same → increment count, else decrement When count reaches 0, pick new candidate Phase 2: Verify candidate actually appears > n/2 times #LeetCode #Day68 #100DaysOfCode #Array #Java #Algorithm #CodingChallenge #ProblemSolving #MajorityElement #EasyProblem #BoyerMoore #VotingAlgorithm #DSA
To view or add a comment, sign in
-
-
Day 35 – Plus One Worked on a problem where a large integer is represented as an array of digits, and the task was to increment the number by one. Key Learnings: Traversing arrays from right to left to simulate digit addition Handling carry when a digit becomes 10 Managing edge cases where all digits are 9 by creating a new array #DSA #Java #Arrays #ProblemSolving #CodingPractice #LeetCode
To view or add a comment, sign in
-
-
Day 44 of DSA 🚀 Solved Minimum Number of Flips to Make the Binary String Alternating. 💡 Key Insight: An alternating binary string can only follow two patterns: 010101... 101010... To handle rotations efficiently: Double the string (s + s) Use a sliding window of size n Count mismatches with both patterns Track the minimum flips required. ⏱ Time: O(n) 💾 Space: O(n) Good practice for sliding window + pattern matching. #DSA #Java #LeetCode #100DaysOfCode
To view or add a comment, sign in
-
-
𝗬𝗼𝘂 𝗵𝗮𝘃𝗲 𝗯𝗲𝗲𝗻 𝘂𝘀𝗶𝗻𝗴 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 𝘄𝗿𝗼𝗻𝗴. It is not just for sorted arrays. The real definition — "Eliminate HALF the search space with each decision." That one shift in thinking unlocks 20+ LeetCode problems. Swipe to see the template, live code, and 7 problems you can now solve with one pattern. 👇 Save this. 🔖 #DSA #BinarySearch #Java #LeetCode #CodingInterview #DebugWithPurpose
To view or add a comment, sign in
-
🚀 Day 23 of #100DaysOfCode 🌱 Topic: Strings / Hashing / Sliding Window ✅ Problem Solved: LeetCode 1461 – Check If a String Contains All Binary Codes of Size K 🛠 Approach: Generated all substrings of length k using sliding window. Stored each substring inside a HashSet to track uniqueness. Compared set size with 2^k to check if all possible binary codes exist. Used bit manipulation concept (1 << k) to calculate total combinations. ⏱ Time Complexity: O(n * k) 📦 Space Complexity: O(2^k) #100DaysOfCode #Day23 #DSA #Strings #Hashing #BitManipulation #Java #LeetCode #ProblemSolving #CodingJourney #Consistency #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 20/30 – LeetCode streak Problem: Check if Binary String Has at Most One Segment of Ones A valid string can have ones only in a single continuous block, and the input has no leading zeros, so it always starts with '1'. Core idea Since 's' starts with '1', any second segment of ones must look like: '1...1 0...0 1...1' → which necessarily contains "01" at the point where ones restart. So if "01" appears anywhere, there are at least two segments of ones → return false. If "01" never appears, all ones are in a single prefix (possibly followed by zeros), so return true. This reduces to a single substring check in 'O(n)' time and 'O(1)' space. Day 20 takeaway: Instead of explicitly counting segments, spotting the forbidden pattern "01" turns the whole logic into a one-line string check that’s still fully correct and optimal. #leetcode #dsa #java #strings #consistency
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