🧮 Day 30 of My #100DaysOfLeetCode Challenge ✅ Problem: Find Target Indices After Sorting Array 🧩 Difficulty: Easy 📂 Category: Array / Counting ⚡ Runtime: 0 ms (Beats 100%) 💾 Memory: 44.54 MB 🔹 Approach: Instead of sorting, I used counting logic to find the number of elements smaller than the target (lessThan) and the count of target elements (count). The resulting indices are then [lessThan, lessThan + 1, ..., lessThan + count - 1]. This avoids unnecessary sorting and keeps the solution O(n). 🧠 Time Complexity: O(n) 💾 Space Complexity: O(1) ✨ Learnings: How counting-based reasoning can replace sorting for index-based problems. Focused on optimization and problem pattern recognition. 📈 Progress: Day 30 ✅ | 70 days remaining 🚀 💭 “Optimization is not about doing more — it’s about doing smarter.” #100DaysOfCode #LeetCode #Java #DSA #ProblemSolving #CodingChallenge #Consistency
Anmol Gupta’s Post
More Relevant Posts
-
🚩 Problem: 349. Intersection of Two Arrays 🔥 Day 42 of #100DaysOfLeetCode 🔍 Problem Summary: Given two integer arrays nums1 and nums2, return an array of unique elements that appear in both arrays. The result must not contain duplicates. The order of elements in the output does not matter. ✅ Approach: Using HashSet Store elements of nums1 in a HashSet. Loop through nums2, and if an element exists in HashSet, add to result set. Convert the set into an array. 📊 Complexity: Time Complexity: O(m + n) Space Complexity: O(m + n) 🎯 Key Takeaway: This problem reinforces the concept of using HashSet to remove duplicates and perform efficient membership checks, which is fundamental for solving array and hashing problems optimally. Link:[https://lnkd.in/gz9Y99Ak] #100DaysOfLeetCode #Day42 #Problem349 #IntersectionOfArrays #HashSet #Java #DSA #Algorithms #CodingChallenge #ProblemSolving #LeetCode #InterviewPreparation #DataStructures #TechCareers #ArjunInfoSolution #CodingCommunity #CodeNewbie #ZeroToHero #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 115 days of #200DaysOfCode Problem: 24. Swap Nodes in Pairs Problem Statement: Given the head of a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes, only nodes themselves may be changed. Approach: Used a dummy node and iteratively swapped each adjacent node pair via pointer manipulation, which enabled in-place node swaps without extra space. Logic: Leveraged pointer rewiring to achieve the swaps efficiently with O(1) extra space and O(n) time complexity, cleanly iterating through the list to handle both even and odd-length cases. 👉 Question link 🔗: https://lnkd.in/g6cwvMgz #LeetCode #Java #LinkedList #Pointers #DSA #Coding #Algorithms #InterviewPrep #200DaysOfCode
To view or add a comment, sign in
-
-
💻 Day 74 of #LeetCode100DaysChallenge Solved LeetCode 18: 4Sum — a challenging problem that enhances array manipulation, two-pointer technique, and handling duplicates. 🧩 Problem: Given an array nums of n integers and a target value, return all unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: All indices are distinct The sum of the four numbers equals the target Result can be returned in any order 💡 Approach — Sorting + Two Pointers: 1️⃣ Sort the array to simplify duplicate handling. 2️⃣ Use two nested loops to fix the first two numbers. 3️⃣ Apply the two-pointer technique for the remaining two numbers to find quadruplets that sum to the target. 4️⃣ Skip duplicate elements to ensure uniqueness. 5️⃣ Collect all valid quadruplets in a list. ⚙️ Complexity: Time: O(N³) — two loops + two-pointer scan Space: O(N) — for storing results ✨ Key Takeaways: ✅ Strengthened two-pointer technique for k-sum problems. ✅ Learned efficient strategies for avoiding duplicates in combinatorial results. ✅ Practiced breaking down complex problems into smaller, manageable steps. #LeetCode #100DaysOfCode #Java #TwoPointers #Arrays #4Sum #DSA #ProblemSolving #CodingJourney #WomenInTech
To view or add a comment, sign in
-
-
🗓 Day 6 / 100 – #100DaysOfLeetCode 📘 Problem: 3228. Maximum Number of Operations to Move Ones to the End Difficulty: Medium 💡 Problem Summary: Given a binary string s, you can repeatedly choose an index i where s[i] == '1' and s[i+1] == '0', and move that '1' to the right until it reaches the end of the string or hits another '1'. The goal is to find the maximum number of such operations possible. 🧠 My Approach: Instead of simulating the moves (which would be inefficient), I used a counting strategy: Keep a running count of the number of '1's seen so far (cnt). Whenever a '0' appears after one or more '1's, we can perform cnt operations involving those ones. Sum these up for the final result. 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
✅Day 44 : Leetcode 162 - Find Peak Element #60DayOfLeetcodeChallenge 🧩 Problem Statement: You are given a 0-indexed integer array nums. A peak element is an element that is strictly greater than its neighbors. Your task is to find a peak element and return its index. If the array contains multiple peaks, return the index of any one of them. You must solve this in O(log n) time complexity. 💡 My Approach: I used the Binary Search approach to solve this problem efficiently. Check for edge cases: If there’s only one element, return index 0. If the first element is greater than the second, it’s a peak → return 0. If the last element is greater than the second last, return n-1. Apply binary search between indices 1 and n-2: Find the middle index mid. If nums[mid] is greater than both its neighbors (nums[mid-1] and nums[mid+1]), we found the peak → return mid. If the left neighbor is greater, move the search to the left half. Otherwise, move to the right half. This guarantees logarithmic search efficiency. ⏱️ Time Complexity: O(log n) — due to binary search. 💾 Space Complexity: O(1) — constant extra space. #BinarySearch #LeetCode #FindPeakElement #DSA #Java #CodingPractice #ProblemSolving #LogN
To view or add a comment, sign in
-
-
Day 51 of My DSA Challenge Problem: Count all triplets (i, j, k) in an array such that arr[i] + arr[j] + arr[k] = target Example: Input → arr = [1, 2, 3, 4, 5], target = 9 Output → 2 (Triplets: [1,3,5] and [2,3,4]) My Approach: Instead of using a brute force O(N³) approach, I optimized it using HashMap + Two Loops. Logic Breakdown: Store frequency of all elements (from index 2 onwards) in a HashMap. Fix j, iterate i < j, and calculate target - (arr[i] + arr[j]). If that value exists in the HashMap → it forms valid triplets! After processing each j, reduce the count of future elements to maintain correctness. Time Complexity: O(N²) Space Complexity: O(N) #Day51 #DSAChallenge #HashMap #ProblemSolving #TripletsSum #DSA #CodingChallenge #Java #100DaysOfCode #CodeEveryday #GeeksforGeeks #LeetCode #DataStructures #Algorithms #TwoPointers #DSAwithJava #CodingCommunity #ProgrammingJourney #DeveloperMindset #TechLearning
To view or add a comment, sign in
-
-
🚀 Day 50 of My LeetCode Journey Today, I solved Reshape the Matrix. Problem in short: Given a matrix, reshape it to a new size r x c while keeping the row-traversal order intact. If it’s impossible, return the original matrix. Approach I used: First, check if total elements match (originalRows * originalCols == r * c). Use a single counter to traverse the original matrix and place elements in the new matrix. Map the counter to new matrix indices: row = count / c col = count % c Why this is clean: Simple and intuitive. No need for multiple variables for row and column. Works for any size matrix. Example: Original: 1 2 3 4 Reshaped to 1×4 → 1 2 3 4 This problem taught me how matrix manipulation can be simplified using smart indexing. #100DaysOfLeetCode #CodingJourney #ProblemSolving #DSA #Java #Matrix
To view or add a comment, sign in
-
-
📌 Day 21/100 – Sort an Array (LeetCode 912) 🔹 Problem: Given an integer array, sort it in ascending order without using built-in sorting functions and ensure the solution runs in O(n log n) time. 🔹 Approach (Merge Sort): Divide the array into two halves recursively Sort each half independently Merge the two sorted halves while maintaining order Uses Divide & Conquer and guarantees stable sorting 🔹 Key Learnings: Merge Sort ensures worst-case O(n log n) time unlike QuickSort Works well for linked lists & large datasets because of stability Uses extra space due to temporary list merging Great example of recursion + two-pointer merging technique #100DaysOfCode #Day21 #LeetCode #Java #DSA #MergeSort #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🗓 Day 3 / 100 – #100DaysOfLeetCode 📌 Problem 1252: Cells with Odd Values in a Matrix The task was to determine how many cells in a matrix have odd values after performing a series of row and column increment operations. 🧠 My Approach: Instead of updating the entire matrix (which would be inefficient), I tracked the number of increments for each row and column separately using two arrays. Then, for every cell, I checked if the sum of its corresponding row and column increments was odd — if yes, I counted it. 📈 Key Insight: No need to maintain the full matrix. Just track row and column increments — the sum determines parity. Time complexity: O(m × n) Space complexity: O(m + n) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
📌 Day 2/100 – Remove Element (LeetCode 27) 🔹 Problem: Given an integer array nums and a value val, remove all instances of that value in-place and return the new length of the array. The order of elements can be changed. 🔹 Approach: Used the two-pointer technique to efficiently modify the array in-place. One pointer iterates through the array, while the other tracks the position to overwrite non-val elements. Returned the position of the second pointer as the new length. 🔹 Key Learning: Strengthened understanding of in-place array manipulation. Improved logic building for pointer movement and conditional overwriting. Learned how to minimize extra space usage while maintaining readability and clarity. Another small yet powerful step toward mastering array-based problems! 💻 🔥 #100DaysOfCode #LeetCode #Java #ProblemSolving #TwoPointers #DSA #CodingJourney
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