💡 Day 48 of LeetCode Problem Solved! 🔧 🌟128. Longest Consecutive Sequence🌟 🔗 Solution Code: https://lnkd.in/gyN5ZJBr Task: Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. You must write an algorithm that runs in O(n) time. Example 1: Input: nums = [100,4,200,1,3,2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. Example 2: Input: nums = [0,3,7,2,5,8,4,6,0,1] Output: 9 Example 3: Input: nums = [1,0,1,2] Output: 3 #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfChallenge #CodingJourney #KeepGrowing
LeetCode 128 Longest Consecutive Sequence Problem Solution
More Relevant Posts
-
𝐃𝐚𝐲 87/100 – 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞 🚀 Problem: 228. 𝐒𝐮𝐦𝐦𝐚𝐫𝐲 𝐑𝐚𝐧𝐠𝐞𝐬 Today I solved a problem where we need to summarize consecutive numbers in a sorted unique array into ranges. 🔑 𝐈𝐝𝐞𝐚: Traverse the array and keep extending the range while consecutive numbers continue. Once the sequence breaks, close the range and store it. 💡 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Start with the first element as start Move forward while nums[i] + 1 == nums[i+1] If range exists → "start->end" Else → single number "start" 𝐊𝐞𝐲 𝐈𝐧𝐬𝐢𝐠𝐡𝐭: Efficient single pass solution (O(n)) by grouping consecutive elements on the fly. #LeetCode #Java #ProblemSolving #DSA #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
Day 66 — LeetCode Progress (Java) Problem: Find the Difference of Two Arrays Required: Given two integer arrays, return: Elements present in nums1 but not in nums2 Elements present in nums2 but not in nums1 Idea: Use sets to remove duplicates and quickly check membership. Approach: Convert both arrays into sets Iterate over nums1 set: Add elements not present in nums2 set to result1 Iterate over nums2 set: Add elements not present in nums1 set to result2 Return both lists Time Complexity: O(n + m) Space Complexity: O(n + m) #LeetCode #DSA #Java #HashSet #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 58 of #100DaysOfCode Solved 165. Compare Version Numbers on LeetCode 🔗 🧠 Key Insight: Version strings are split by "." into multiple revisions. We compare each revision numerically (not lexicographically). Example: "1.01" = "1.1" (leading zeros don’t matter) ⚙️ Approach (Split + Compare): 1️⃣ Split both versions using "." 🔹 version1 → s1[] 🔹 version2 → s2[] 2️⃣ Traverse till max length of both arrays 3️⃣ For each index i: 🔹 num1 = i < s1.length ? parseInt(s1[i]) : 0 🔹 num2 = i < s2.length ? parseInt(s2[i]) : 0 4️⃣ Compare: 🔹 if num1 < num2 → return -1 🔹 if num1 > num2 → return 1 5️⃣ If all equal → return 0 ⏱️ Time Complexity: O(n + m) 📦 Space Complexity: O(n + m) (for split arrays) #100DaysOfCode #LeetCode #DSA #Strings #TwoPointers #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
💡 Day 42 of LeetCode Problem Solved! 🔧 🌟1876. Substrings of Size Three with Distinct Characters🌟 Task : • A string is good if there are no repeated characters. • Given a string s, return the number of good substrings of length three in s. • Note that if there are multiple occurrences of the same substring, every occurrence should be counted. • A substring is a contiguous sequence of characters in a string. Example 1: Input: s = "xyzzaz" Output: 1 Explanation: There are 4 substrings of size 3: "xyz", "yzz", "zza", and "zaz". The only good substring of length 3 is "xyz". Example 2: Input: s = "aababcabc" Output: 4 Explanation: There are 7 substrings of size 3: "aab", "aba", "bab", "abc", "bca", "cab", and "abc". The good substrings are "abc", "bca", "cab", and "abc". #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfChallenge #CodingJourney #KeepGrowing
To view or add a comment, sign in
-
-
🚀 Day 67 of #LeetCode Challenge ✅ Problem Solved: Check If Two String Arrays are Equivalent 💡 What I learned today: • Learned how to compare two string arrays without joining them • Understood how to traverse multiple strings using pointers • Improved handling of indices across arrays and strings • Realized the importance of edge cases to avoid runtime errors 🧠 Approach: • Used four pointers to track positions in both arrays and strings • Compared characters one by one • Moved to the next string when current string ends • Ensured both arrays are fully traversed at the end 📊 Key Takeaway: Efficient solutions avoid extra space — comparing character by character is better than building new strings 🔥 Consistency + small improvements every day = big progress #Day67 #LeetCode #CodingJourney #DSA #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 91/100 – 𝐌𝐚𝐱 𝐂𝐨𝐧𝐬𝐞𝐜𝐮𝐭𝐢𝐯𝐞 𝐎𝐧𝐞𝐬 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: Keeping track of a running count helps efficiently solve problems involving continuous sequences. 💡 𝐂𝐨𝐫𝐞 𝐈𝐝𝐞𝐚: Traverse the array Count consecutive 1s Reset count when 0 appears Track maximum count 𝐖𝐡𝐲 𝐢𝐭 𝐰𝐨𝐫𝐤𝐬? We only care about continuous 1s, so resetting on 0 ensures we start counting a new sequence. ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Initialize count = 0 and max = 0 Loop through array: If 1 → increment count Else → reset count Update max at each step ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #Day91 #100DaysOfCode #Java #DSA #LeetCode #Arrays #CodingJourney
To view or add a comment, sign in
-
-
Day 63 - Odd Even Linked List Today’s problem was about rearranging nodes based on their positions. Approach: • Maintain two pointers → odd and even • Traverse and rearrange links in-place • Keep track of even head to attach later • Merge odd list with even list at the end Key insight: 👉 Focus on node positions, not node values. Time Complexity: O(n) Space Complexity: O(1) #Day63 #LeetCode #Java #LinkedList #CodingPractice #TechJourney #DSA
To view or add a comment, sign in
-
-
🚀 Day 35 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Valid Anagram Problem Insight: Given two strings, check if one is an anagram of the other. Approach: • First, check if the strings have the same length; if not, return false • Convert both strings to character arrays • Sort both arrays • Compare the sorted arrays — if equal, the strings are anagrams Time Complexity: • O(n log n) — due to sorting the arrays Space Complexity: • O(n) — for the character arrays Key Learnings: • Sorting is a simple and effective way to compare character compositions • Edge cases like different lengths should be handled first • Breaking the problem into small steps makes it easy to reason about Takeaway: Sometimes, sorting can reduce a seemingly complex problem into a simple comparison. #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #Strings
To view or add a comment, sign in
-
-
💡 Day 41 of LeetCode Problem Solved! 🔧 🌟28. Find the Index of the First Occurrence in a String🌟 Task : • Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "sadbutsad", needle = "sad" Output: 0 Explanation: "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0. Example 2: Input: haystack = "leetcode", needle = "leeto" Output: -1 Explanation: "leeto" did not occur in "leetcode", so we return -1. #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfChallenge #CodingJourney #KeepGrowing
To view or add a comment, sign in
-
-
Day 59 - Merge Two Sorted Lists Worked on merging two sorted linked lists into one sorted list. Approach: • Used a dummy node to simplify pointer handling • Compared nodes from both lists and attached the smaller one • Appended remaining elements after traversal A classic linked list problem that strengthens pointer manipulation skills. Time Complexity: O(n + m) Space Complexity: O(1) #Day59 #LeetCode #Java #LinkedList #CodingPractice #DSA #TechJourney
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