🚀 Day 13 of My DSA Journey Today I solved the “Power of Four” problem on LeetCode using Java. 💡 Problem Statement Given an integer n, determine whether it is a power of four. A number is a power of four if: n=4xn = 4^xn=4xfor some integer x. Example: Input: 16 → Output: true (because 42=164^2 = 1642=16) Input: 5 → Output: false ⚙️ Approach I Used I used a division-based approach. Steps: 1️⃣ First check if n is less than or equal to 0 → return false 2️⃣ While n is divisible by 4, keep dividing it by 4 3️⃣ After the loop, check if the result becomes 1 If it becomes 1, the number is a power of four. 📊 Complexity Analysis Time Complexity: O(log₄ n) because we keep dividing the number by 4. Space Complexity: O(1) ⚡ Result ✅ Accepted ⚡ Runtime: 0 ms (Beats 100%) 📚 Key Learning This problem helped reinforce number patterns and mathematical thinking, which are very useful in algorithmic problem solving. Consistency is slowly building stronger DSA fundamentals. On to Day 14 tomorrow. 💪 #DSA #LeetCode #100DaysOfCode #Java #ProblemSolving #CodingJourney #Algorithms #LearningInPublic #Consistency
Power of Four Problem Solved on LeetCode with Java
More Relevant Posts
-
🚀 Day 1 of my DSA Journey with CodeOjas! Today, I solved a “Array Reversal in Blocks” problem using Java. Problem: Given an array of integers and a block size, reverse the array in blocks of the given size, but keep the middle block unchanged. Example: Input: arr = [1, 2, 3, 4, 5, 6, 7], blockSize = 3 Output: [3, 2, 1, 4, 5, 6, 7] Approach: Divided the array into blocks of the given size Reversed all blocks except the middle block Constructed a new array to store the modified result (brute-force approach) Complexity: Time Complexity: O(n) Space Complexity: O(n) This is a brute-force approach — simple, clear, and easy to understand for beginners. 💡 Takeaway: Breaking down a problem into blocks can help solve complex array manipulation problems easily. 📂 Code is available on my GitHub: [https://lnkd.in/g4yxAJs3] #DSA #Java #CodingJourney #CodeOjas #ProblemSolving #LinkedInLearning
To view or add a comment, sign in
-
Day 7 of Java with DSA Journey 🚀 📌 Topic: Missing Number (LeetCode 268) Quote: "Sometimes the best way to solve a coding problem is to put down the keyboard and pick up a calculator." ✨ What I learned today: 🔹 Mathematical Optimization: Instead of brute force, I used a simple formula to compute the expected sum. {Sum} = \frac{n(n+1)}{2} 🔹 Core Idea: Expected Sum − Actual Sum = Missing Number 🔹 Alternative Approach: Used XOR technique where duplicates cancel out → leaving the missing value. 🔹 Efficiency: ✔️ Time Complexity: O(n) ✔️ Space Complexity: O(1) 🧠 Problem Solved: ✔️ Missing Number 💡 Key Insight: We often jump to HashSet or Sorting, but this problem taught me to pause and think mathematically first. A simple formula reduced space complexity from O(n) → O(1) 🚀 ⚡ Interview Tip (Important!): Be careful of integer overflow in Java: n * (n + 1) can exceed int limit Use long OR switch to XOR method for safety Consistency is the real key 🔑 #DSA #LeetCode #Java #Mathematics #CodingJourney #ProblemSolving #Day7 #Algorithms #Optimization #Array #MCA #lnct #100DaysOfCode #Algorithms #SoftwareEngineering #InPlaceAlgorithms #TechLearning #JavaDeveloper
To view or add a comment, sign in
-
-
🤯 Most developers get this wrong! The misconception: You might think the output is 2147483648. After all, Math.abs() should return a positive value… right? The reality: The output is actually -2147483648. Yes — still negative. Why does this happen? 🧠 • Integer limits: In Java, int is a 32-bit signed integer. Integer.MIN_VALUE = -2147483648 • Maximum value: Integer.MAX_VALUE = 2147483647 • The catch (overflow): The positive counterpart of Integer.MIN_VALUE doesn’t fit in an int. Because Java uses two’s complement, it overflows… and wraps right back to the same negative value. How to handle it? 🛠️ Use Math.absExact() if you want to catch this edge case. It throws an ArithmeticException instead of silently overflowing. Drop a 💻 if you knew this, or a 🤯 if this surprised you! #Java #Programming #Coding #DeveloperLife #TechTrivia #ComputerScience #SoftwareEngineering #LearnToCode #CodingMisconceptions
To view or add a comment, sign in
-
-
🚀 Day 7/100 – DSA Journey Today’s focus was on implementing a classic and important problem in number theory — Armstrong Numbers — using Java. 🔍 What I built: A program to find all Armstrong numbers within a given range (100 to 2000). 💡 Approach Breakdown: Iterated through each number in the range Counted the number of digits Extracted each digit and computed its power using Math.pow() Summed the results and compared with the original number Printed the number if it satisfies the Armstrong condition ⚙️ Key Concepts Used: Loops (while, for) Mathematical logic (digit extraction, power calculation) Function design for reusability Clean iteration over a range 📌 Output Observed: 153, 370, 371, 407, 1634 📈 Learning Insight: This problem strengthened my understanding of number manipulation and algorithmic thinking, which are fundamental in DSA and frequently asked in interviews. 🔥 Consistency is the key — building problem-solving skills step by step! #Day7 #100DaysOfCode #DSA #Java #ProblemSolving #CodingJourney #SoftwareDevelopment #LearnInPublic 🙌 Tagging: 10000 Coders Pathulothu Navinder
To view or add a comment, sign in
-
-
Something interesting I realized while practicing DSA: At first, I used to think problems are about finding the correct answer. But now it feels like most problems are actually about eliminating wrong possibilities efficiently. While working on Binary Search patterns, a small change in condition can completely change the result - and that’s where the real thinking happens. It’s not about writing more code, it’s about thinking in a more structured way. Still early in the journey, but slowly starting to see how patterns repeat across problems. Learning and building in public. #DSA #ProblemSolving #LearningInPublic #Java #CodingJourney
To view or add a comment, sign in
-
Day 4 of Java with DSA Journey 🚀 📌 Topic: Search Insert Position (LeetCode 35) 💬 Quote: "Binary Search is about more than finding a needle in a haystack; it's about knowing exactly where to put a new needle." ✨ What I Learned: 🔹 Power of the low Pointer: If the target isn’t found, low directly gives the correct insertion index. 🔹 Finding Position Even When Missing: Binary Search doesn’t just search — it tells you where the element belongs. 🔹 Efficient Gap Detection: Even for missing values, we maintain O(log n) efficiency. 🔹 Complexity: ⏱ Time: O(log n) 📦 Space: O(1) 🧠 Problem Solved: ✔️ Search Insert Position 💡 Key Insight: Binary Search helps determine the rank/position of a number in a sorted array — whether it exists or not ⚡ ⚡ Interview Insight (Post-Loop Behavior): 👉 When the loop ends (low > high): low → first index greater than target high → last index smaller than target 🎯 That’s why low = insert position 🔑 Takeaway: Binary Search is not just about finding — it's about positioning. Consistency is the real key 🔑 #DSA #LeetCode #Java #CodingJourney #BinarySearch #ProblemSolving #100DaysOfCode #Algorithms #TechLearning #Day4 #Array #MCA #lnct
To view or add a comment, sign in
-
-
🚀 DSA Learning Journey | Day 13 | Java Solved “20. Valid Parentheses.” 💡 Key Idea: Used a Stack to track opening brackets and match them with corresponding closing brackets. ⚙ Implementation • Language: Java • Time Complexity: O(n) • Space Complexity: O(n) 📚 Learning: How stack data structure helps in solving matching and validation problems efficiently. #JavaDeveloper #DSA #LeetCode #ProblemSolving #Stack #Algorithms #CodingJourney #100DaysOfCode #DevelopersOfLinkedIn
To view or add a comment, sign in
-
-
🚀 Day 39 / 180 – DSA with Java 🚀 📘 Topic Covered: Math & Simulation 🧩 Problem Solved: Count Operations to Obtain Zero Problem: Given two integers, repeatedly subtract the smaller number from the larger one until one of them becomes zero. Return the number of operations performed. Approach: Simulated the process by comparing both numbers and subtracting the smaller from the larger in each step, counting the number of operations until one becomes zero. Key Learning: ✔️ Understanding simulation-based problem solving ✔️ Applying basic math logic efficiently ✔️ Breaking problems into simple iterative steps If you’re also preparing for DSA, let’s connect and learn together 🤝 #DSA #Java #180DaysOfCode #LearningInPublic #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 90 of Learning DSA with Java Today, Saksham Sir explained an important tree problem from LeetCode — Lowest Common Ancestor (LCA). 💡 Key Understanding: The goal of this problem is to find the first common intersection node where both nodes p and q meet in a binary tree. 🧠 Approach I Learned (Recursion): First, handle base cases: If root == null → return null If root == p or root == q → return root Then recursively search: Call left subtree → lowestCommonAncestor(root.left, p, q) Call right subtree → lowestCommonAncestor(root.right, p, q) Store results in variables: left and right 🔍 Final Logic: If both left and right are not null → return root (this is the LCA) If only left is not null → return left If only right is not null → return right ✨ This problem really helped me understand how recursion works deeply in tree structures and how to handle multiple conditions effectively. 📌 Consistency is key — small steps every day! #DSA #Java #LeetCode #BinaryTree #Recursion #CodingJourney #PlacementPreparation
To view or add a comment, sign in
-
-
🚀 Day 38 / 180 – DSA with Java 🚀 📘 Topic Covered: Strings & Two-Pointer Technique 🧩 Problem Solved: Is Subsequence Problem: Given two strings s and t, check whether s is a subsequence of t. Approach: Used two pointers to traverse both strings. Moved the pointer in s only when characters matched, and always moved the pointer in t, ensuring order is maintained. Key Learning: ✔️ Applying two-pointer technique on strings ✔️ Understanding subsequence vs substring ✔️ Writing clean and efficient O(n) solutions If you’re also preparing for DSA, let’s connect and learn together 🤝 #DSA #Java #180DaysOfCode #LearningInPublic #Strings #ProblemSolving #Consistency
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