Day 4 of #60daysofLeetcode, #7. Reverse Integer - https://lnkd.in/dqA_ciDP is the question. Solution. To reverse the integer, we repeatedly extract the last digit using modulo 10, then build the result from left to right. Each iteration: extract the last digit, update result = result * 10 + digit, and divide input by 10. We continue until the input becomes zero. Before each result update, we check if the operation would cause overflow, and if so, return 0. Time Complexity is O(log n) or O(d) where d is the number of digits. Space complexity is O(1). #LeetCode, #Java, #DSA, #LearningInPublic
Reverse Integer Solution - Java
More Relevant Posts
-
#day329 of #1001daysofcode problem statement (1784): Check if Binary String Has at Most One Segment of Ones Approach 1: If the pattern "01" appears in the string, it means the sequence of '1's was broken Approach 2: Traverse the string and count segments of consecutive '1's. Whenever a '1' is found, skip the entire sequence of continuous '1's and increase the segment count. If the number of segments of '1's is exactly one, the condition is satisfied. --both approaches have same time and space complexity. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(1) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
🚀 Day 37 / 100 | Smallest Pair With Different Frequencies -Intuition: -This problem is based on frequency counting and greedy selection. -So the idea is to count the frequency of each number and then check pairs in increasing order. -Approach: O(n log n) -First, store the frequency of each number using HashMap. -Then, store all distinct numbers in a list. -Sort the list so we can get the smallest values first. -Now, check every pair (x, y) where x < y: If freq(x) != freq(y), return that pair immediately. -Since the list is sorted, the first valid pair will be the answer. -Complexity: Time Complexity: O(n log n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode
To view or add a comment, sign in
-
-
Day 28 — LeetCode Progress (Java) Problem: Sort Array By Parity Required: Given an integer array, rearrange it so that all even numbers come first, followed by all odd numbers. Return the modified array. Idea This is a simple partitioning problem. Even and odd separation can be done by: Collecting evens first Then placing odds No sorting required. Just grouping. Approach Create a result array of same length. Traverse once: If number is even → place in result. Traverse again: If number is odd → place in result. Return result. Time Complexity: O(n) Space Complexity: O(n) (extra array used) #LeetCode #DSA #Java #Arrays #TwoPointers #CodingJourney
To view or add a comment, sign in
-
-
Day 48 — LeetCode Progress (Java) Problem: Divide Array Into Equal Pairs Required: Given an integer array nums of 2n integers, determine whether it is possible to divide the array into n pairs such that each pair contains equal numbers. Idea: Track numbers that appear an odd number of times using a HashSet. Whenever a number appears twice, it forms a pair and gets removed from the set. Approach: Create a HashSet to track numbers with unmatched occurrences. Traverse the array: If the number is not in the set, add it (first occurrence). If the number already exists in the set, remove it (pair completed). At the end: If the set is empty, all numbers formed valid pairs. Otherwise, some numbers appeared an odd number of times. Time Complexity: O(n) Space Complexity: O(n) #LeetCode #DSA #Java #HashSet #Arrays #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🔢 Day 67/100: Convert to Hex - Bit Masking Day 67. Convert integer to hex. Labeled "EASY." Actually easy this time. ✅ 📌 Problem: Convert number to hexadecimal string. 💡 Solution: & 15 → Get last 4 bits (0-15) Map to hex char (0-9, a-f) >>> 4 → Shift right 4 bits Repeat, reverse result 🎯 The Key: num & 15 extracts last hex digit. num >>>= 4 moves to next digit. 📊 Complexity: O(1) - at most 8 hex digits Day 67. Bit manipulation ftw. 💪 #100DaysOfCode #DSA #LeetCode #Day67 #Java #HexConversion #BitManipulation
To view or add a comment, sign in
-
-
#day334 of #1001daysofcode problem statement (1009): Complement of Base 10 Integer 💡 Approach: To find the complement of a number, I converted it into binary and flipped every bit (0 → 1 and 1 → 0). After flipping the bits, the resulting binary string was converted back to decimal. Example: 5 → Binary: 101 Complement: 010 → 2 ⏱ Time Complexity: O(log n) 🧠 Space Complexity: O(log n) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
🚀 Day 38 of #100DaysOfCode 🌱 Topic: Linked List / Recursion ✅ Problem Solved: LeetCode 24 – Swap Nodes in Pairs 🛠 Approach: Used a recursive approach to swap every two adjacent nodes in the linked list. If the list has 0 or 1 node, return it directly (base case). Store the second node (head.next) as a temporary node. Recursively swap the remaining list starting from temp.next. Adjust pointers so the second node becomes the new head of the pair. Connect the swapped pair with the recursively processed list. This swaps nodes without modifying their values, only changing pointers. #100DaysOfCode #Day38 #DSA #LinkedList #Recursion #LeetCode #Java #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 15/100 – LeetCode Challenge Problem: Reverse Linked List Today’s problem focused on reversing a singly linked list using an iterative approach. Approach: Used three pointers to reverse the links: prev → keeps track of the previous node curr → current node being processed next → stores the next node before changing the link Steps: Store curr.next in next Reverse the link → curr.next = prev Move prev and curr one step forward Continue until the list ends. Finally, prev becomes the new head of the reversed list. Complexity: Time: O(n) Space: O(1) Concepts Practiced: Linked List pointer manipulation Iterative reversal technique In-place algorithm #100DaysOfCode #LeetCode #DSA #Java #LinkedList #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Leetcode Problem | | Check If a String Contains All Binary Codes (1461) 🚀 Problem: Given a binary string s and integer k, check if all possible binary codes of length k exist in the string. Example: s = "00110", k = 2 Expected Output = true In substring and sliding window problems, boundary conditions matter more than logic sometimes. Consistent debugging > frustration. #DSA #Java #LeetCode #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
#day327 of #1001daysofcode problem statement (1582): Special Positions in a Binary Matrix -First counted the number of 1s in each row and column, then identified positions where both counts were exactly one. -Brute force tc=O(m*n*(m+n)), sc=O(1) -Reduced repeated checks and brought the solution down to O(m × n) but it costs some space. sc=O(m+n) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
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