❄️ takeUforward — Day 38 ✅ A Number After a Double Reversal | Number Manipulation | LeetCode Today I worked on a number manipulation problem involving reversing digits twice and checking if the result equals the original number. 💡 Problem Statement Given an integer "num", reverse the number twice and check whether the final value is equal to the original number. 🧠 Approach • Store the original number. • Reverse the number using modulo ("% 10") and division ("// 10"). • Reverse the result again. • Compare the final value with the original number. 🐍 Python Code class Solution: def isSameAfterReversals(self, num: int) -> bool: orginal = num sum_ = 0 sum2 = 0 while num > 0: last_digit = num % 10 sum_ = (sum_ * 10) + last_digit num //= 10 while sum_ > 0: lsd = sum_ % 10 sum2 = (sum2 * 10) + lsd sum_ //= 10 return True if sum2 == orginal else False ⏱ Complexity • Time: O(d) — where d is number of digits • Space: O(1) 📌 Key Learning Reversing numbers using digit extraction helps build strong fundamentals in number-based algorithms and edge case handling. 🚀 Day 38 completed — improving number manipulation skills step by step. #Python #DSA #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #100DaysOfCode
Yaswanth Kumar D’s Post
More Relevant Posts
-
Day 9: Mastering the Two-Sum Strategy 🎯 💡 How I solved it: Instead of a slow O(n^2) nested loop to find pairs, I used a One-Pass Hash Map to achieve a lightning-fast O(n) solution. Target Calculation: For every number n, I calculated the diff (target - n) needed to complete the pair. Instant Lookup: I checked if this diff already existed in my prev_map. If it did, I immediately returned the indices. Dynamic Mapping: If the complement wasn't found, I stored the current number and its index {val: index} in the map to be used for future lookups. The Single Pass: This allowed me to find the answer in just one trip through the array. 🧠 Key Takeaway: Efficiency First: Trading a tiny bit of memory O(n) space for a massive gain in speed O(n) time is the hallmark of an optimized algorithm. Complement Logic: Learned that searching for a "pair" is really just searching for a "complement." Dictionary Power: This project reinforced how Python’s dictionary lookups are O(1) on average, making them the ultimate tool for reducing time complexity. One step closer to mastering Data Structures and Algorithms! 💻🔥 The logic is getting sharper every day! 📈🤝 #100DaysOfCode #DSA #Python #TwoSum #ProblemSolving #StriverA2ZSheet #CodingJourney
To view or add a comment, sign in
-
-
🚀Day 12 of #75DaysofLeetcode LeetCode #11 – Container With Most Water (Medium) Today I solved the classic Two Pointer problem: Container With Most Water. 🔎 Problem Summary: Given an array height, each element represents a vertical line. The goal is to choose two lines such that together with the x-axis they form a container that holds the maximum amount of water. 💡 Key Insight: Instead of checking all pairs (O(n²)), we can use the Two Pointer Technique: ✔ Start with one pointer at the beginning and one at the end ✔ Calculate the container area using area = min(height[left], height[right]) * (right - left) ✔ Move the pointer pointing to the smaller height ✔ Keep track of the maximum area ⚡ Optimal Complexity: ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 💻 Python Implementation: from typing import List class Solution: def maxArea(self, height: List[int]) -> int: left, right = 0, len(height) - 1 max_water = 0 while left < right: area = min(height[left], height[right]) * (right - left) max_water = max(max_water, area) if height[left] < height[right]: left += 1 else: right -= 1 return max_water 📚 This problem reinforced how two pointers can reduce a brute force problem from O(n²) to O(n). Consistency in solving problems daily is slowly improving my problem-solving skills and algorithmic thinking. 💪 #LeetCode #DSA #Python #TwoPointers #ProblemSolving #CodingPractice #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 29/181 — Optimization Mindset Today I revisited Group Anagrams and focused on improving my approach. ✅ Step 1 — Used defaultdict Learned the importance of defaultdict(list): Automatically initializes missing keys Cleaner and more Pythonic Reduces conditional checks ✅ Step 2 — Optimized the Solution Approach 1: Sorted each word and used it as a key. Time Complexity: O(N × K log K) Approach 2 (Optimized): Used character frequency count (26-size array) as the key. Converted it to a tuple for hashing. Time Complexity improved to: O(N × K) 🎯 Key Takeaway Solving a problem is good. Optimizing it is better. Understanding why it improves is best. One month of consistency completed. On to stronger patterns 💪🔥 #DSA #Optimization #HashMap #Python #LeetCode #CodingJourney #Consistency
To view or add a comment, sign in
-
🚀 Solved the 4Sum Problem Today Today I worked on the classic 4Sum problem. 👉 Given an array of integers and a target value, find all unique quadruplets that sum to the target. At first, it feels like a simple extension of 2Sum or 3Sum. But the real challenge is: Handling duplicates correctly Managing multiple pointers carefully Avoiding unnecessary computations 💡 My Approach Instead of brute forcing all combinations (which would be O(n⁴)): Sort the array Fix the first two elements using loops Use the two-pointer technique for the remaining two elements Skip duplicates at every level This reduces the complexity significantly. ⏱ Complexity Time Complexity: O(n³) Space Complexity: O(1) (excluding output) The optimization comes from: Sorting Avoiding repeated combinations Using structured pointer movement python code : https://lnkd.in/gfqZZdJB 🧠 My Learning 4Sum is not about writing four nested loops. It’s about recognizing patterns: 2Sum → Two pointers 3Sum → Fix one + Two pointers 4Sum → Fix two + Two pointers Once you see the pattern, the problem becomes structured. It reminded me that many “hard” problems are just extensions of simpler ones. Break them down. Reduce dimensions. Control duplicates. That’s the game. One problem at a time. 💪 #DataStructures #Algorithms #CodingInterview #LeetCode #ProblemSolving #LearningInPublic #4Sum Rajan Arora
To view or add a comment, sign in
-
-
Some instructions are used more than once. When working with data, certain operations tend to repeat. Cleaning a value. Checking a condition. Transforming a piece of information. Applying the same rule across different parts of a dataset. Writing the same set of instructions every time would quickly make code longer and harder to follow. This is where functions come in. A function is simply a way of grouping a set of instructions under a name so that the same logic can be used again whenever it is needed. Instead of rewriting the steps, the program calls the function and runs those instructions again. For example: def check_pass(score): if score >= 50: return "Pass" return "Fail" Once defined, the same logic can be applied wherever it is needed. check_pass(72) check_pass(43) The instructions stay the same. Only the input changes. Functions don’t introduce new logic. They organize existing logic so it can be reused clearly and consistently. And in larger programs, that organization becomes just as important as the logic itself. Day 28 / 30. #30DaysOfDataScience #Python #Functions #ProgrammingLogic #LearningInPublic
To view or add a comment, sign in
-
-
𝐖𝐡𝐲 𝐱 𝐢𝐧 𝐬𝐞𝐭 𝐟𝐞𝐞𝐥𝐬 𝐢𝐧𝐬𝐭𝐚𝐧𝐭… 𝐛𝐮𝐭 𝐱 𝐢𝐧 𝐥𝐢𝐬𝐭 𝐝𝐨𝐞𝐬𝐧’𝐭 👇 𝑻𝒘𝒐 𝒍𝒊𝒏𝒆𝒔 𝒕𝒉𝒂𝒕 𝒍𝒐𝒐𝒌 𝒕𝒉𝒆 𝒔𝒂𝒎𝒆: ---> x in my_list ---> x in my_set Both answer a simple question: “Is x present?” But their time complexity is very different. 🧾 𝐋𝐢𝐬𝐭𝐬 → 𝐥𝐢𝐧𝐞𝐚𝐫 𝐬𝐞𝐚𝐫𝐜𝐡 (𝐎(𝐧)) 𝑨 𝒍𝒊𝒔𝒕 𝒊𝒔 𝒍𝒊𝒌𝒆 𝒄𝒉𝒆𝒄𝒌𝒊𝒏𝒈 𝒂 𝑵𝒐𝒕𝒆𝒃𝒐𝒐𝒌: You start at page 1… then page 2… then page 3… Worst case: you check every element. nums = [3, 8, 2, 10, 7] 7 in nums # Python checks one-by-one So membership in a list is 𝑶(𝒏) — time grows with size. ⚡ 𝐒𝐞𝐭𝐬 → 𝐡𝐚𝐬𝐡𝐢𝐧𝐠 (𝐚𝐯𝐞𝐫𝐚𝐠𝐞 𝐎(1)) 𝑨 𝒔𝒆𝒕 𝒊𝒔 𝒎𝒐𝒓𝒆 𝒍𝒊𝒌𝒆 𝒂 𝒍𝒐𝒄𝒌𝒆𝒓 𝒔𝒚𝒔𝒕𝒆𝒎. Instead of scanning all elements, Python: Computes a hash of the value Jumps directly to its bucket nums = {3, 8, 2, 10, 7} 7 in nums # direct lookup No scanning. Just direct access → 𝐚𝐯𝐞𝐫𝐚𝐠𝐞 𝐎(1). 𝐈𝐦𝐩𝐨𝐫𝐭𝐚𝐧𝐭 𝐝𝐞𝐭𝐚𝐢𝐥 Set lookup is O(1) on average, not magic. If many values collide into the same bucket (rare but possible), it can degrade — but Python handles hashing well, so in practice it stays near constant time. 𝐏𝐫𝐚𝐜𝐭𝐢𝐜𝐚𝐥 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Use a list when: -order matters -duplicates matter -small dataset Use a set when: -you need fast membership checks -uniqueness matters -solving frequency / existence problems 𝑾𝒉𝒂𝒕 𝒔𝒎𝒂𝒍𝒍 𝑷𝒚𝒕𝒉𝒐𝒏 𝒅𝒆𝒕𝒂𝒊𝒍 𝒔𝒂𝒗𝒆𝒅 𝒚𝒐𝒖 𝒕𝒉𝒆 𝒎𝒐𝒔𝒕 𝒅𝒆𝒃𝒖𝒈𝒈𝒊𝒏𝒈 𝒕𝒊𝒎𝒆? #Python #DataStructures #Algorithms #DSA #CodingTips #LearningInPublic #YogeshLearns
To view or add a comment, sign in
-
🚀 Day 8/30 | LeetCode Problem: Merge Sorted Array (88) Problem: You are given two sorted arrays nums1 and nums2, and two integers m and n representing the number of initialized elements in each array. Merge nums2 into nums1 as one sorted array. The final result should be stored inside nums1. 💡 Approach Since nums1 already has enough space at the end (filled with 0s), Steps: Copy elements of nums2 into the empty positions of nums1 Sort the entire nums1 array This ensures the final array remains sorted. ⏱ Complexity Time Complexity: O((m+n) log(m+n)) → due to sorting Space Complexity: O(1) → in-place modification 🧠 Python Code class Solution: def merge(self, nums1, m, nums2, n): for i in range(n): nums1[m + i] = nums2[i] nums1.sort() return nums1 📌 Example Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] 🎯 Key Takeaway Sometimes a straightforward solution works perfectly. Optimization (like using two pointers from the end) can further improve time complexity — but clarity first, then optimization. ✅ Accepted | 100% Runtime 🔖 Hashtags #LeetCode #30DaysOfLeetCode #Day8 #Python #Arrays #Sorting #DataStructures #Algorithms #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
I wasted 3 hours thinking my code was broken. It wasn't. The GIL was just laughing at me. I had this Python script doing heavy data processing. Added multi-threading to speed it up. Ran it. Expected magic. Got... the exact same speed. Maybe even slower. I checked my logic. Checked my threads. Googled everything. Then finally someone in a forum said — "Bro, that's just the GIL." So what even IS the GIL? In simple words — Python has this rule that says only one thread can run at a time, no matter how many cores your machine has. It's called the Global Interpreter Lock. It exists to keep Python's memory safe, but it also means your threads aren't actually running in parallel when doing heavy computation. I felt cheated honestly. But here's what actually helped me: → Switched to multiprocessing — each process has its own GIL. Problem solved. → For API calls and I/O stuff? asyncio is your best friend. → Libraries like NumPy actually release the GIL during computation. Smart. → Python 3.13 is experimenting with making the GIL optional. The future looks good. The GIL isn't evil. It's just something nobody tells you about when you're starting out — and you only discover it when you're sitting there confused at 2am wondering why your "optimized" code is still slow. If this saves even one person that 3-hour spiral, this post was worth it. Have you hit the GIL wall before? What did you do? Let me know below #Python #PythonDeveloper #Programming #LearnPython #SoftwareEngineering #CodingLife #TechCommunity
To view or add a comment, sign in
-
-
🚀 Day 45 of #100DaysOfCode ✅ Solved: Binary Gap (LeetCode 868) Today’s problem was about finding the longest distance between two consecutive 1s in the binary representation of a number. 🔎 Problem Insight: Given a positive integer n, convert it into binary and calculate the maximum distance between adjacent 1s. 💡 Key Idea: Instead of converting the number into a string, we can directly use bit manipulation: Traverse each bit using n & 1 Track the position of the previous 1 Update the maximum distance whenever a new 1 is found ⚡ Optimized Python Solution (O(log n) time | O(1) space) Python Copy code class Solution: def binaryGap(self, n: int) -> int: last = -1 max_dist = 0 position = 0 while n > 0: if n & 1: if last != -1: max_dist = max(max_dist, position - last) last = position n >>= 1 position += 1 return max_dist 🧠 What I Learned: Bitwise operations can make solutions more efficient Always think beyond string conversion for binary problems Tracking positions smartly reduces extra space usage Consistency > Motivation 🔥 One problem at a time towards mastering Data Structures & Algorithms. #LeetCode #ProblemSolving #Python #BitManipulation #DSA
To view or add a comment, sign in
-
-
Day 6 was the most hands-on day yet. I stopped looking at Python as a collection of rules and started using it as a high-powered filter for data. Here is how Day 6 changed my perspective on Algorithms and Strings: 🔹 The Accumulator Pattern: I learned how to make a loop remember things. Whether it’s counting occurrences, summing up values, or finding the average, it’s all about maintaining a state while the loop churns through data. 🔹 The Search Party: I built logic to find the largest and smallest values in a set. Realizing that Smallest is tricky—you have to be careful with how you initialize your variables, or your starting "zero" might accidentally become your answer. 🔹 Strings are Collections: I used to think of a word as just "text." Now I see it as a sequence. I’ve learned to Slice strings to grab exactly what I need, Strip away the "noise" (whitespace), and use Parsing to extract specific data from a messy block of text. 🔹 The "In" Operator: Python’s readability shines here. Using if 'search_term' in text: feels like writing English, but it’s actually a powerful logical tool for filtering information instantly. Next up: File Handling. I’m moving from typing data manually into the console to letting Python read and analyze entire documents for me. 📂 #Python #DataAnalysis #CodingJourney #BuildInPublic #SoftwareLogic #Algorithms #StringManipulation
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
great!