LeetCode Parctice: Problem no.217 (Contains Duplicate) Statement- A array is given , we have to return true if any value appears at least twice in the array, and return false if every element is distinct. Approach- Brute Force Key Idea: - I used sets data structure for this problem as set only store unique elements. -While traversing the array, I check whether the current element already exists in the set. If the element is already present → it means a duplicate exists, so return true. Otherwise, insert the element into the set and continue checking. Time Complexity = O(n) Space Complexity = O(n) #DSA #CodingPractice #Cpp #Programming #ProblemSolving
Detecting Duplicate Elements in an Array with Brute Force Approach
More Relevant Posts
-
Minimum Distance Between Three Equal Elements Thought process 🤔 Brute force: Run 3 loops, pick every (i, j, k), check if elements are equal, and calculate the absolute difference of indices. Keep updating the minimum. Simple approach, but yeah… O(n³) is too slow when input size grows 😅 How to optimize:: We only care about indices where values are the same, so instead of checking every triplet, let’s store them.. Use a map For each elem, store all its indices Then check only valid triplets from those indices, compute distances for valid groups and track the mini. O(n) time 💡 #leetcode #potd #programming #dsa #developer #softwareengineer #datastructures #algorithms
To view or add a comment, sign in
-
-
42 of #100DaysOfCode Solved LeetCode 930 – Binary Subarrays With Sum 💡 Today’s problem was a great example of how powerful Prefix Sum + HashMap can be when dealing with subarrays. 🔹 Approach Used: Instead of checking all subarrays (which would be inefficient), I used a running prefix sum and stored its frequency in a hashmap. This reduces the time complexity to O(n) — much more efficient than brute force! ⚡ 🔹 What I Learned: Prefix sum helps convert subarray problems into lookup problems HashMap optimizes repeated computations Always think: “Can I store previous results to save time?” #LeetCode #DSA #CodingJourney #Programming #Cpp #SoftwareEngineering #ProblemSolving #LearnToCode
To view or add a comment, sign in
-
-
#Day58 Solved: Longest Substring Without Repeating Characters(Leetcode 3) Worked on one of the most asked string problems that really tests problem-solving and optimization skills. Problem: Find the length of the longest substring without repeating characters. Approach: Used the sliding window technique to efficiently track a valid substring. Instead of checking all possible substrings (which is slow), I dynamically adjusted the window: Expanded when characters were unique Shrunk when a duplicate appeared Key Learning: The biggest takeaway was understanding how to maintain a valid window rather than restarting every time a duplicate is found. Complexity: Optimized the solution from O(n²) to O(n) using the right approach. This problem strengthened my understanding of: Sliding Window Two Pointer Technique Optimizing brute force solutions #leetcode #dsa #programming #coding #slidingwindow #problemSolving
To view or add a comment, sign in
-
-
When you miss a base case🧠 What is the best way to check it, what do you guys think??💬 When working with recursion, the most important step is often the simplest one: 💻Define your base case first. Before thinking about the recursive calls, pause and ask, like what’s the worst or simplest condition where the function should stop? Most of the time, that’s your base case. A small reminder from experience: 👉 Start with the base case 👉 Think about edge conditions first 👉 Then build the recursive flow Strong foundations make complex problems manageable. You can also check out✨, and learn CPP from the basics💻: https://lnkd.in/gdPC4D6f #Recursion #Programming #CPP #DSA #problemSolving #leetcode #basecase #lpu
To view or add a comment, sign in
-
-
#Day56 solved LeetCode 169 Majority Element Problem: Find the element that appears more than ⌊n/2⌋ times in an array. Approach: Instead of using extra space like HashMap, I learned a very efficient method called the Boyer-Moore Voting Algorithm. The idea is simple: 1.Keep a candidate and a count 2.Increase count if same element appears 3.Decrease count if different element appears 4.The final candidate will be the majority element Time Complexity: O(n) Space Complexity: O(1) This problem taught me how powerful optimized logic can be compared to brute force solutions. #DSA #LeetCode #Coding #ProblemSolving #100DaysOfCode #Programmer #LearningJourney
To view or add a comment, sign in
-
-
Day 60/100 of #100DaysOfCode 🔹 GFG POTD – Count Increasing Subarrays My approach: Instead of checking all subarrays, I tracked the length of every increasing streak. Example: If the array is: [1, 2, 3, 1, 2] Then: 1 → 2 → 3 is one increasing streak of length 3 1 → 2 is another streak of length 2 For every streak of length len, the number of increasing subarrays is: len * (len - 1) / 2 Why? Because from a streak of 3: [1,2] [2,3] [1,2,3] This helped solve it in O(n) time. 🔹 LeetCode POTD – Minimum Distance of Good Tuple We had to find 3 equal values in the array such that: distance = |i-j| + |j-k| + |k-i| My thought process: For any 3 indices: a < b < c The formula simplifies to: 2 * (c - a) That means: 👉 only the first and last index matter. So instead of checking all triplets: I stored indices of each number For every number appearing 3+ times: checked every 3 consecutive indices calculated minimum distance This reduced the solution from brute force to O(n). Slowly improving every day. On to Day 61 💻🔥 #LeetCode #GeeksforGeeks #CodingJourney #DSA #ProblemSolving #Cpp #Programming #100DaysOfCode
To view or add a comment, sign in
-
-
Day 45/120: Equal partition - DP enters the picture One problem today. Felt different from recent backtracking problems. Partition Equal Subset Sum Can you partition an array into two subsets with equal sum? First insight: if the total sum is odd, impossible. You can't split an odd number evenly. Second insight: if sum is even, the problem becomes "can you find a subset with sum = total/2?" This is the classic subset sum problem. Solved it with DP. Create a boolean array where dp[i] represents whether sum i is achievable. For each number, update what sums become possible. Why this felt different: Backtracking would work - try including/excluding each element, check if you reach target. But DP is cleaner here. We're not finding all solutions or one specific solution - just checking if a solution exists. This is the first problem in a while where DP clearly beats backtracking. This problem reminded me that different techniques solve different problems best. Forty-five days in. The toolkit keeps expanding. #DSA #CodingJourney #100DaysOfCode #LeetCode #GeeksForGeeks #DynamicProgramming #SubsetSum #Algorithms #SoftwareEngineering #Programming #Learning #Day45
To view or add a comment, sign in
-
-
🚀 Just solved LeetCode 3643 - Reverse Submatrix Today I worked on a really interesting matrix manipulation problem where the goal was to reverse a k×k submatrix efficiently. 💡 Key Insight: Instead of using extra space, I applied a two-pointer approach to swap rows from top to bottom — making the solution clean and optimal. ⚡ What I learned: • In-place matrix transformations • How to optimize nested loops • Importance of visualizing row/column operations 🧠 Complexity: Time → O(k²) Space → O(1) 📌 Small problems like this build strong fundamentals for bigger DSA challenges! 💻 Code + Explanation PPT available on my GitHub 👇 #LeetCode #DSA #CodingJourney #CPlusPlus #ProblemSolving #Tech #Programming #100DaysOfCode
To view or add a comment, sign in
-
LCA - Lowest Common Ancestor of binary tree Approach: If current node is null, return null If current node is p or q, return current node Recursively search left and right If both sides return non-null → current node is LCA Otherwise → return the non-null side TC = O(n) SC = O(h) - depends on tree height (recursion stack) O(logn) - balanced tree O(n) - skewed tree #dsa #leetcode #consistency #coding #programming
To view or add a comment, sign in
-
-
Stop memorizing Rust rules. Start deriving them. 🦀 This free interactive course reframes the borrow checker not as a set of rules to memorize, but as the logical outcome of three primitives: Space, Time, and Coordinates. 📐 Every memory bug is just a failure in one of these three dimensions: 🔸 Use-after-free 🔸 Dangling pointer 🔸 Data race 🔍 Understand the framework, and the compiler's behavior clicks into place. ✅ 💡 Highly recommended for experienced devs, especially those with a C/C++ background. 👇 https://lnkd.in/gAsQvjhv #Rust #RustLang #Programming #SystemsProgramming #SoftwareEngineering
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