📅 Day 52 of #160DaysofDSA 🔹 Count Pairs with Sum Less Than Target Today’s problem was an elegant use of the Two Pointer Technique — a powerful approach for solving array problems efficiently ⚡ 🧩 Problem Statement: Given an array arr[] and an integer target, find the number of pairs whose sum is strictly less than the target. 💡 Approach (Two Pointer Technique): 1️⃣ Sort the array. 2️⃣ Use two pointers: left = 0, right = n - 1. 3️⃣ If arr[left] + arr[right] < target: ➤ All pairs between left and right will also have smaller sums. ➤ Add (right - left) to count and move left++. 4️⃣ Else, move right--. ⏱ Time Complexity: O(N log N) 💾 Space Complexity: O(1) #DSA #Java #Coding #TwoPointers #ProblemSolving #Sum #Count #Target #GFG #Day52
How to Count Pairs with Sum Less Than Target using Two Pointer Technique
More Relevant Posts
-
🚀 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 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 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
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟏𝟒 of #50DaysOfDSA 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝟏𝟔𝟏𝟏. 𝐌𝐢𝐧𝐢𝐦𝐮𝐦 𝐎𝐧𝐞 𝐁𝐢𝐭 𝐎𝐩𝐞𝐫𝐚𝐭𝐢𝐨𝐧𝐬 𝐭𝐨 𝐌𝐚𝐤𝐞 𝐈𝐧𝐭𝐞𝐠𝐞𝐫𝐬 𝐙𝐞𝐫𝐨 (𝐇𝐚𝐫𝐝) 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐋𝐢𝐧𝐤 : https://lnkd.in/g9NsEW8F 𝐒𝐨𝐥𝐮𝐭𝐢𝐨𝐧 𝐋𝐢𝐧𝐤 : https://lnkd.in/gKZGpX2B Today’s problem was a real brain-teaser that beautifully connects 𝐛𝐢𝐭 𝐦𝐚𝐧𝐢𝐩𝐮𝐥𝐚𝐭𝐢𝐨𝐧 with 𝐆𝐫𝐚𝐲 𝐜𝐨𝐝𝐞 𝐥𝐨𝐠𝐢𝐜 𝐓𝐡𝐞 𝐜𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞: Transform an integer n → 0 using minimum operations, where each operation allows you to flip certain bits based on specific rules. At first, it looks like a simulation problem... but the real trick lies in understanding how Gray codes work — where consecutive numbers differ by exactly one bit. 𝐈𝐧𝐭𝐮𝐢𝐭𝐢𝐨𝐧: We keep 𝐗𝐎𝐑𝐢𝐧𝐠 𝐧 with its 𝐫𝐢𝐠𝐡𝐭-𝐬𝐡𝐢𝐟𝐭𝐞𝐝 𝐯𝐚𝐥𝐮𝐞 until it becomes 0 , this effectively computes the minimal number of bit flips required. #LeetCode #Java #BitManipulation #CodingChallenge #DSA #ProblemSolving #DeveloperJourney #CodeNewbie #50DaysOfCode #WomenInTech #LearningEveryday
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
-
-
Today’s problem: Merge Two Sorted Lists 🔗 Problem: Given two sorted linked lists, merge them into one sorted list and return it. Example: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Approach I used: ✅ Create a dummy node to simplify pointer operations. ✅ Use a pointer (tail) to build the merged list by comparing the heads of both lists. ✅ Append the smaller node each time and move forward. ✅ When one list ends, attach the remaining nodes of the other. A clean iterative solution that keeps the space usage minimal (O(1) extra space). ⚡
To view or add a comment, sign in
-
-
🚩 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
-
-
🔥 #100DaysOfDSA — Day 31/100 Topic: Pairs in an Array 👯♂️ 💡 What I Did Today: Today, I learned how to print all possible pairs from an array — a great exercise to understand nested loops and how combinations work in arrays. 🧠 Logic Used: Use two loops: Outer loop picks the first element. Inner loop pairs it with every following element. Keep a counter to track the total number of pairs. 📊 Example: Input → {2, 4, 6, 8, 10} Output → (2,4) (2,6) (2,8) (2,10) (4,6) (4,8) (4,10) (6,8) (6,10) (8,10) Total pairs = 10 ⚙️ Time Complexity: O(n²) because of the nested loops. Simple yet powerful way to grasp combinations in arrays. ✨ Takeaway: Working with array pairs really strengthens how I think about nested iterations and relationships between elements. Each small step builds stronger DSA intuition 💪 #100DaysOfCode #Day31 #Java #DSA #Arrays #CodingJourney #ProblemSolving #DeveloperLife #LearnInPublic #CodeNewbie #LogicBuilding
To view or add a comment, sign in
-
-
🗓 Day 10 / 100 — #100DaysOfLeetCode 🔍 Problem 1437: Check If All 1's Are at Least Length K Places Away Given a binary array nums and an integer k, the goal is to check whether every pair of 1s in the array is separated by at least k zeros. 🧠 My Approach I traversed the array while keeping track of the last index where a 1 appeared. When encountering a new 1: If it’s the first 1, just store its index. Otherwise, check the distance from the previous 1. If the gap is less than k, return false. If no violations are found, return true ⏱ Time Complexity O(n) – Only a single pass through the array. 💾 Space Complexity O(1) – Uses only constant extra space. #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
💡 Day 34/100 ✅ Remove Zeros in Decimal Representation Today’s challenge was about removing all the zeros from a given number’s decimal form. For example, 1020030 → 123. It might look simple, but it reinforced the importance of handling edge cases like n = 0 or n = 1000, which can lead to empty strings or parsing errors. 🧠 Key Features: Practiced both String-based and Math-based approaches. Explored the difference between using int and long for large numbers. Strengthened understanding of number-to-string conversions and parsing. ⚙️ Time Complexity: O(d) (where d = number of digits) 💾 Space Complexity: O(d) #Day33Of100 #DSA #Java #ProblemSolving #CodingJourney #100DaysOfCode #LearnEveryday #LogicBuilding
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
Interesting to see the two-pointer technique being discussed — we use similar sorting and pointer-based approaches in RTL design when optimizing memory controllers and implementing efficient search algorithms in hardware. The O(N log N) complexity reminds me of the trade-offs we face between logic area and timing closure when designing parallel sorting networks in FPGA implementations.