🤯 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
Java Math.abs() returns negative for Integer.MIN_VALUE
More Relevant Posts
-
Day 73 of #100DaysOfLeetCode 💻✅ Solved #389. Find the Difference problem in Java. Approach: • Initialized a variable to store the sum • Added ASCII values of all characters in string t • Subtracted ASCII values of all characters in string s • The remaining value represents the extra character • Converted the result back to character and returned it Performance: ✓ Runtime: 1 ms (Beats 99.89% submissions) 🚀 ✓ Memory: 42.77 MB (Beats 97.03% submissions) Key Learning: ✓ Practiced optimized string traversal using enhanced for-loop ✓ Reinforced understanding of ASCII-based solutions ✓ Learned how mathematical approaches can simplify problems Learning one problem every single day 🚀 #Java #LeetCode #DSA #Strings #Math #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 3 🧩 Problem Solved: Combination Sum III (LeetCode #216) 💭 What I Learned: Used backtracking to find all possible combinations of k numbers (1–9) that sum up to a target n. At each step: ✔️ Decided whether to include the current number ✔️ Reduced both the remaining sum (n) and count (k) ✔️ Ensured each number is used only once by moving to the previous index Applied constraints effectively to prune recursion when: Remaining sum becomes negative Required count becomes invalid This improved my understanding of handling multiple constraints (sum + size) simultaneously in recursion. ⏱ Time Complexity: O(C(9,k) 🧠 Space Complexity: O(k) (recursion depth) ⚡ Key Takeaways: ✔️ Backtracking with multiple constraints requires careful pruning ✔️ Fixed input size can significantly reduce complexity ✔️ Choosing + skipping pattern helps explore all valid combinations 💻 Language Used: Java ☕ 📘 Concepts: Backtracking · Recursion · Combinations · Constraint Handling #CodeEveryday #DSA #LeetCode #Java #Backtracking #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
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
-
-
Leveling up my DSA skills with Stacks! 🥞 Just finished writing a custom, array-based Stack implementation in Java. Instead of relying on built-in collections, I wanted to manually map out the exact LIFO (Last-In, First-Out) behavior from scratch. Here’s a breakdown of how I structured the code: 🔹 The Core State: Initialized a fixed-size integer array (size 6) along with a top pointer set to -1 to act as the index tracker for the current topmost element. 🔹 The push() Logic: Built to check boundaries first. If top < size - 1, it safely increments the pointer and adds the new value (array[++top] = value). If full, it catches the error and alerts that the stack is overflowing. 🔹 The pop() Logic: Verifies the stack isn't empty (top > -1), then dynamically retrieves the topmost value while decrementing the pointer (array[top--]), effectively "removing" the element. Testing edge cases is the best part. Purposefully pushing a 7th element (nums.push(22)) into my size-6 stack and seeing my custom "the stack is overflowing..." text pop up in the terminal proves the boundary validations are rock solid! Nothing beats the feeling of a bug-free terminal run! Onward to the next coding challenge. 🚀 #Java #Programming #DataStructures #Algorithms #ComputerScience #StudentDeveloper #LIFO
To view or add a comment, sign in
-
-
Understanding arrays is not enough knowing how to operate on them is key. In this short video, I covered: - Traversal (O(n)) - Insertion (O(1) at end, O(n) in middle) - Deletion (O(1) at end, O(n) in middle) - Why shifting elements impacts performance These fundamentals help in choosing the right data structure and writing optimized code. Explore structured DSA in Java roadmap + practice: www.quipoin.com #DSA #Java #Programming #Coding #SoftwareEngineering #DataStructures #Algorithms
To view or add a comment, sign in
-
🔥 𝗗𝗮𝘆 𝟵𝟰/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟱𝟯𝟵. 𝗞𝘁𝗵 𝗠𝗶𝘀𝘀𝗶𝗻𝗴 𝗣𝗼𝘀𝗶𝘁𝗶𝘃𝗲 𝗡𝘂𝗺𝗯𝗲𝗿 | 🟢 Easy | Java Marked as Easy — but the optimal solution is pure binary search brilliance. 🎯 🔍 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 Given a sorted array, find the kth missing positive integer. Linear scan works — but can we do O(log n)? 💡 𝗧𝗵𝗲 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 At index i, the value arr[i] should be i+1 in a complete sequence. So missing numbers before arr[i] = arr[i] - 1 - i This lets us binary search on the count of missing numbers! ⚡ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 ✅ If arr[mid] - 1 - mid < k → not enough missing numbers yet, go right ✅ Else → too many missing, go left ✅ After the loop, left + k gives the exact answer 𝗪𝗵𝘆 𝗹𝗲𝗳𝘁 + 𝗸? After binary search, left is the index where the kth missing number falls beyond. left numbers exist in the array before that point, so the answer is left + k. No extra passes needed. ✨ 📊 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 ⏱ Time: O(log n) — vs O(n) linear scan 📦 Space: O(1) This is a perfect example of binary searching on a derived condition, not just a value. A real upgrade from the naive approach. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gVYcjNS6 𝟲 𝗺𝗼𝗿𝗲 𝗱𝗮𝘆𝘀. 𝗧𝗵𝗲 𝗳𝗶𝗻𝗶𝘀𝗵 𝗹𝗶𝗻𝗲 𝗶𝘀 𝗿𝗶𝗴𝗵𝘁 𝘁𝗵𝗲𝗿𝗲! 🏁 #LeetCode #Day94of100 #100DaysOfCode #Java #DSA #BinarySearch #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
💻 Interesting Java problem: Find the closest target in a circular array Given a words[] array, a target, and a startIndex, the task is to find the shortest circular distance to the target. Leetcode Problem :https://lnkd.in/g3RbdChE Core idea: For every matching target: find direct distance → Math.abs(i - startIndex) find circular distance → n - dist take the minimum. This is a neat example of how circular arrays require us to think beyond normal linear traversal. Small twist, smart logic. 🚀 #Java #Algorithms #DSA #CodingInterview #ProblemSolving #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 11 of Java with DSA Journey — This one blew my mind 🤯 📌 Problem: Power of Three (LeetCode 326) Yesterday, I used bit manipulation for Power of Two. Today? 👉 That approach completely fails. So I had to think differently… 💡 Breakthrough Idea Instead of loops or recursion… I used math + number theory to solve it in O(1) time. 👉 1162261467 % n == 0 Yes, one line. 🧠 Key Learnings 🔹 Bitwise isn’t universal Works great for base-2, but not for base-3 🔹 Prime Numbers Matter Since 3 is prime, its powers divide each other perfectly 🔹 Max Value Trick Largest power of 3 in 32-bit int = 3^19 = 1162261467 ⚡ Complexity ⏱ Time: O(1) 📦 Space: O(1) 🔥 Pro Tips (Interview Level) 💡 Tip 1: Know Your Data Type Limits Many O(1) tricks depend on constraints like 32-bit integer limits 💡 Tip 2: Prime Numbers Unlock Shortcuts If a number is prime, its powers have clean divisibility properties 💡 Tip 3: Always Question the Default Approach Most people write: while (n % 3 == 0) n /= 3; 💡 Tip 4: This is a Pattern Power of 2 → bitwise Power of 3 → math Power of 4 → hybrid 💡 Tip 5: Edge Cases First Always check n <= 0 🔥 Real Insight This problem forced me to shift my thinking: ❌ Rely on one technique ✅ Adapt based on the problem Consistency compounds 📈 #DSA #LeetCode #Java #CodingJourney #ProblemSolving #NumberTheory #InterviewPrep #Day11 #BitManipulation #CleanCode #Array #Optimization #MCA #lnct #100DaysOfCode #SoftwareEngineering #Algorithms #InPlaceAlgorithms #TechLearning #JavaDeveloper
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 33 Today’s focus: Binary Search for next greater element. Problem solved: • Find Smallest Letter Greater Than Target (LeetCode 744) Concepts used: • Binary Search • Upper bound concept • Circular handling Key takeaway: The goal is to find the smallest character strictly greater than a given target in a sorted array. This is a classic upper bound problem: We use binary search to find the first element greater than the target. During search: • If letters[mid] > target, store it as a possible answer and move left • Else move right An important edge case: If no character is greater than the target, we return the first element (circular behavior). Continuing to strengthen binary search patterns and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
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
-
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