🚀 Day 12 of My DSA Journey Today I solved the “Contains Duplicate” problem on LeetCode using Java. 💡 Problem Statement Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. Example: Input: [1,2,3,1] Output: true Because the value 1 appears more than once. ⚙️ Approach I Used I solved this problem using sorting. Steps: 1️⃣ First, I sorted the array using Arrays.sort() 2️⃣ Then I traversed the array once 3️⃣ Compared each element with the next element 4️⃣ If nums[i] == nums[i+1], it means a duplicate exists → return true 5️⃣ If no duplicates are found, return false 📊 Complexity Analysis Time Complexity: O(n log n) → due to sorting Space Complexity: O(1) (ignoring sorting internal space) 📚 Key Learning Even simple problems can be solved in multiple ways. Sorting helped reduce the problem to a simple adjacent comparison. In future, I’ll also explore a HashSet approach which can solve this in O(n) time. Another day of improving problem-solving skills and algorithmic thinking. On to Day 13 tomorrow. 💪 #DSA #LeetCode #100DaysOfCode #Java #ProblemSolving #CodingJourney #Algorithms #LearningInPublic #Consistency
LeetCode 'Contains Duplicate' Problem Solved with Java Sorting
More Relevant Posts
-
🚀 Day 13 of My DSA Journey Today I solved the “Power of Four” problem on LeetCode using Java. 💡 Problem Statement Given an integer n, determine whether it is a power of four. A number is a power of four if: n=4xn = 4^xn=4xfor some integer x. Example: Input: 16 → Output: true (because 42=164^2 = 1642=16) Input: 5 → Output: false ⚙️ Approach I Used I used a division-based approach. Steps: 1️⃣ First check if n is less than or equal to 0 → return false 2️⃣ While n is divisible by 4, keep dividing it by 4 3️⃣ After the loop, check if the result becomes 1 If it becomes 1, the number is a power of four. 📊 Complexity Analysis Time Complexity: O(log₄ n) because we keep dividing the number by 4. Space Complexity: O(1) ⚡ Result ✅ Accepted ⚡ Runtime: 0 ms (Beats 100%) 📚 Key Learning This problem helped reinforce number patterns and mathematical thinking, which are very useful in algorithmic problem solving. Consistency is slowly building stronger DSA fundamentals. On to Day 14 tomorrow. 💪 #DSA #LeetCode #100DaysOfCode #Java #ProblemSolving #CodingJourney #Algorithms #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 7 of Java with DSA Journey 🚀 📌 Topic: Missing Number (LeetCode 268) Quote: "Sometimes the best way to solve a coding problem is to put down the keyboard and pick up a calculator." ✨ What I learned today: 🔹 Mathematical Optimization: Instead of brute force, I used a simple formula to compute the expected sum. {Sum} = \frac{n(n+1)}{2} 🔹 Core Idea: Expected Sum − Actual Sum = Missing Number 🔹 Alternative Approach: Used XOR technique where duplicates cancel out → leaving the missing value. 🔹 Efficiency: ✔️ Time Complexity: O(n) ✔️ Space Complexity: O(1) 🧠 Problem Solved: ✔️ Missing Number 💡 Key Insight: We often jump to HashSet or Sorting, but this problem taught me to pause and think mathematically first. A simple formula reduced space complexity from O(n) → O(1) 🚀 ⚡ Interview Tip (Important!): Be careful of integer overflow in Java: n * (n + 1) can exceed int limit Use long OR switch to XOR method for safety Consistency is the real key 🔑 #DSA #LeetCode #Java #Mathematics #CodingJourney #ProblemSolving #Day7 #Algorithms #Optimization #Array #MCA #lnct #100DaysOfCode #Algorithms #SoftwareEngineering #InPlaceAlgorithms #TechLearning #JavaDeveloper
To view or add a comment, sign in
-
-
Day 4 of Java with DSA Journey 🚀 📌 Topic: Search Insert Position (LeetCode 35) 💬 Quote: "Binary Search is about more than finding a needle in a haystack; it's about knowing exactly where to put a new needle." ✨ What I Learned: 🔹 Power of the low Pointer: If the target isn’t found, low directly gives the correct insertion index. 🔹 Finding Position Even When Missing: Binary Search doesn’t just search — it tells you where the element belongs. 🔹 Efficient Gap Detection: Even for missing values, we maintain O(log n) efficiency. 🔹 Complexity: ⏱ Time: O(log n) 📦 Space: O(1) 🧠 Problem Solved: ✔️ Search Insert Position 💡 Key Insight: Binary Search helps determine the rank/position of a number in a sorted array — whether it exists or not ⚡ ⚡ Interview Insight (Post-Loop Behavior): 👉 When the loop ends (low > high): low → first index greater than target high → last index smaller than target 🎯 That’s why low = insert position 🔑 Takeaway: Binary Search is not just about finding — it's about positioning. Consistency is the real key 🔑 #DSA #LeetCode #Java #CodingJourney #BinarySearch #ProblemSolving #100DaysOfCode #Algorithms #TechLearning #Day4 #Array #MCA #lnct
To view or add a comment, sign in
-
-
🚀 DAY 69/150 — FINDING PATTERNS THROUGH SORTING! 🚀 Day 69 of my 150 Days DSA Challenge in Java and today I solved a problem that highlights the power of sorting and pattern observation 💻🧠 📌 Problem Solved: Minimum Absolute Difference 📌 LeetCode: #1200 📌 Difficulty: Easy–Medium The task is to find all pairs of elements with the minimum absolute difference in a given array. 🔹 Approach Used Instead of comparing every pair (which would take O(n²)), I used a more optimized approach: • First, sort the array • Then check only adjacent elements • Keep track of the minimum difference • Store all pairs that match this minimum difference Sorting helps reduce unnecessary comparisons and simplifies the logic. ⏱ Complexity Time Complexity: O(n log n) (due to sorting) Space Complexity: O(n) (for storing result pairs) 🧠 What I Learned • Sorting can significantly reduce the complexity of comparison-based problems • Adjacent elements in a sorted array often reveal important patterns • Avoid brute-force when a structured approach exists 💡 Key Takeaway This problem taught me that sometimes the simplest optimization is just reordering the data first. Once sorted, many complex problems become easier to solve. It also strengthened my understanding of how pattern recognition + sorting can lead to efficient solutions. ✅ Day 69 completed 🚀 81 days to go 🔗 Java Solution on GitHub: 👉 https://lnkd.in/gYHCJmkv 💡 Sorting is not just a technique — it’s a problem-solving strategy. #DSAChallenge #Java #LeetCode #Sorting #Greedy #150DaysOfCode #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 1 of my DSA Journey with CodeOjas! Today, I solved a “Array Reversal in Blocks” problem using Java. Problem: Given an array of integers and a block size, reverse the array in blocks of the given size, but keep the middle block unchanged. Example: Input: arr = [1, 2, 3, 4, 5, 6, 7], blockSize = 3 Output: [3, 2, 1, 4, 5, 6, 7] Approach: Divided the array into blocks of the given size Reversed all blocks except the middle block Constructed a new array to store the modified result (brute-force approach) Complexity: Time Complexity: O(n) Space Complexity: O(n) This is a brute-force approach — simple, clear, and easy to understand for beginners. 💡 Takeaway: Breaking down a problem into blocks can help solve complex array manipulation problems easily. 📂 Code is available on my GitHub: [https://lnkd.in/g4yxAJs3] #DSA #Java #CodingJourney #CodeOjas #ProblemSolving #LinkedInLearning
To view or add a comment, sign in
-
🚀 Day 7/100 – DSA Journey Today’s focus was on implementing a classic and important problem in number theory — Armstrong Numbers — using Java. 🔍 What I built: A program to find all Armstrong numbers within a given range (100 to 2000). 💡 Approach Breakdown: Iterated through each number in the range Counted the number of digits Extracted each digit and computed its power using Math.pow() Summed the results and compared with the original number Printed the number if it satisfies the Armstrong condition ⚙️ Key Concepts Used: Loops (while, for) Mathematical logic (digit extraction, power calculation) Function design for reusability Clean iteration over a range 📌 Output Observed: 153, 370, 371, 407, 1634 📈 Learning Insight: This problem strengthened my understanding of number manipulation and algorithmic thinking, which are fundamental in DSA and frequently asked in interviews. 🔥 Consistency is the key — building problem-solving skills step by step! #Day7 #100DaysOfCode #DSA #Java #ProblemSolving #CodingJourney #SoftwareDevelopment #LearnInPublic 🙌 Tagging: 10000 Coders Pathulothu Navinder
To view or add a comment, sign in
-
-
🚀 DSA Learning Journey | Day 13 | Java Solved “20. Valid Parentheses.” 💡 Key Idea: Used a Stack to track opening brackets and match them with corresponding closing brackets. ⚙ Implementation • Language: Java • Time Complexity: O(n) • Space Complexity: O(n) 📚 Learning: How stack data structure helps in solving matching and validation problems efficiently. #JavaDeveloper #DSA #LeetCode #ProblemSolving #Stack #Algorithms #CodingJourney #100DaysOfCode #DevelopersOfLinkedIn
To view or add a comment, sign in
-
-
🚀 Day 22 of My DSA Journey Today I solved an important Binary Search problem: 👉 Find First and Last Position of Element in Sorted Array 🧩 Problem Understanding Given a sorted array and a target value, we need to find the starting and ending position of the target element. If the target is not present, return [-1, -1]. 🔍 Approach I Used (Binary Search x2) Instead of linear search, I used Binary Search twice: ✔️ First Binary Search → to find the first occurrence ✔️ Second Binary Search → to find the last occurrence 💡 Trick: Even after finding the target, we don’t stop — we continue searching on left/right side to get the boundary indices. ⚡ Why this approach? Because the array is sorted → Binary Search reduces time complexity drastically. ⏱ Time Complexity: O(log n) 📦 Space Complexity: O(1) 🔥 Key Learning Binary Search is not just for finding elements — it can also be used to find boundaries, ranges, and conditions. Consistency is the real game changer 💪 One problem at a time! #DSA #BinarySearch #LeetCode #Java #ProblemSolving #100DaysOfCode #CodingJourney #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 90 of Learning DSA with Java Today, Saksham Sir explained an important tree problem from LeetCode — Lowest Common Ancestor (LCA). 💡 Key Understanding: The goal of this problem is to find the first common intersection node where both nodes p and q meet in a binary tree. 🧠 Approach I Learned (Recursion): First, handle base cases: If root == null → return null If root == p or root == q → return root Then recursively search: Call left subtree → lowestCommonAncestor(root.left, p, q) Call right subtree → lowestCommonAncestor(root.right, p, q) Store results in variables: left and right 🔍 Final Logic: If both left and right are not null → return root (this is the LCA) If only left is not null → return left If only right is not null → return right ✨ This problem really helped me understand how recursion works deeply in tree structures and how to handle multiple conditions effectively. 📌 Consistency is key — small steps every day! #DSA #Java #LeetCode #BinaryTree #Recursion #CodingJourney #PlacementPreparation
To view or add a comment, sign in
-
Explore related topics
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