Solved the Ugly Number problem using a simple and efficient approach 💡 🔹 Approach: An ugly number is a number whose prime factors are only 2, 3, and 5. So instead of checking all factors, I used a reduction method: If the number is ≤ 0 → not ugly. Keep dividing the number by 2, 3, and 5 as long as it is divisible. If after removing all these factors the number becomes 1, then it is an ugly number. If something else remains, it means another prime factor exists → not ugly. This approach works because ugly numbers are made only by multiplying 2, 3, and 5. Removing these factors should eventually reduce the number to 1. 🔹 Why this approach? Avoids factorization of all numbers. Directly removes allowed prime factors. Simple, clean, and optimal logic. 🔹 Time Complexity: O(log n) Each division reduces the number significantly. 🔹 Space Complexity: O(1) No extra space used, only variable updates. #Java #Coding #ProblemSolving #DSA #LeetCode #Programming #LogicBuilding #DataStructures #Algorithms #JavaDeveloper #CodingJourney #LearnToCode #TechSkills #ComputerScience #CodeDaily #DeveloperLife #PlacementPreparation #CodingPractice #SoftwareEngineering
Solving Ugly Number Problem with Efficient Java Approach
More Relevant Posts
-
Day 52 - LeetCode Journey Solved LeetCode 11: Container With Most Water (Medium) today using the Two Pointer approach. This journey isn’t about solving the hardest problems daily, but about showing up consistently and improving step by step. Problems like this help in building a deeper understanding of how optimization actually works. Today’s problem reinforced key concepts like: • Using two pointers to reduce time complexity • Understanding how width and height affect area • Moving the pointer with smaller height for better results • Avoiding brute force and thinking optimally The key idea is simple — start from both ends and move intelligently to maximize the area 💡 Every problem adds a new layer to problem-solving skills, and that’s where real growth happens 💯 ✅ Better understanding of two-pointer technique ✅ Improved optimization thinking ✅ More confidence in DSA concepts Still a long way to go, but progress is progress 🚀 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #Algorithms #Programming #DeveloperJourney #KeepCoding
To view or add a comment, sign in
-
-
Day 53 - LeetCode Journey Solved LeetCode 80: Remove Duplicates from Sorted Array II (Medium) using an in-place two-pointer approach. This journey isn’t about solving the hardest problems daily, but about showing up consistently and improving step by step. Problems like this strengthen core fundamentals of arrays and in-place manipulation. Today’s problem reinforced key concepts like: • Using two pointers to track valid positions • Maintaining constraints (at most twice occurrence) • Performing operations in-place with O(1) space • Writing clean and efficient logic The idea is simple — allow each element at most twice by comparing with the element at index i-2, ensuring the array stays valid 💡 Every problem adds a new layer to problem-solving skills, and that’s where real growth happens 💯 ✅ Better understanding of array manipulation ✅ Improved in-place optimization skills ✅ More confidence in DSA concepts Still a long way to go, but progress is progress 🚀 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #Algorithms #Programming #DeveloperJourney #KeepCoding
To view or add a comment, sign in
-
-
Day 58 - LeetCode Journey Solved LeetCode 374: Guess Number Higher or Lower (Easy) today using the Binary Search approach. This problem is a perfect example of how a simple idea can become very efficient with the right approach. Instead of checking every number, we narrow down the search space step by step. Today’s problem reinforced key concepts like: • Applying Binary Search on a defined range • Reducing time complexity from O(n) to O(log n) • Making decisions based on feedback (higher / lower) • Writing clean and optimized logic The idea is straightforward — pick the middle number and adjust the search space based on whether the guess is high or low 💡 This approach ensures we find the answer in the minimum number of steps, making it both efficient and elegant. Every problem, even easy ones, helps strengthen the fundamentals and improve thinking 💯 ✅ Better understanding of binary search ✅ Improved problem-solving efficiency ✅ Stronger DSA foundation Still a long way to go, but progress is progress 🚀 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #Algorithms #BinarySearch #Programming #DeveloperJourney #KeepCoding
To view or add a comment, sign in
-
-
🚀 Day 522 of #750DaysOfCode 🚀 Today I solved the LeetCode problem “Minimum Changes To Make Alternating Binary String.” 🔹 Problem: Given a binary string consisting of 0s and 1s, we need to make the string alternating (no two adjacent characters are the same). In one operation, we can flip any character (0 → 1 or 1 → 0). The goal is to find the minimum number of operations required. 🔹 Key Idea: An alternating string can only follow two possible patterns: 1️⃣ 010101... 2️⃣ 101010... So we: Count mismatches assuming the string starts with '0'. The other pattern mismatches will be n - mismatch. The minimum of these two values gives the answer. 🔹 Time Complexity: O(n) – we traverse the string once. 📌 Example Input: "1111" Possible alternating strings → "0101" or "1010" Minimum operations → 2 Problems like this highlight how pattern observation can simplify the solution. #leetcode #programming #coding #java #softwaredevelopment #dsa #problemSolving #750DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 66 / 100 Days of LeetCode Challenge ✅ Problem Solved: Binary Gap 💡 Goal: Find the longest distance between two consecutive 1s in the binary representation of a given integer. 🧠 Thought Process: Removed trailing zeros to start from the first 1. Handled edge case where number is 1 → return 0. Iterated through bits, tracking gap length between 1s. Updated max gap whenever a 1 was found. 📈 Result: ⚡ Runtime: 0 ms (Beats 100% 🎉) 💾 Memory: 42.56 MB (Beats 14.06%) 📝 Key Takeaway: Efficient bit manipulation can drastically simplify problems involving binary representations. Always consider using bitwise operators for performance gains! #LeetCode #100DaysOfCode #CodingChallenge #Java #BitManipulation #ProblemSolving #TechJourney #DeveloperLife #WomenInTech #CodeNewbie #Programming #DailyCoding #BinaryGap #LearnToCode #DSA #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 4/60 — LeetCode Discipline Problem Solved: Median of Two Sorted Arrays (Revision) Difficulty: Hard Today’s session was focused on revisiting one of the most conceptually demanding problems in array partitioning and binary search. The goal wasn’t just correctness — it was clarity in partition logic and optimal time complexity. 💡 Focus Areas: • Strengthened binary search partition technique • Deepened understanding of array boundary conditions • Practiced logarithmic-time optimization thinking • Focused on writing clean and efficient logic ⚡ Performance Highlight: Achieved ~99% runtime efficiency on submission. Consistent refinement of hard problems is steadily sharpening my algorithmic intuition. #LeetCode #60DaysOfCode #100DaysOfCode #DSA #BinarySearch #HardProblems #Algorithms #DataStructures #ProblemSolving #CodingJourney #SoftwareEngineering #TechCareers #Programming #Developers #CodingLife
To view or add a comment, sign in
-
-
Day 57 - LeetCode Journey Solved LeetCode 2104: Sum of Subarray Ranges (Medium) today using a brute force + optimization approach. This journey isn’t about solving the hardest problems daily, but about showing up consistently and improving step by step. Problems like this help in building a deeper understanding of arrays and how subarrays behave. Today’s problem reinforced key concepts like: • Understanding how to generate all subarrays efficiently • Tracking minimum and maximum values dynamically • Avoiding recomputation using smart updates • Writing clean and logical nested loop solutions The idea is simple — for each starting index, expand the subarray and keep updating min and max to calculate the range 💡 Every problem adds a new layer to problem-solving skills, and that’s where real growth happens 💯 ✅ Better understanding of subarrays and ranges ✅ Improved thinking for optimization ✅ More confidence in array-based problems Still a long way to go, but progress is progress 🚀 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #Algorithms #Programming #DeveloperJourney #KeepCoding
To view or add a comment, sign in
-
-
✳️Day 17 of #100DaysOfCode✳️ Solving the Longest Common Subsequence Problem! 🚀 I recently took on the "Longest Common Subsequence" (LCS) challenge on LeetCode—a classic problem that perfectly illustrates the power of Dynamic Programming. Here’s the step-by-step approach I took: ✅ 1. Problem Decomposition: I broke the problem down into smaller sub-problems. If the last characters of two strings match, they contribute to the subsequence; if not, we explore the possibilities by skipping a character from either string. ✅ 2. Recursive Foundation: I started by defining the base case—if either string is empty, the LCS length is 0. ✅ 3. Optimization with Memoization: Pure recursion leads to redundant calculations (overlapping sub-problems). I implemented a 2D array (dp[n][m]) to store results of previously computed states, significantly boosting performance. ✅ 4. Refinement: Fine-tuning the logic to ensure the time and space complexity were balanced for an efficient "Accepted" result . Always learning, always coding. On to the next challenge! 💻 #LeetCode #Java #DynamicProgramming #CodingLife #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
-
Day 98 of my 100 Days of LeetCode Challenge 🚀 Solved Longest Increasing Subsequence (LIS) — a classic Dynamic Programming + Binary Search problem. The task is to find the length of the longest strictly increasing subsequence in an array. Approach used in my solution (Optimized O(n log n)): • Maintained a list res to store the smallest possible tail value for increasing subsequences of different lengths • If the current number is greater than the last element in res, append it • Otherwise, use binary search to find the correct position and replace the element • The size of res at the end gives the length of LIS Important Insight: The list does not store the actual LIS, but it maintains optimal subsequence tails to guarantee maximum possible length. Time Complexity: O(n log n) Space Complexity: O(n) This problem is a huge milestone in understanding advanced DP optimization techniques. #100DaysOfCode #100DaysOfLeetCode #LeetCode #DynamicProgramming #BinarySearch #LIS #Java #DSA #Algorithms #CodingJourney
To view or add a comment, sign in
-
-
Most beginners see methods as a way to “reuse code.” That’s only half the story. Methods are really about 𝗮𝗯𝘀𝘁𝗿𝗮𝗰𝘁𝗶𝗼𝗻 𝗮𝗻𝗱 𝗿𝗲𝘀𝗽𝗼𝗻𝘀𝗶𝗯𝗶𝗹𝗶𝘁𝘆. When you write: public int add(int a, int b) { return a + b; } You’re not just grouping logic. You’re defining a clear contract: • What goes in (parameters) • What comes out (return type) • What the method promises to do That’s powerful. Methods: • Reduce duplication • Improve readability • Break large problems into smaller ones • Create testable units But poorly designed methods create chaos: • Too many parameters • Hidden side effects • Doing multiple responsibilities A good method does one thing clearly. Today was about: • Understanding parameters and return types • Why method signatures matter • Writing small, focused methods Clean systems are built from clean functions. #Java #Methods #CleanCode #SoftwareDesign #Programming #LearningInPublic
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