=> Day 20/90 DSA Journey Solved: Count Odd Numbers in an Interval (LeetCode 1523) Instead of using loops, I applied a simple mathematical formula to achieve an O(1) solution. 🔹 Key Idea: Count of odd numbers between low and high can be calculated directly using: -> ((high + 1) / 2) - (low / 2) 🔹 Why this works: -> It efficiently counts odds up to high and subtracts odds before low. 🔹 Example: Input: low = 3, high = 7 Output: 3 (Odd numbers → 3, 5, 7) -> Time Complexity: O(1) -> Space Complexity: O(1) #Java #DSA #LeetCode #ProblemSolving #Coding #Optimization
Solving LeetCode 1523 with O(1) Time Complexity
More Relevant Posts
-
🚀 DSA Journey Update: 7/75 Solved another problem today: Product of Array Except Self 💡 Key Idea: Used prefix (left) and suffix (right) products to build the answer without using division and in O(n) time. 🔍 Example: Input: [1,2,3,4] Output: [24,12,8,6] ⚡ Learning: Optimized approach using two passes Space optimization by reusing the same array Stronger understanding of prefix/suffix patterns Step by step progress… consistency matters more than speed 🔥 #DSA #Java #LeetCode #CodingJourney #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
Day 13/90 DSA Journey Today I worked on an interesting problem where each bulb toggles between ON and OFF based on its occurrences in the input list. ->Key Idea: If a number appears odd number of times → bulb remains ON If it appears even number of times → bulb turns OFF -> Approach: Used a List-based toggle logic Add element if not present Remove element if already present Finally, sort the result -> Time Complexity: O(n²) using List (due to contains & remove) -> Optimized Insight: This problem can be solved more efficiently using a HashSet in O(n) time. -> Takeaway: Understanding frequency and toggling patterns is very useful in solving real-world and DSA problems. #LeetCode #DSA #Java #Coding #ProblemSolving #Learning #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 61 of my DSA Journey Today’s problem: Reverse Linked List 🔁 This is one of those classic problems that really tests your understanding of pointers and how data structures work internally. 💡 What I learned: How to reverse links instead of values Importance of using prev, curr, and next pointers How in-place algorithms help optimize space ⚡ Approach in short: Traverse the list once, and keep reversing the direction of each node step by step. 📊 Complexity: Time: O(n) Space: O(1) 🧠 Takeaway: DSA is not about memorizing solutions, it’s about understanding patterns. Every problem adds a new piece to the puzzle. Consistency > Motivation 💯 #DSAJourney #100DaysOfCode #Java #LeetCode #Coding #ProblemSolving #LinkedList #KeepLearning
To view or add a comment, sign in
-
-
🚀 DSA Preparation 💪 Solved an interesting Array traversal problem by iterating from right to left. Learned how to efficiently track the maximum element on the right side without extra space 🔥 🧠 Problem 🔎 Replace Elements with Greatest Element on Right Side Given an array arr, replace every element with the greatest element among the elements to its right. 👉 The last element should be replaced with -1 (since no elements exist on its right). 👉 Return the modified array. Example Input: arr = [17,18,5,4,6,1] Output: [18,6,6,6,1,-1] Input: arr = [400] Output: [-1] ⚡ Key Learning 📌 Traverse from right to left to keep track of maximum 📌 Update elements in-place for better efficiency 📌 Avoid extra arrays by maintaining a running max 📌 Time Complexity: O(n) → single pass through the array 📌 Space Complexity: O(1) → no extra space used (in-place update) Improving DSA with smart array techniques 🚀 #DSA #LeetCode #Arrays #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 99 of DSA Problem Solving Today’s problem: Word Pattern (LeetCode 290) At first glance, this problem looked simple — just matching characters with words. But the real challenge was ensuring a bijective mapping (one-to-one relationship) between pattern characters and words. 🔍 Problem Idea: We need to check if each character in the pattern maps to exactly one word in the string, and each word maps back to only one character. 💡 Key Learning: One-directional mapping is NOT enough We must ensure two-way consistency (character → word AND word → character) HashMaps are powerful for maintaining this mapping efficiently 🧠 Concepts Practiced: HashMap usage String manipulation (split) Bijective mapping logic ⏱ Time Complexity: O(n), where n = number of words 💭 Real Journey Behind the Solution: Initially, I thought using just one HashMap would work, but that led to incorrect mappings. Then I realized the importance of maintaining two HashMaps to ensure no duplication from either side. This problem strengthened my understanding of mapping relationships — a small concept but very powerful in many DSA problems. 🔥 Slowly getting better every day… consistency is the real game. #Day99 #DSA #LeetCode #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🔥 Day 58 of DSA Journey Solved the classic 3Sum (LeetCode 15) problem today. 💡 Key Learnings: Sorting simplifies the problem structure Fix one element and apply the two-pointer approach Handling duplicates correctly is crucial to avoid redundant results ⏱️ Complexity: Time: O(n²) Space: O(1) (excluding output) 📊 Result: Runtime: 33 ms (Beats 73.75%) Strengthened understanding of two-pointer pattern This problem reinforced an important pattern: 👉 Reduce 3Sum → 2Sum using two pointers On to the next challenge 🚀 #DSA #LeetCode #CodingJourney #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🔥 DSA Challenge – Day 135/360 🚀 📌 Topic: Bit Manipulation 🧩 Problem: Check if a Number is Power of 2 Problem Statement: Given an integer n, determine whether it is a power of 2 or not. 🔍 Example: Input: n = 4 Output: true Input: n = 6 Output: false 💡 Approach: Optimized (Bit Manipulation) 1️⃣ Step 1 – If n <= 0, return false (powers of 2 are always positive) 2️⃣ Step 2 – Use bit trick: n & (n - 1) removes the lowest set bit 3️⃣ Step 3 – If result becomes 0, then only one set bit was present ✔ This avoids looping and works in constant time ⏱ Complexity: Time: O(1) Space: O(1) 📚 Key Learning: A number is a power of 2 if it has exactly one set bit in binary representation. #DSA #Java #Coding #InterviewPrep #ProblemSolving #TechJourney #135DaysOfCode #BitManipulation #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 83 – DSA Journey | Symmetric Tree (Mirror Check) Continuing my daily DSA practice, today I worked on a problem that deepened my understanding of tree symmetry and recursion. 📌 Problem Practiced: Symmetric Tree (LeetCode 101) 🔍 Problem Idea: Check whether a binary tree is a mirror of itself — i.e., symmetric around its center. 💡 Key Insight: A tree is symmetric if the left subtree is a mirror reflection of the right subtree. This can be checked by comparing nodes in a cross manner. 📌 Approach Used: • Compare the tree with itself using a helper function • If both nodes are null → symmetric at this level • If one is null or values differ → not symmetric • Recursively compare: – Left of first subtree with right of second – Right of first subtree with left of second 📌 Concepts Strengthened: • Tree traversal • Recursion • Mirror / symmetry logic • Structural comparison ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) 🔥 Today’s takeaway: Many tree problems become easier when visualized as mirror or reflection comparisons. On to Day 84! 🚀 #Day83 #DSAJourney #LeetCode #BinaryTree #Recursion #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 84 of My DSA Journey Today’s problem: Counting Bits Given a number n, the task is to return an array where each index i contains the number of 1’s in the binary representation of i. 🔍 Example: Input: n = 5 Output: [0, 1, 1, 2, 1, 2] 💡 Key Insight: Instead of counting bits every time, we reuse previous results: i >> 1 → removes last bit i & 1 → checks if last bit is 1 So, 👉 ans[i] = ans[i >> 1] + (i & 1) ⚡ This reduces time complexity to O(n) (single pass!) 📈 What I learned today: Dynamic Programming can simplify repeated computations Bit manipulation makes problems faster and cleaner Small patterns can lead to big optimizations #Day84 #DSA #Java #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 89 – DSA Journey | Invert Binary Tree Continuing my daily DSA practice, today I worked on a classic problem that builds strong intuition around tree manipulation. 📌 Problem Practiced: Invert Binary Tree (LeetCode 226) 🔍 Problem Idea: Given a binary tree, invert it by swapping the left and right children of every node. 💡 Key Insight: At each node, simply swap its left and right subtrees. Recursively applying this across the tree results in a complete mirror of the original tree. 📌 Approach Used: • If the node is null → return null • Swap left and right children • Recursively apply the same operation on both subtrees • Return the root after inversion 📌 Concepts Strengthened: • Tree traversal • Recursion • Tree manipulation • Mirror transformation ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) 🔥 Today’s takeaway: Simple operations applied recursively can transform complex structures like trees efficiently. On to Day 90! 🚀 #Day89 #DSAJourney #LeetCode #BinaryTree #Recursion #Java #ProblemSolving #Coding #LearningInPublic #Consistency
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