🚀 Day 9 of My DSA Journey Today I solved the “Reverse Integer” problem on LeetCode using Java. This problem focuses on number manipulation, loops, and handling integer overflow, which makes it slightly tricky compared to basic problems. 💡 Problem Overview We are given a signed 32-bit integer x, and the task is to reverse its digits. Example: Input: 123 → Output: 321 Input: -123 → Output: -321 However, there is an important condition: ⚠️ If reversing the number causes it to go outside the 32-bit integer range, we must return 0. The valid integer range is: −2³¹ to 2³¹ − 1 ⚙️ Approach I Used I solved this problem using mathematical digit extraction: 1️⃣ Extract the last digit using x % 10 2️⃣ Remove the last digit from the number using x / 10 3️⃣ Build the reversed number using sum = sum * 10 + digit 4️⃣ Before adding the digit, check overflow condition using Integer.MAX_VALUE and Integer.MIN_VALUE. 📊 Complexity Analysis Time Complexity: O(log₁₀ n) Because the number of digits in n determines the number of loop iterations. Space Complexity: O(1) No extra memory is used. 📚 Key Learnings ✔ Practiced digit manipulation using modulus and division ✔ Learned how to handle integer overflow conditions ✔ Strengthened understanding of mathematical logic in algorithms 💻 Result ✅ Accepted ⚡ Runtime: 1 ms (Beats 99.88%) Small progress every day is building a strong foundation in Data Structures and Algorithms. On to Day 10 tomorrow. 💪 #DSA #LeetCode #100DaysOfCode #Java #ProblemSolving #CodingJourney #LearningInPublic #SoftwareDevelopment #Consistency
Reversing Integers on LeetCode with Java
More Relevant Posts
-
🚀 Day47 🚀 DSA Spotlight: Find Duplicate Characters in a String (with Count) in Java Today I solved a classic string problem using a basic brute-force approach 👨💻 🔍 Problem Statement: Given a string, identify all duplicate characters along with their frequency. 🧩 Example Input: "akashkulal" 📊 Output: a → 4, k → 2, l → 2 💡 Approach Used (Beginner-Friendly): ✔ Convert string to character array ✔ Compare each character with the rest (nested loop) ✔ Mark visited characters to avoid repetition ✔ Print characters with count > 1 ⏱️ Time Complexity: O(n²) 📦 Space Complexity: O(1) (No extra data structures used) ✨ Key Learning: Even without advanced data structures like HashMap, we can solve problems using strong fundamentals. Mastering these basics builds a solid foundation for optimized solutions later! 🔥 Next Step: Try optimizing this using HashMap to reduce time complexity to O(n) #Java #DSA #CodingPractice #BeginnerFriendly #ProblemSolving #TechInterview #100DaysOfCode #Developers #LearningJourney
To view or add a comment, sign in
-
🚀 #Headline: Day 10 — How Arrays Work in Java (Deep Dive into Memory) After learning array syntax in Part 1, today's lecture went beneath the surface. This wasn't about writing more code – it was about understanding what actually happens inside the JVM when you write new int[5]. This is the kind of knowledge that separates surface-level coders from developers who truly understand their tools. Covered in this lecture: ✔️ Why arrays are stored in contiguous memory ✔️ How arrays are allocated in Heap memory (not Stack!) ✔️ Stack vs Heap – what goes where ✔️ What happens inside the JVM during array creation ✔️ How memory indexing actually works (base address + offset) ✔️ Why array access is O(1) – constant time random access ✔️ Performance implications of contiguous storage ✔️ Special case of boolean arrays ✔️ How 2D arrays are stored in memory ✔️ Array of Strings – reference behavior The "aha" moment came when the instructor explained the math behind indexing: address = base + (index × size). This is why arrays are so fast – no traversal needed, just a direct calculation. Understanding that array references live on the Stack while the actual data lives on the Heap clarifies so many concepts about memory management. This lecture truly delivered on its promise: "This is not just coding. This is internal understanding." Learning from: 👨🏫 Aditya Tandon (Instructor) 🚀 Rohit Negi (CoderArmy) 📺 Source: https://lnkd.in/gpFpcpDs Let's connect 🤝 Pankaj Kumar #Java #100DaysOfCode #ProgrammingJourney #JavaBasics #BuildInPublic
To view or add a comment, sign in
-
-
Binary Search is a powerful algorithm that efficiently finds an element in a sorted array by repeatedly dividing the search space in half. This approach significantly reduces time complexity compared to linear search. ✅ Problem: Binary Search 💻 Language: Java ⚡ Time Complexity: O(log n) Consistent practice is the key to mastering Data Structures & Algorithms. #LeetCode #BinarySearch #DSA #Java #Coding #ProblemSolving #SoftwareDevelopment
To view or add a comment, sign in
-
-
75 days of solving DSA problems. One thing I've learned so far: Many problems are just variations of classic algorithms. Today was a perfect example. 🚀 Day 75/365 — DSA Challenge Solved: Search in Rotated Sorted Array The Problem Normally, Binary Search works on a sorted array. But what if the array was rotated? 💡 My Approach This problem is solved using a modified Binary Search. At each step: 1️⃣ Find the middle element 2️⃣ Check which side is sorted Left sorted: nums[left] <= nums[mid] Right sorted: nums[mid] <= nums[right] 3️⃣ Decide which half to search based on where the target can exist This keeps the search time at O(log n). Complexity ⏱ Time: O(log n) 📦 Space: O(1) Day 75/365 complete. 💻 290 days to go. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #Algorithms #LearningInPublic
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
-
Day 23/100 of DSA , (Arrays) 🚀 Today IPractice – Container With Most Water Solved the classic Two Pointer problem “Container With Most Water” on LeetCode today. I used brute force approach with O(n²) complexity, the problem can be optimized using the Two Pointer technique, reducing the time complexity to O(n). 🔹 Key Idea: Start with two pointers at both ends of the array. Calculate the area using min(height[left], height[right]) * width. Move the pointer with the smaller height to potentially find a taller boundary and maximize the area. 💻 Tech Stack: Java | DSA Consistency in practicing data structures and algorithms is the key to improving problem-solving ability. #DSA #Java #LeetCode #ProblemSolving #CodingJourney #SoftwareDevelopment
To view or add a comment, sign in
-
-
🚀 DAY 69/150 — FINDING PATTERNS THROUGH SORTING! 🚀 Day 69 of my 150 Days DSA Challenge in Java and today I solved a problem that highlights the power of sorting and pattern observation 💻🧠 📌 Problem Solved: Minimum Absolute Difference 📌 LeetCode: #1200 📌 Difficulty: Easy–Medium The task is to find all pairs of elements with the minimum absolute difference in a given array. 🔹 Approach Used Instead of comparing every pair (which would take O(n²)), I used a more optimized approach: • First, sort the array • Then check only adjacent elements • Keep track of the minimum difference • Store all pairs that match this minimum difference Sorting helps reduce unnecessary comparisons and simplifies the logic. ⏱ Complexity Time Complexity: O(n log n) (due to sorting) Space Complexity: O(n) (for storing result pairs) 🧠 What I Learned • Sorting can significantly reduce the complexity of comparison-based problems • Adjacent elements in a sorted array often reveal important patterns • Avoid brute-force when a structured approach exists 💡 Key Takeaway This problem taught me that sometimes the simplest optimization is just reordering the data first. Once sorted, many complex problems become easier to solve. It also strengthened my understanding of how pattern recognition + sorting can lead to efficient solutions. ✅ Day 69 completed 🚀 81 days to go 🔗 Java Solution on GitHub: 👉 https://lnkd.in/gYHCJmkv 💡 Sorting is not just a technique — it’s a problem-solving strategy. #DSAChallenge #Java #LeetCode #Sorting #Greedy #150DaysOfCode #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
DSA – Day 1 (Revision): Array Basics (Java) Today, I revised the basic concepts of arrays by asking simple questions and understanding them with small examples. 1. What is an array? An array is used to store multiple values of the same data type using a single variable name. Example: int[] numbers = {10, 20, 30}; 2. Why do we use arrays? When we have many values of the same type,creating separate variables is difficult. Arrays help store and manage such data easily. Example: int[] marks = {75, 80, 90}; 3. What is array declaration and initialization? Declaration defines the data type and name of the array.Initialization allocates memory and sets the size or values. Example: int[] a = new int[5]; // declaration + initialization int[] b = {1, 2, 3}; // direct initialization 4.What is an index and why does it start from 0? An index represents the position of an element in an array.Index starts from 0 because the first element is stored at the starting memory location. Example: a[0] // first element 5.Why is array size fixed? When an array is created, memory is allocated based on its size.This size cannot be changed later. Example: int[] a = new int[3]; // array size is fixed to 3 Revising these basics helped me clearly understandhow arrays work in Java. #DataStructures #Algorithms #Coding #Programming #ProblemSolving #CodingLife #CodeDaily
To view or add a comment, sign in
-
-
📅 Day 61 out of 100 — Solving LeetCode Problems Daily, Kickstarting My Java + DSA Journey 📘 Course: Data Structures & Algorithms 📈 One Problem a Day: Consistency Compounding. Leetcode Problem_94. Binary Tree Inorder Traversal Given the root of a binary tree, return the inorder traversal of its nodes' values. Example 1: Input: root = [1,null,2,3] Output: [1,3,2] Explanation: Example 2: Input: root = [1,2,3,4,5,null,8,null,null,6,7,9] Output: [4,2,6,5,7,1,3,9,8] Explanation: Example 3: Input: root = [] Output: [] Example 4: Input: root = [1] Output: [1] Constraints: The number of nodes in the tree is in the range [0, 100]. -100 <= Node.val <= 100 https://lnkd.in/drecZB7b Solution:- class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> result; stack<TreeNode*> st; TreeNode* curr = root; while (curr != NULL || !st.empty()) { while (curr != NULL) { st.push(curr); curr = curr->left; } curr = st.top(); st.pop(); result.push_back(curr->val); curr = curr->right; } return result; } };
To view or add a comment, sign in
-
-
Day 53 of My DSA Journey Today I solved a problem on searching an element in a rotated sorted array with duplicates. The array is originally sorted but rotated at some pivot. The task is to determine whether a given target value exists in the array. 🔎 My Approach Instead of directly jumping to complex techniques, I used a simple linear search approach: Traverse through the array from start to end. Compare each element with the target value. If a match is found, return true. If the loop finishes without finding the target, return false. 💡 Key Takeaway Sometimes starting with a simple and correct solution is important before optimizing. While more efficient approaches like modified binary search can reduce time complexity, this straightforward method helps clearly understand the problem first. ⏱ Time Complexity: O(n) Every day I’m improving my problem-solving skills in Java and Data Structures & Algorithms step by step. #Day53 #DSA #Java #ProblemSolving #CodingJourney #LeetCode #100DaysOfCode
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