Day 46 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to move all zeros in an array to the end while maintaining the order of non-zero elements. This is a common problem that helps in understanding the two-pointer technique and in-place array manipulation. What the program does: • Takes an array as input • Moves all zero elements to the end • Maintains the relative order of non-zero elements • Performs the operation in-place (no extra space) How the logic works: • Initialize a pointer non_zero_ptr to track the position of the next non-zero element • Traverse the array from left to right • If the current element is non-zero: – Place it at non_zero_ptr position – Increment non_zero_ptr • After placing all non-zero elements, fill the remaining positions with zeros • Return the modified array Example: Input: [0, 1, 0, 3, 12] [0, 0, 1, 0, 3, 0, 0, 12, 0] [1, 2, 3, 4, 5] Output: [1, 3, 12, 0, 0] [1, 3, 12, 0, 0, 0, 0, 0, 0] [1, 2, 3, 4, 5] Why this approach is efficient: – Uses two-pointer technique – No extra space required (in-place) – Time Complexity: O(n) Key learnings from Day 46: – Understanding two-pointer approach – Performing in-place array operations – Maintaining order while modifying arrays – Writing optimized and clean code #100DaysOfCode #Day46 #Python #PythonProgramming #TwoPointers #Arrays #Algorithms #DataStructures #ProblemSolving #CodingPractice #InterviewPrep #LearnByDoing #ProgrammingJourney #DeveloperGrowth #BTech #CSE #AIandML #VITBhopal #TechJourney
Python Array Manipulation: Moving Zeros to End
More Relevant Posts
-
Day 47 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find a peak element in an array. A peak element is an element that is greater than or equal to its neighbors. What the program does: • Takes an array as input • Finds any peak element in the array • Handles edge cases (first and last elements) • Returns the peak value How the logic works: • If the array is empty → return None • If it has only one element → return that element • Check the first element: – If it is greater than or equal to the next element → it's a peak • Check the last element: – If it is greater than or equal to the previous element → it's a peak • Traverse the array from index 1 to n-2: – If an element is greater than or equal to both neighbors → return it • If no peak is found (rare case), return None Example: Input: [1, 3, 20, 4, 1, 0] Output: 20 Another example: Input: [1, 2, 3, 4, 5] Output: 5 Another example: Input: [5, 4, 3, 2, 1] Output: 5 Why this approach works: – Checks boundary conditions properly – Works for increasing and decreasing arrays – Time Complexity: O(n) Key learnings from Day 47: – Handling edge cases in arrays – Comparing neighboring elements – Writing clean traversal logic – Strengthening problem-solving skills #100DaysOfCode #Day47 #Python #PythonProgramming #Arrays #Algorithms #ProblemSolving #CodingPractice #DataStructures #InterviewPrep #LearnByDoing #DeveloperGrowth #ProgrammingJourney #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 35/60 — LeetCode Discipline Problem Solved: Remove Nth Node From End of List Difficulty: Medium Today’s challenge was about linked lists and efficient traversal. Instead of calculating the length first, I used the two-pointer approach to remove the target node in a single pass. 💡 Focus Areas: • Two-pointer technique (fast & slow pointers) • Linked list traversal optimization • Handling edge cases (removing head node) • Use of dummy node for cleaner logic • Writing efficient O(n) solutions ⚡ Performance Highlight: Achieved 0 ms runtime (100% performance) Two pointers… one moving ahead, one following behind— like time and memory walking together. And at the right moment… one node quietly disappears, as if it was never meant to stay. #LeetCode #60DaysOfCode #100DaysOfCode #DSA #LinkedList #TwoPointers #CodingJourney #ProblemSolving #Python #Developers #TechGrowth #Consistency
To view or add a comment, sign in
-
-
🚀 #Day59 - #geekstreak60 🔹 Problem Solved: Remove Spaces Today’s problem was simple yet important for understanding string manipulation and clean coding practices. 🧩 Problem Statement: Given a string, remove all the spaces and return the modified string while maintaining the order of characters. 💡 Approach: Instead of iterating manually, I used Python’s built-in string method to efficiently remove spaces in one line. 🧠 Key Takeaways: ✔️ Built-in methods can simplify code significantly ✔️ String manipulation is a fundamental skill in DSA ✔️ Always look for optimized and readable solutions ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) #DSA #Python #CodingJourney #100DaysOfCode #ProblemSolving #LearningEveryday GeeksforGeeks National Payments Corporation Of India (NPCI)
To view or add a comment, sign in
-
-
🚀 Day 7 – LeetCode Journey Today’s problem: String to Integer (atoi) This one really tested my understanding of edge cases and string parsing. Not just coding, but thinking like a machine step-by-step 👇 ✅ Ignored leading whitespaces ✅ Handled positive & negative signs ✅ Extracted numbers until non-digit appears ✅ Managed overflow within 32-bit integer range At first, it looked simple… but the edge cases made it interesting 😅 💡 Key Learning: Writing code is one thing, but handling all possible inputs correctly is what truly matters in real-world problems. Slowly getting better at breaking down problems and building clean logic 💻🔥 On to Day 8… 🚀 #Day7 #LeetCode #CodingJourney #Python #ProblemSolving #Consistency #LearningEveryday
To view or add a comment, sign in
-
-
✅ Day 92 of 100 Days LeetCode Challenge Problem: 🔹 #2011 – Final Value of Variable After Performing Operations 🔗 https://lnkd.in/gX-JQNUJ Learning Journey: 🔹 Today’s problem was about evaluating a sequence of increment and decrement operations. 🔹 I initialized a variable ans = 0 to track the value. 🔹 Used a hashmap to map each operation to its effect: • "++X" and "X++" → +1 • "--X" and "X--" → -1 🔹 Iterated through the operations and updated ans accordingly. 🔹 Returned the final computed value. Concepts Used: 🔹 HashMap / Dictionary 🔹 String Matching 🔹 Simple Simulation Key Insight: 🔹 Instead of using multiple condition checks, mapping operations to values simplifies logic and improves readability. Complexity: 🔹 Time: O(n) 🔹 Space: O(1) #LeetCode #Algorithms #DataStructures #CodingInterview #100DaysOfCode #Python #ProblemSolving #LearningInPublic #TechCareers
To view or add a comment, sign in
-
-
Day 58 of my #100DaysOfCode challenge 🚀 Today I implemented a Python program to generate prime numbers within a given range. This is a practical extension of prime checking and useful in many DSA and real-world problems. What the program does: • Takes a range (start, end) as input • Checks each number in the range • Identifies whether it is prime or not • Returns a list of all prime numbers in that range Example Output: Prime numbers between 1 and 50: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] How the logic works: Start from max(2, start) For each number: • Assume it is prime • Check divisibility from 2 → √n If divisible → not prime If not divisible → add to result list 👉 Uses square root optimization for better performance Why this is important: – Builds on prime number fundamentals – Useful in: Competitive programming Number theory problems Range-based queries – Helps understand optimization using √n Time Complexity: O(n√n) Space Complexity: O(k) (number of primes) Key Takeaways: – Applying optimized prime checking – Working with ranges and loops – Improving efficiency using √n – Writing clean and scalable code #100DaysOfCode #Day58 #Python #Programming #DSA #Algorithms #PrimeNumbers #NumberTheory #CodingPractice #ProblemSolving #InterviewPrep #Optimization #DeveloperJourney #Consistency #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
🚀 Just solved the “Valid Number” problem on LeetCode! This problem looks simple at first glance—but handling edge cases like decimals, signs, and exponents makes it a great test of attention to detail and logical thinking. ✅ Key takeaways: Careful handling of edge cases is crucial Validating input step-by-step can simplify complex parsing problems Writing clean, readable logic beats overcomplicated solutions 💡 Performance: ⚡ Runtime: 3 ms 🧠 Efficient space usage ✅ All test cases passed Problems like this remind me that consistency in practice is what builds strong problem-solving skills. On to the next one! 🔥 #LeetCode #Coding #Python #ProblemSolving #SoftwareEngineering #AIEngineerJourney link of #Solution :- https://lnkd.in/ga9b5pVb
To view or add a comment, sign in
-
-
𝐈 𝐰𝐫𝐨𝐭𝐞 𝐭𝐡𝐞 𝐬𝐚𝐦𝐞 𝐥𝐢𝐧𝐞 𝟕 𝐭𝐢𝐦𝐞𝐬 𝐛𝐞𝐟𝐨𝐫𝐞 𝐥𝐞𝐚𝐫𝐧𝐢𝐧𝐠 𝐭𝐡𝐢𝐬 😩 A function is just a named block of code you can run anytime. def = define. then call it by name. 𝑾𝒉𝒚 𝒖𝒔𝒆 𝒕𝒉𝒆𝒎? → Write once, use many times → Easier to fix bugs (one place) → Cleaner code that humans can read 𝑲𝒆𝒚 𝒇𝒆𝒂𝒕𝒖𝒓𝒆𝒔: → 𝑷𝒂𝒓𝒂𝒎𝒆𝒕𝒆𝒓𝒔: Values you pass into a function so it can work with different data each time. → 𝙍𝙚𝙩𝙪𝙧𝙣: The value a function sends back to you after it finishes its job. → 𝘿𝙚𝙛𝙖𝙪𝙡𝙩 𝙖𝙧𝙜𝙪𝙢𝙚𝙣𝙩𝙨:Fallback values that a function uses if you don't pass anything for that parameter. → 𝘿𝙤𝙘𝙨𝙩𝙧𝙞𝙣𝙜𝙨: A short note inside a function that explains what it does (for you and others reading the code). 𝙁𝙪𝙡𝙡 𝙘𝙤𝙙𝙚 + 𝙚𝙭𝙖𝙢𝙥𝙡𝙚𝙨: 🔗 Vist my Github :https://lnkd.in/drGxebiM 𝑶𝒗𝒆𝒓 𝒕𝒐 𝒚𝒐𝒖: 👇 Type "GUILTY" if you've ever written the same loop 4 times in one script.😅 #python #AiEngineer #coding
To view or add a comment, sign in
-
-
Day 59 of my #100DaysOfCode challenge 🚀 Today I implemented a Python program to count the number of set bits (1s) in a binary number. This uses an efficient bit manipulation technique and is very important in DSA & low-level optimization. What the program does: • Takes an integer n as input • Converts it conceptually to binary • Counts the number of set bits (1s) • Uses an optimized approach instead of checking each bit Example Outputs: 29 (11101) → 4 set bits 7 (111) → 3 set bits 16 (10000) → 1 set bit How the logic works: Uses Brian Kernighan’s Algorithm: n = n & (n - 1) • This removes the rightmost set bit in each step • Repeat until n = 0 • Count how many times this operation runs Why this is important: – Much faster than checking every bit ⚡ – Used in: Bit manipulation problems Competitive programming Low-level optimizations – Common in coding interviews Time Complexity: O(number of set bits) Space Complexity: O(1) Key Takeaways: – Understanding bitwise operations – Efficient counting techniques – Writing optimized solutions – Learning real-world low-level logic #100DaysOfCode #Day59 #Python #Programming #DSA #Algorithms #BitManipulation #Binary #CodingPractice #ProblemSolving #InterviewPrep #Optimization #DeveloperJourney #Consistency #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 16 Task – Python Mini Challenge Today’s task was a simple yet powerful exercise to strengthen my looping and conditional logic skills. 🔹 Task: Given a list of numbers, calculate the sum of even and odd numbers separately. 🔹 What I implemented: ✔️ Method 1: Stored even & odd numbers in separate lists and used sum() ✔️ Method 2: Calculated sums directly using variables (more optimized approach) 🔹 Key takeaway: There’s always more than one way to solve a problem. Writing multiple approaches helps in understanding efficiency and clean coding practices. 🔹 What I learned deeply: Using loops effectively Applying conditional logic (if-else) Writing optimized solutions instead of relying only on extra space github link : https://lnkd.in/g4iZcnGt 📌 Completed the task and tested it with different inputs successfully. Building consistency, one problem at a time 💪🚀 #Python #100DaysOfCode #Coding #ProblemSolving #DeveloperJourney #LearnInPublic Codegnan BhanuTeja Garikapati
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