🚀 Day47-#100DaysOfCode Today, I tackled a classic array problem: Finding the Third Maximum Number in an Array. 💡 Problem: Given an integer array, return the third distinct maximum number. If it doesn’t exist, return the maximum number. ✨ How it works: Sort the array in ascending order. Traverse backwards to count distinct numbers. Return the third distinct max if found. Otherwise, return the largest. 🔑 Key takeaway: Handling distinct elements separately ensures correctness even when duplicates exist. #Java #CodingChallenge #DataStructures #Algorithms #100DaysOfCode #LinkedInLearning
Finding Third Maximum Number in Array with Java
More Relevant Posts
-
🚀 Day 7 of #100DaysOfCode Solved Search a 2D Matrix on LeetCode using an optimized matrix traversal approach 🔎 🧠 Key insight: Since rows and columns are sorted, starting from the top-right corner helps eliminate either a row or a column in each step. ⚙️ Approach: 🔹Start at top-right element 🔹If value < target → move down 🔹If value > target → move left 🔹Repeat until target is found or bounds are crossed ⏱️ Time Complexity: O(m + n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Matrix #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
Today I learned why converting large binary strings using Integer.parseInt() is a bad idea. Solved LeetCode 1404 – Number of Steps to Reduce a Number in Binary Representation to One. Initially, I tried converting the binary string into an integer. Got NumberFormatException. Reason? Binary length > 31 bits → overflow. So instead of converting, I: Processed the string from right to left Simulated carry manually Counted steps without using BigInteger Result: Runtime: 0 ms Beat: 100% Key takeaway: Sometimes the optimal solution is not about bigger data types — It’s about avoiding conversion entirely. #LeetCode #Java #ProblemSolving #DataStructures #Binar
To view or add a comment, sign in
-
-
Day 46/200 – LeetCode Challenge Multiply Strings Today’s focus was on implementing string-based multiplication without using built-in big integer libraries. The solution involved simulating manual multiplication using an integer array for positional sums. This approach ensured efficiency and accuracy even for very large inputs. Optimizing nested loops and handling digit positions carefully can significantly improve runtime in high-precision arithmetic problems. Continuing the 200-day journey with consistent progress #LeetCode #CodingChallenge #Java #ProblemSolving #200DaysOfCode #LearningEveryday
To view or add a comment, sign in
-
-
Day 4/100 – LeetCode Challenge Problem Solved: Find First and Last Position of Element in Sorted Array (34) Today I worked on a problem that requires finding the starting and ending index of a target value in a sorted array. If the target is not present, the result should be [-1, -1]. The important constraint is that the solution must run in O(log n) time. Since the array is sorted, this naturally points to Binary Search. Instead of performing a simple search, the idea is to perform two modified binary searches: One to find the first occurrence of the target, and another to find the last occurrence. By carefully adjusting the search boundaries when the target is found, we can narrow down the exact range. This ensures: The algorithm maintains logarithmic time complexity. The solution handles duplicates correctly. Edge cases such as the target not being present are properly addressed. Time Complexity: O(log n) Space Complexity: O(1) Key takeaway: Binary search is not just about finding whether an element exists. With small boundary modifications, it can be adapted to solve range-based problems efficiently. Understanding how to tweak the search condition is more important than memorizing the template. Day 4 completed. Continuing to sharpen problem-solving fundamentals. #100DaysOfLeetCode #BinarySearch #Java #DataStructures #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 55: Root-to-Leaf Grinding 🌳 Problem 1022: Sum of Root To Leaf Binary Numbers Back to trees. The goal: sum binary paths from root to leaf. The Strategy: • DFS Brute Force: Stack-based traversal to explore every branch. • Path Tracking: Dragging binary strings down the tree like a heavy backpack. • Conversion: Hit a leaf, parse the string to base 2, add to sum. Ngl, string concatenation in a loop is a bit mid for efficiency, but it got the job done. Bitwise shortcuts are next on the menu. We move. 🚀 #LeetCode #Java #BinaryTree #DFS #Coding #DailyCode
To view or add a comment, sign in
-
-
#Day13 of #365DaysOfCode Today’s LeetCode practice: 🔹 Plus One (LeetCode 66) Worked on array manipulation and carry handling while incrementing a number represented as digits. This problem reinforced edge-case thinking, especially scenarios involving all 9’s and array resizing. #LeetCode #Java #DataStructures #Algorithms #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 74 of #100DaysOfCode Today’s challenge was a clean and efficient array problem — LeetCode: Merge Sorted Array ✅ 📌 Problem Summary You’re given two sorted arrays, where the first array has extra space at the end. The task is to merge the second array into the first in-place, keeping everything sorted. 🧠 My Approach Instead of shifting elements forward (which is costly), I used a reverse two-pointer technique: Start comparing elements from the end of both arrays Place the larger element at the last available position Move pointers accordingly until all elements are merged This avoids extra space and keeps the solution optimal. ⚙️ Complexity ⏱ Time: O(m + n) 💾 Space: O(1) (in-place merge) 🔥 Key Learning Working from the back can simplify in-place array problems Two-pointer strategies are incredibly powerful for sorted data Another day, another solid problem solved 💪 Onward to Day 75 🚀 #100DaysOfCode #LeetCode #Java #DSA #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 5/365 – Recover Binary Search Tree Today I solved an interesting Binary Tree problem: 👉 Recover a Binary Search Tree where exactly two nodes were swapped by mistake — without changing the tree structure. 🧠 Key Insight In a valid Binary Search Tree (BST), an inorder traversal produces a sorted sequence. If two nodes are swapped, this sorted order gets violated. So the strategy is: Perform an inorder traversal Detect the two nodes where the order breaks Swap their values back 🔍 What I Learned ✔️ How inorder traversal helps validate BST properties ✔️ Handling both adjacent and non-adjacent swaps ✔️ Improving problem-solving using pattern recognition ✔️ Importance of maintaining previous node reference ⏱ Complexity Time Complexity: O(n) Space Complexity: O(h) (recursion stack) Can be optimized to O(1) space using Morris Traversal Binary Tree problems always strengthen recursion clarity and tree traversal intuition 🌳 Consistency is the key 🔥 On to the next challenge! #DayXof365 #LeetCode #DataStructures #BinarySearchTree #Java #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
Day 27/100 – LeetCode Challenge 🚀 Problem: Sort List Approach: Applied Merge Sort on the linked list Used slow and fast pointers to find the middle Recursively sorted both halves Merged the sorted halves Time Complexity: O(n log n) Space Complexity: O(log n) (recursion stack) Key takeaway: Merge sort is the most efficient sorting technique for linked lists, as it avoids random-access operations required by other algorithms. #LeetCode #100DaysOfCode #DSA #Java #LinkedList #MergeSort #ProblemSolving
To view or add a comment, sign in
-
-
🔥 Day 84 of #100DaysOfCode Today’s problem: LeetCode: Find K Closest Elements 🎯 📌 Problem Summary Given: A sorted array arr An integer k A target value x Return the k closest integers to x in the array. The result must be sorted in ascending order. Example: arr = [1,2,3,4,5], k = 4, x = 3 Output → [1,2,3,4] 🧠 Approach: Two Pointers Shrinking Window Since the array is already sorted, we can: Start with: l = 0 r = arr.length - 1 While the window size is bigger than k: Compare distances: |x - arr[l]| |x - arr[r]| Remove the element that is farther from x ⚙️ Core Idea: while (r - l >= k): if left is closer: r-- else: l++ Finally, return elements from l to r. ⏱ Time Complexity: O(n - k) 💾 Space Complexity: O(1) (excluding result list) 🚀 Performance ⚡ Runtime: 4ms (98% faster) Solid optimization using the sorted property of the array. 💡 What I Learned When array is sorted → always think Two Pointers. Instead of building result, shrink unnecessary elements. Cleaner logic often gives better performance. Consistency > Motivation. On to Day 85 🔥 #100DaysOfCode #LeetCode #TwoPointers #Java #DSA #CodingJourney #InterviewPrep
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