🔥 Day 90 of my 100 Days of Code Problem: Set Mismatch (LeetCode #645) Problem Statement (Simplified): You are given an array representing numbers from 1 to n. One number is duplicated, and another is missing. Return the numbers [duplicate, missing]. Code : class Solution { public int[] findErrorNums(int[] nums) { int n = nums.length; int[] count = new int[n + 1]; int duplicate = -1, missing = -1; // Count occurrences for (int num : nums) { count[num]++; } // Identify duplicate and missing for (int i = 1; i <= n; i++) { if (count[i] == 2) duplicate = i; else if (count[i] == 0) missing = i; } return new int[]{duplicate, missing}; } } Time Complexity: O(n) Space Complexity: O(n) #Day90 #LeetCode #100DaysOfCode #Java #Arrays #Hashing #SetMismatch
Solved Set Mismatch problem in 100 Days of Code
More Relevant Posts
-
Day 43/100 Problem Statement : Given an array nums, you can perform the following operation any number of times: Select the adjacent pair with the minimum sum in nums. If multiple such pairs exist, choose the leftmost one. Replace the pair with their sum. Return the minimum number of operations needed to make the array non-decreasing. An array is said to be non-decreasing if each element is greater than or equal to its previous element (if it exists). Input: nums = [5,2,3,1] Output: 2 Solution : https://lnkd.in/g3Z6NQxN public int minimumPairRemoval(int[] nums) { int length = nums.length - 1; int count = 0; while(length > 0) { boolean increase = true; int minSum = Integer.MAX_VALUE; int minIndex = -1; for(int i = 0; i < length; i++) { if(nums[i] > nums[i + 1]) increase = false; if(nums[i] + nums[i + 1] < minSum) { minSum = nums[i] + nums[i + 1]; minIndex = i; } } if(increase) break; nums[minIndex] = minSum; for(int i = minIndex + 1; i < length; i++) nums[i] = nums[i + 1]; length--; count++; } return count; } #100DaysDSA #100DaysOfCode #Java #Leetcode #Neetcode #Neetcode250 #TUF
To view or add a comment, sign in
-
✨ Day 58 of 100: Partition List ✨ Today’s challenge was LeetCode 86 – Partition List 🧩 Problem Statement: Given the head of a linked list and a value x, partition it such that all nodes with values less than x come before nodes with values greater than or equal to x. The relative order of nodes in each partition should remain the same. 💡 Key Takeaways: 🔹 Practiced two-pointer technique to maintain separate lists for nodes < x and ≥ x. 🔹 Improved understanding of in-place linked list manipulation. 🔹 Strengthened grasp of dummy node usage to simplify edge cases. 🧠 Approach: 1️⃣ Create two dummy lists – one for smaller nodes and one for greater/equal nodes. 2️⃣ Traverse the original list and link nodes accordingly. 3️⃣ Finally, connect the smaller list’s tail to the head of the greater list. 🔥 Learning: This problem reinforced how clean list partitioning can be done efficiently with dummy nodes and careful pointer handling — all in O(n) time and O(1) extra space! #100DaysOfCode #Day58 #LeetCode #Java #LinkedList #CodingChallenge #ProblemSolving #DSA #PartitionList
To view or add a comment, sign in
-
-
💡 Day 10 of 100 Days of LeetCode 📘 Problem: #26. Remove Duplicates from Sorted Array 💻 Difficulty: Easy 🔍 Concept: The task is to remove duplicates in-place from a sorted array so that each unique element appears only once. The function should return the count of unique elements k. ⚙️ Approach Used: Used two-pointer technique — one pointer (k) tracks the position of the last unique element, and the other (i) scans the array. Whenever a new unique element is found, it’s moved to the next position in the array. 🧠 Key Learnings: Mastered the in-place modification concept in arrays. Learned how to use two-pointer pattern for linear-time solutions. Refined logic building with minimal extra space usage (O(1) space). 💻 Code (Java): class Solution { public int removeDuplicates(int[] nums) { int k = 0; for (int i = 1; i < nums.length; i++) { if (nums[i] != nums[k]) { k++; nums[k] = nums[i]; } } return k + 1; } } 💬 Reflection: A simple-looking problem — but it’s one that trains you to think in space-efficient ways. Sometimes, mastering basics is what builds strong problem-solving intuition! #100DaysOfLeetCode #Day10 #DSA #Java #ProblemSolving #CodingChallenge #LeetCode #LearningEveryday
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
-
-
🚀 Day 9 of 100 Days of LeetCode 🧩 Problem: #242. Valid Anagram 💻 Difficulty: Easy 🔍 Concept: Check whether two strings are anagrams — i.e., they contain the same characters in the same frequency, just rearranged. ⚙️ Approach Used: Used a frequency array of size 26 to count occurrences of each character in the first string. Decreased counts while scanning the second string. If all frequencies end up zero ➜ both strings are anagrams ✅ 🧠 Key Learnings: Practiced frequency array patterns for string problems. Learned to avoid sorting-based solutions for better time complexity (O(n) instead of O(n log n)). 💻 Code (Java): class Solution { public boolean isAnagram(String s, String t) { int[] freq = new int[26]; for (char c : s.toCharArray()) freq[c - 'a']++; for (char c : t.toCharArray()) freq[c - 'a']--; for (int f : freq) if (f != 0) return false; return true; } } 💬 Reflection: Small wins count too! Even the simplest problems help in strengthening your logical foundation and pattern recognition. #100DaysOfLeetCode #Day9 #ProblemSolving #Java #LeetCode #DSA #CodingChallenge #LearningEveryday
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 73 String Manipulation Problem: Valid Anagram (LeetCode #242) Task: Given two strings s and t, return true if t is an anagram of s, otherwise false. Example: Input: s = "anagram", t = "nagaram" → Output: true Input: s = "rat", t = "car" → Output: false My Approach:- Method 1 – Sorting Converted both strings into character arrays. Sorted them and compared if equal, they’re anagrams! Time Complexity: O(n log n) Method 2 – Frequency Count (Optimized) Counted occurrences of each character in s and t. If every count matches, it’s a valid anagram. Time Complexity: O(n) | Space: O(1) String problems may look simple, but optimizing from sorting to counting makes a huge difference. Small logic shifts often lead to big performance gains! #100DaysOfCode #Java #ProblemSolving #LeetCode #CodingJourney #takeUforward #DataStructures #Algorithms #StringManipulation #GeeksForGeeks #CodeNewbie
To view or add a comment, sign in
-
-
Day 33 of #50DaysOfLeetCodeChallenge Problem: 3Sum Given an integer array, find all unique triplets that sum up to zero. 🔍 Approach: Used the Two Pointer Technique after sorting the array. For each element, I fixed one number and used two pointers (left and right) to find pairs that complement it to make zero — skipping duplicates along the way for efficiency. 🧠 Key Takeaways: Sorting simplifies duplicate handling. Two pointers help reduce time complexity from O(n³) → O(n²). Clean implementation using conditions to skip repeated numbers. ⏱️ Runtime: 31 ms (beats 56.43%) 💾 Memory: 51.76 MB #LeetCode #CodingChallenge #ProblemSolving #Java #DSA #TwoPointers #50DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 32: Added two numbers stored in reverse order! 🧮 ✅ Add Two Numbers II Stack-based approach to process digits from least significant to most Head-insertion technique to build result in correct order 3 ms runtime | Handled carry propagation elegantly The Process: Push digits to stacks to reverse access order Pop and add digits from both numbers plus carry Create new nodes and link in reverse using node.next = head Update head to maintain most significant digit first Key Insight: Stacks + head insertion = perfect for adding numbers without reversing original lists! 💡 #LinkedList #Stacks #Addition #Carry #Java #LeetCode #100DaysOfCode #Day32
To view or add a comment, sign in
-
-
✅ Day 32 of #100DaysOfCode Challenge 📘 LeetCode Problem 108: Convert Sorted Array to Binary Search Tree 🧩 Problem Statement: Given a sorted array, convert it into a height-balanced Binary Search Tree (BST). A height-balanced BST ensures both left and right subtrees of every node differ in height by at most 1. 💡 Simple Idea: Pick the middle element as the root. Elements on the left become the left subtree. Elements on the right become the right subtree. Repeat the same steps recursively. ⚙ Complexity: ⏱ Time: O(n) 💾 Space: O(log n) (recursion stack height) 🌱 Step by step, building balance in both code and logic! #Day32 #100DaysOfCode #LeetCode #Java #DSA #BinarySearchTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 32 of #100DaysOfCode Challenge 📘 LeetCode Problem 108: Convert Sorted Array to Binary Search Tree 🧩 Problem Statement: Given a sorted array, convert it into a height-balanced Binary Search Tree (BST). A height-balanced BST ensures both left and right subtrees of every node differ in height by at most 1. 💡 Simple Idea: Pick the middle element as the root. Elements on the left become the left subtree. Elements on the right become the right subtree. Repeat the same steps recursively. ⚙ Complexity: ⏱ Time: O(n) 💾 Space: O(log n) (recursion stack height) 🌱 Step by step, building balance in both code and logic! #Day32 #100DaysOfCode #LeetCode #Java #DSA #BinarySearchTree #Recursion #CodingChallenge
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