🚀 Day 40 / 100 | Contains Duplicate II -Intuition: -The goal is to check whether there exist two equal elements within a distance k. -Instead of checking every pair, we only check for duplicates inside a window of size k. -If a number repeats within this window, the condition |i - j| ≤ k is satisfied, then return true. -Approach: O(n) -Use a HashSet to maintain a sliding window of size k. -Traverse through the array. -If the current element already exists in the set, return true. -Add the current element to the set. -If the window size exceeds k, remove the element that goes out of range (nums[i - k]). -If traversal completes without finding duplicates, return false. -Complexity: Time Complexity: O(n) Space Complexity: O(k) #100DaysOfCode #Java #DSA #LeetCode #SlidingWindow
Day 40: Checking Duplicate Elements in Sliding Window
More Relevant Posts
-
Day 62 — LeetCode Progress (Java) Problem: Merge Strings Alternately Required: Given two strings word1 and word2, merge them by alternating characters, starting from word1. If one string is longer, append the remaining characters at the end. Idea: Use two pointers to traverse both strings simultaneously and build the result step by step. Approach: Initialize two pointers for both strings. Iterate while either string still has characters: Pick one character from word1 (if available) Then pick one from word2 (if available) Continue alternating until both strings are fully traversed. Time Complexity: O(n + m) Space Complexity: O(n + m) #LeetCode #DSA #Java #Strings #TwoPointers #Algorithms #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
#day329 of #1001daysofcode problem statement (1784): Check if Binary String Has at Most One Segment of Ones Approach 1: If the pattern "01" appears in the string, it means the sequence of '1's was broken Approach 2: Traverse the string and count segments of consecutive '1's. Whenever a '1' is found, skip the entire sequence of continuous '1's and increase the segment count. If the number of segments of '1's is exactly one, the condition is satisfied. --both approaches have same time and space complexity. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(1) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
Day 45 — LeetCode Progress (Java) Problem: Intersection of Two Arrays Required: Given two integer arrays nums1 and nums2, return an array containing their unique intersection elements. Idea: Use a HashSet to efficiently track elements from the first array and check their presence in the second array. Approach: Create a HashSet to store all elements from nums1. Traverse nums1 and insert each element into the set. Initialize a list to store the intersection result. Iterate through nums2: If an element exists in the set: Add it to the result list. Remove it from the set to avoid duplicates. Convert the result list into an array and return it. Time Complexity: O(n + m) n = length of nums1 m = length of nums2 Space Complexity: O(n) #LeetCode #DSA #Java #HashSet #Arrays #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 37 — LeetCode Progress (Java) Problem: Sort Colors Required: Sort an array containing only 0s, 1s, and 2s in-place. Idea: Use three pointers to partition the array into three sections for 0s, 1s, and 2s. Approach: Initialize three pointers: left = 0, mid = 0, right = n - 1. Traverse while mid ≤ right. If nums[mid] == 0: swap with nums[left], move both left and mid forward. If nums[mid] == 1: move mid forward. If nums[mid] == 2: swap with nums[right] and move right backward. Time Complexity: O(n) Space Complexity: O(1) #LeetCode #DSA #Java #Arrays #TwoPointers #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 42 / 100 | Remove Duplicates from Sorted List II -Intuition: -The linked list is sorted, so duplicate values appear next to each other. -The goal is to remove all nodes that contain duplicate numbers and keep only distinct values. -To handle cases where duplicates appear at the beginning of the list, use a temp node before the head. -When a duplicate sequence is detected, we skip all nodes with that value. -Approach: O(n) -Create a temp node pointing to the head of the linked list. -Use two pointers: prev (last confirmed unique node) and curr (current node). -Traverse the linked list while checking if the current node has duplicates. -If duplicates are found, skip all nodes with that value. -Update prev.next to the next non-duplicate node. -If no duplicate is found, move both pointers forward. -Repeat the process until the end of the list. -Complexity: Time Complexity: O(n) Space Complexity: O(1) #100DaysOfCode #Java #DSA #LeetCode #linkedlist
To view or add a comment, sign in
-
-
💡 Day 37 of LeetCode Problem Solved! 🔧 🌟75. Sort Colors🌟 Task : • Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. • We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. • You must solve this problem without using the library's sort function. Example 1: Input: nums = [2,0,2,1,1,0] Output: [0,0,1,1,2,2] Example 2: Input: nums = [2,0,1] Output: [0,1,2] #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfChallenge #CodingJourney #KeepGrowing
To view or add a comment, sign in
-
-
Day 39 — LeetCode Progress (Java) Problem: Find First and Last Position of Element in Sorted Array Required: Return the starting and ending position of a target value in a sorted array. If the target is not found, return [-1, -1]. Idea: Traverse the array and track the first and last occurrence of the target while iterating. Approach: Initialize result array as [-1, -1]. Traverse the array from left to right. When the target is found for the first time, update both positions. For subsequent matches, update the ending index. Continue until the end of the array. Time Complexity: O(n) Space Complexity: O(1) Note: This approach works but is not the optimized solution for this problem. The next post will cover the optimized solution. #LeetCode #DSA #Java #Arrays #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 65 of 365 — and I just wrapped up the "Things to Know" section on Striver's A2Z Sheet. 9/9. ✅ Honestly, most of it felt like revision — Input/Output, loops, conditionals, arrays, strings. Stuff you think you know until you actually sit down and go through it properly. But the one that made me pause? 📌 Functions — Pass by Value vs Pass by Reference Pass by Value → a copy is made. The original doesn't change. Pass by Reference → you're working on the actual variable. Changes stick. Seems simple. But this is the kind of thing that causes silent bugs when you're deep in a real codebase and you can't figure out why your variable isn't updating. Solid foundations matter. Even when it feels like "I already know this." On to the next section. 💪 #365DaysOfDSA #Day65 #DSA #Java #CodingJourney #StriverA2Z #BackendDeveloper #LearningInPublic
To view or add a comment, sign in
-
-
Solved a problem where we need to check if two strings can be made equal using a special operation. The rule is: you can swap characters only if the distance between their positions is even. So basically, characters at even indices can only swap among themselves, and same for odd indices. Idea: Instead of actually swapping, I just counted characters separately for even and odd positions in both strings. If both match, then it’s possible — otherwise not. Simple concept, but interesting twist! 😊 #LeetCode #DSA #Java #ProblemSolving #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
-
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