Day 23 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Check if a Number is an Abundant Number We were given an integer n. We had to check if it is an abundant number. An abundant number means: The sum of its proper divisors is greater than the number itself. Example: 12 → divisors: 1, 2, 3, 4, 6 Sum = 16 Since 16 > 12, it is an abundant number. 💻 Brute Force Approach 🔹️Traverse from 1 to n. 🔹️Check if i divides n. 🔹️If yes, add it to sum. 🔹️After traversal, compare sum with n. 🔹️If sum > n → abundant number. Time Complexity: O(n) Space Complexity: O(1) Works. But unnecessary checks happen. 💻 Optimized Approach 🔹️Traverse from 1 to √n. 🔹️If i divides n, add i to sum. 🔹️Also add n / i as paired divisor. 🔹️Avoid adding duplicates when i = n/i. 🔹️Finally compare sum with n. Time Complexity: O(√n) Space Complexity: O(1) Much fewer iterations. 📚 What I learned today: ▫️Divisors always appear in pairs. ▫️Checking till √n is enough for divisor problems. ▫️Avoid counting duplicate divisors for perfect squares. ▫️Small observations can reduce complexity a lot. Day 23 completed. #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
Checking Abundant Numbers: Brute Force vs Optimized Approach
More Relevant Posts
-
🚀 Day 7/100 — LeetCode Challenge Solved Search in Rotated Sorted Array II At first, it feels similar to the previous rotated array problem, but duplicates make it harder to decide which half is sorted. 💡 Key idea: 1) Normally, one half is always sorted 2) But with duplicates, we can get stuck when low == mid == high 3) In that case, we shrink the search space from both ends 4) Otherwise, identify the sorted half and proceed like binary search 👉 Small change, but big impact on logic. 🧠 Time Complexity: Average: O(log n) Worst case: O(n) (due to duplicates) 💾 Space Complexity: O(1) Learning that small edge cases can completely change how an algorithm behaves. Staying consistent. #LeetCode #DSA #100DaysOfCode #Cpp #BinarySearch #CodingJourney
To view or add a comment, sign in
-
-
Day 28 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Convert Numbers to Words We were given a number. Task was to convert it into words. Example: 123 → One Hundred Twenty Three This problem was more about mapping. And understanding place values. 💻 Approach 🔹️Create mappings for digits 0–9. 🔹️Create mappings for 10–19 (special cases). 🔹️Create mappings for tens like 20, 30, 40... 🔹️Also store place values like hundred and thousand. Then process the number step by step. 🔹️Traverse the number from left to right. 🔹️If more than two digits remain, handle place values. 🔹️If two digits remain and first digit is 1, use teen mapping. 🔹️Otherwise print tens and units separately. Continue until all digits are processed. 📊 Complexity Analysis Time Complexity: O(n) Each digit is processed once. Space Complexity: O(1) Only fixed mapping arrays are used. 📚 What I learned today: ▫️This problem is essentially a lookup + formatting problem using constant mapping tables. ▫️Handling special ranges (10–19) separately avoids incorrect word formation. ▫️Breaking the number into place-value segments (hundreds, tens, units) simplifies logic. ▫️Designing clean mapping structures is important for problems involving symbolic representation. Day 28 completed. Learning different problem patterns every day 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
🚀 𝐌𝐲 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐖𝐞𝐞𝐤𝐥𝐲 𝐂𝐨𝐧𝐭𝐞𝐬𝐭 𝟒𝟗𝟓 𝐄𝐱𝐩𝐞𝐫𝐢𝐞𝐧𝐜𝐞 Solved 2 out of 4 problems! Here's my approach breakdown: 🎯 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝟭: 𝗙𝗶𝗿𝘀𝘁 𝗠𝗮𝘁𝗰𝗵𝗶𝗻𝗴 𝗖𝗵𝗮𝗿𝗮𝗰𝘁𝗲𝗿 𝗙𝗿𝗼𝗺 𝗕𝗼𝘁𝗵 𝗘𝗻𝗱𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Find smallest index i where s[i] == s[n-i-1], return -1 if none. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: - Simple linear scan from left (i=0 to n/2) - For each i, check if s[i] matches s[n-i-1] - Return first matching index found - O(n) straightforward solution 📋𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝟮: 𝗗𝗲𝘀𝗶𝗴𝗻 𝗘𝘃𝗲𝗻𝘁 𝗠𝗮𝗻𝗮𝗴𝗲𝗿 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Priority queue-based event manager with priority updates and highest-priority polling. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: - Max-heap: {priority, -eventId} (negative ID for smallest ID tiebreaker) - HashMap: Track latest priority for each eventId - Lazy deletion in pollHighest(): 1. Pop top from heap 2. If priority outdated (doesn't match hashmap), discard and continue 3. Otherwise, valid event - return it and erase from map - Handles updates efficiently! 🔄 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝟯: 𝗦𝘂𝗺 𝗼𝗳 𝗦𝗼𝗿𝘁𝗮𝗯𝗹𝗲 𝗜𝗻𝘁𝗲𝗴𝗲𝗿𝘀 (𝗠𝗟𝗘 𝗜𝘀𝘀𝘂𝗲) 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Find all k where n%k==0 and nums can become sorted via k-length subarray rotations. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: - Check each divisor k of n - For each k-length group, verify if original subarray is rotation of sorted version - Rotation check: concatenate sorted subarray with itself, check if original appears as substring #LeetCode #WeeklyContest495 #CompetitiveProgramming #Algorithms #PriorityQueue #DataStructures #ProblemSolving #LazyDeletion #CodingChallenge #DailyCoding #CP #Contest #CompetetiveProgramming #leetcode #DSA #LearnInPublic
To view or add a comment, sign in
-
-
🚀 Day 8/100 — LeetCode Challenge Solved Find Minimum in Rotated Sorted Array At first, it looks tricky because the array is rotated. But the key is to identify which part is sorted. 💡 Approach: 1) If the left half is sorted → the minimum could be at low 2) Otherwise → the minimum lies in the right half 3) Keep narrowing the search space using binary search 👉 Instead of searching for a target, here we’re searching for the minimum element. 🧠 Time Complexity: O(log n) 💾 Space Complexity: O(1) 💡 What I’m realizing: Many problems look different, but they’re just variations of binary search with slight tweaks. Slowly building intuition around it. #LeetCode #DSA #100DaysOfCode #Cpp #BinarySearch #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 5/100 — LeetCode Challenge Solved Search in Rotated Sorted Array This problem looks like a normal binary search at first, but the rotation makes it tricky. 💡 Key idea: 1) At every step, one half of the array is always sorted 2) Identify the sorted half 3) Check if the target lies in that range 4) Narrow down the search space accordingly It’s all about modifying binary search with the right conditions. 🧠 Time Complexity: O(log n) 💾 Space Complexity: O(1) ⚠️ Couldn’t record the video today due to some issues, but didn’t want to break the consistency. Will be back with better content tomorrow. Consistency > perfection. #LeetCode #DSA #100DaysOfCode #Cpp #BinarySearch #CodingJourney
To view or add a comment, sign in
-
-
Day 38 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Print Bracket Number We were given a string str. It contains brackets. Task was to assign a number to each bracket. Each pair gets the same number. Based on order of opening. Let’s understand it simply. ◾️Imagine you are assigning IDs to tasks. ◾️Every time a new task starts, you give it a new ID. ◾️When that task ends, it keeps the same ID. ◾️Multiple tasks can be nested. ◾️But each one has its own number. That’s exactly how brackets behave. 💻 Approach (Using Stack) 🔹️Create an empty stack. 🔹️Initialize a counter = 0. 🔹️Traverse the string. 🔹️If opening bracket: ▪️Increase counter ▪️Push it into stack ▪️Print the counter 🔹️If closing bracket: ▪️Take top value from stack ▪️Print it ▪️Pop from stack Each pair gets same number. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) 📚 What I learned today: ▫️Stack helps in tracking nested structures. ▫️Assigning IDs during traversal simplifies pairing logic. ▫️LIFO nature perfectly matches bracket problems. ▫️Nested patterns are easier with stack-based thinking. Day 38 completed. Stack understanding getting stronger 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 29 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Reverse a String We were given a string. Task was simple. Reverse the characters. Example: "hello" → "olleh" Looks easy. But there are multiple ways to do it. 💻 Brute Force Approach 🔹️Create a new empty string. 🔹️Traverse the original string from the end. 🔹️Append each character to the new string. 🔹️Return the new reversed string. Time Complexity: O(n) Space Complexity: O(n) Because a new string is created. 💻 Optimal Approach (Two Pointers) 🔹️Convert the string into a character array. 🔹️Place one pointer at the start. 🔹️Place another pointer at the end. 🔹️Swap both characters. 🔹️Move pointers toward the center. Continue until they meet. Time Complexity: O(n) Space Complexity: O(1) No extra string needed. 📚 What I learned today: ▫️Two-pointer technique works well for reversal problems. ▫️In-place operations help reduce space complexity. ▫️String problems often become easier when treated as arrays. ▫️Pointer movement patterns appear in many algorithms. Day 29 completed. Small problem, but useful pattern 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 4/100 — LeetCode Challenge Today I practiced problems based on the Two Pointer technique, a powerful approach for solving array and string problems efficiently. Problems solved: • Valid Palindrome • Two Sum II (Input Array Is Sorted) 🧠 Concept: Two pointers starting from different ends of the array/string and moving toward each other. 💡 Key Learning: Two pointer techniques often help eliminate nested loops and reduce time complexity to O(n). 📂 Solutions Repository https://lnkd.in/gkFh2mPZ Understanding these patterns is helping me recognize more efficient ways to approach problems. #100DaysOfLeetCode #DSA #LeetCode #CodingChallenge #SoftwareEngineering
To view or add a comment, sign in
-
🚀 Day 6/100 — LeetCode Challenge Today's problem: 3Sum 🧠 Concept: Sorting + Two Pointers 💡 Key Idea: After sorting the array, fix one element and use two pointers to find the remaining pair that sums to the target while avoiding duplicate triplets. ⚡ Time Complexity: O(n²) 📂 Solutions Repository https://lnkd.in/gkFh2mPZ This problem was a great exercise in combining multiple techniques to reduce brute-force complexity. #100DaysOfLeetCode #DSA #LeetCode #CodingChallenge #SoftwareEngineering
To view or add a comment, sign in
-
Day 24 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Convert Binary to Decimal We were given a binary number. Task was to convert it into decimal. Binary uses base 2. Decimal uses base 10. So the idea is simple. Each binary digit represents a power of 2. Example: Binary 1011 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 8 + 0 + 2 + 1 = 11 💻 Brute Force Approach 🔹️Take the binary number as a string. 🔹️Start from the rightmost digit. 🔹️Maintain a base variable (starting from 1). 🔹️If the digit is '1', add base to result. 🔹️Multiply base by 2 after each step. 🔹️Continue till all digits are processed. Time Complexity: O(n) Space Complexity: O(1) 💻 Optimal Approach (Using Built-in Functions) Instead of manual conversion, we can use built-in functions. 🔹️Convert binary string directly to integer. 🔹️Specify base 2 during conversion. 🔹️Language handles the calculation internally. Time Complexity: O(n) Space Complexity: O(1) 📚 What I learned today: ▫️Binary numbers are just powers of 2. ▫️Right-to-left traversal helps in base calculations. ▫️Built-in functions can simplify many tasks. ▫️Understanding manual logic still matters for interviews. Day 24 completed. Bit manipulation basics getting clearer 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
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