🚀 Day 4 – DSA Daily Series Move Zeroes (LeetCode 283) using Python. 🧠 Problem Given an integer array nums, move all 0’s to the end of the array while maintaining the relative order of the non-zero elements. Important constraints: • The operation must be done in-place • No extra array should be created Example: Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] 💡 Approach Used the Two Pointer technique. • Maintain a pointer k to track the position for the next non-zero element • Traverse the array using index i • Whenever a non-zero element is found, swap it with the element at position k • Increment k This ensures: • Non-zero elements move to the front • Zeros automatically shift toward the end ⏱ Complexity Time Complexity: O(n) – single pass through the array Space Complexity: O(1) – in-place modification 🔎 Key Learning This problem helps strengthen: • Two Pointer pattern • In-place array manipulation • Maintaining relative order while rearranging elements Consistency is key when practicing DSA — solving one problem at a time. 🚀 #DSA #LeetCode #Python #Algorithms #ProblemSolving #CodingJourney
Move Zeroes in Array using Python (LeetCode 283)
More Relevant Posts
-
Tried solving a sliding window problem today in Python Found the maximum average subarray of size k without recalculating everything again and again — just shifting the window and updating the sum. Simple logic, but really powerful for optimization! nums = [1,12,-5,-6,50,3] n = len(nums) k = 4 max_avrg = float("-inf") window = sum(nums[:k]) max_avrg = window/k for j in range (k,n): window = window - nums[j-k] + nums[j] if max_avrg < window/k: max_avrg = window/k print(max_avrg) Learning DSA step by step #Python #DSA #SlidingWindow
To view or add a comment, sign in
-
🧠 Python Concept: List Comprehension Write powerful loops in one clean line. ❌ Traditional Way squares = [] for i in range(5): squares.append(i * i) print(squares) Output [0, 1, 4, 9, 16] ✅ Pythonic Way squares = [i * i for i in range(5)] print(squares) Same result, less code. ⚡ With Condition even_squares = [i * i for i in range(10) if i % 2 == 0] print(even_squares) Output [0, 4, 16, 36, 64] 🧒 Simple Explanation Imagine telling a robot: 👉 “Give me squares of numbers from 0–4.” 👉 Instead of repeating instructions, you give one rule. 👉 That rule = list comprehension. 💡 Why This Matters ✔ Shorter code ✔ Faster execution ✔ More readable loops ✔ Very Pythonic 🐍 Python often replaces multiple lines with a single elegant expression 🐍 List comprehensions are one of the most powerful examples of that philosophy. #Python #PythonTips #PythonTricks #AdvancedPython #List #ListComprehension #Tech #CleanCode #LearnPython #Programming #DeveloperLife #DailyCoding #100DaysOfCode
To view or add a comment, sign in
-
-
Python Clarity Series – Episode 19 Topic: map() vs Loop 📌 Two ways to apply a function to a list. Traditional loop: nums = [1,2,3,4] squares = [] for i in nums: squares.append(i*i) Pythonic way: nums = [1,2,3,4] squares = list(map(lambda x: x*x, nums)) 👉 map() applies a function to every element. Result: [1, 4, 9, 16] 💡 Clarity Thought: Loop → easy to understand map() → shorter and functional style Both are correct. Python gives multiple ways to solve problems. Understanding both improves coding flexibility. #PythonLearning #CodingEfficiency #FutureDevelopers
To view or add a comment, sign in
-
-
🚀 Day 51 of My Python Journey Today I solved Apply Discount to Prices on LeetCode. 🔍 Problem Overview: The problem gives a sentence containing words and prices (values starting with $). The task is to apply a given discount percentage to all valid prices and return the updated sentence. The updated prices must be formatted with exactly two decimal places. 🧠 Approach: • Split the sentence into individual words. • Check whether a word represents a valid price (starts with $ and the remaining characters are digits). • Convert the price to an integer and apply the discount formula. • Format the discounted price to two decimal places and rebuild the sentence. ⚡ Key Learnings: • Practiced string parsing and validation • Improved understanding of formatting numbers in Python • Strengthened problem-solving using conditional logic 📊 Complexity: • Time Complexity: O(n) • Space Complexity: O(n) Under the Guidance of : Rudra Sravan kumar and Manoj Kumar Reddy Parlapalli #Day51 #Python #LeetCode #DataStructures #Algorithms #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Simple way to understand vector search in RAG I made a small Python example using SentenceTransformers + FAISS to understand how retrieval works in RAG. What happens here: A few documents are converted into embeddings Those embeddings are stored in FAISS A user question is also converted into an embedding FAISS finds the most similar document chunks This is the basic idea behind RAG: store meaning as vectors, then retrieve the most relevant context before generation Very small code, but it explains a very important concept. Text → Embedding → Similarity Search → Relevant Chunks That is why vector databases are so important in RAG systems. #RAG #FAISS #Embeddings #AIEngineering #Python #LLM Code source: https://lnkd.in/g-cm4BB2
To view or add a comment, sign in
-
-
Most Python workflows rely on heuristics. They’re quick, intuitive, but usually not optimal. A simple greedy approach might get you a solution, but it often leaves efficiency, performance, and cost savings on the table. GAMSPy brings algebraic modeling into Python, so you can express constraints and objectives directly and solve for a true optimum. At PyConDE & PyData 2026, Justine Broihan and Muhammet Soyturk will walk through this using a classic operations example, and then extend it into machine learning. They'll cover: 🔸 How optimization compares to rule-based heuristics and 🔸 How it can be used to test ML models (e.g. minimal changes needed to trigger misclassification) 🔸 The Art of the Optimal: A Pythonic Approach to Complex Decision-Making 📍 April 14 · 16:30 📍 Platinum (2nd Floor) If you're building decision-making systems in Python, this is worth a look. More details 👉 https://lnkd.in/dyifGdVi #PyConDE #PyData #Optimization #GAMSPy #GAMS #Python
To view or add a comment, sign in
-
-
🔍 Day 36 of My Problem Solving Journey Today I explored one of the most important concepts in strings — Searching & Finding. In Python, we often use methods like: ✔️ find() → to get the index of a substring ✔️ in → to check existence ✔️ index() → similar to find but throws error if not found But instead of just using built-in functions, I tried to understand the internal logic behind searching 👇 Find the position of a substring without using built-in functions. 🧠 My Approach: Traverse the string Compare characters one by one Return index if match is found Return -1 if not found def my_find(s, sub): for i in r print(my_find("manuuuu", "u")) # Output: 3 ✨ What I learned: How searching works internally Difference between find() and index() Importance of logic building instead of relying on built-ins 📈 Small steps every day = Big growth over time! #Day36 #Python #CodingJourney #ProblemSolving #100DaysOfCode #Learning #Developers #Programming Rudra Sravan kumar
To view or add a comment, sign in
-
-
Finding Maximum Sum of k Consecutive Elements in Python Check out this simple sliding window trick to get the maximum sum of k consecutive numbers in an array. I used my own code to solve it: Python Copy code nums = [7,1,5,1,3,2] k = 3 n = len(nums) window = sum(nums[:k]) sums = window for i in range(k,n): window = window - nums[i-k] + nums[i] if sums < window: sums = window print(sums) How it works: Start with the sum of the first k numbers Slide the window by removing the first number and adding the next Keep track of the maximum sum Fast, simple, and efficient! #Python #DSA #Coding #Algorithms #LearnByDoing #DeveloperLife
To view or add a comment, sign in
-
While practicing Python today, I came across a small but useful difference between append() and extend() in lists, so I thought of sharing it. Both methods add values at the end of a list, but they work a little differently. append() → adds the entire element as one item extend() → adds each element separately Example: a = [1,2,3] a.append([4,5]) Output → [1,2,3,[4,5]] a = [1,2,3] a.extend([4,5]) Output → [1,2,3,4,5] Sometimes while practicing, these small differences become the best learning moments.
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