LeetCode POTD 💫: Description: You are given an integer array nums that represents a circular array. Your task is to create a new array result of the same size, following these rules: For each index i (where 0 <= i < nums.length), perform the following independent actions: - If nums[i] > 0: Start at index i and move nums[i] steps to the right in the circular array. Set result[i] to the value of the index where you land. - If nums[i] < 0: Start at index i and move abs(nums[i]) steps to the left in the circular array. Set result[i] to the value of the index where you land. - If nums[i] == 0: Set result[i] to nums[i]. Return the new array result. Note: Since nums is circular, moving past the last element wraps around to the beginning, and moving before the first element wraps back to the end. Here's my solution: https://lnkd.in/gczPri7j #Python #DSA #Leetcode #DailyChallenge #Easy
LeetCode Daily Challenge: Circular Array Movement
More Relevant Posts
-
🚀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 9/30 | LeetCode Problem: Binary Search (704) Problem: Given a sorted array of integers nums and a target, return the index if the target exists. If not, return -1. 🧠 Approach: Binary Search Since the array is already sorted, we can apply Binary Search: Use two pointers: l (left) and r (right) Find the middle element Compare target with nums[mid] Reduce the search space by half each time This makes the solution very efficient. ⏱️ Complexity Time: O(log n) Space: O(1) 🧾 Python Code class Solution(object): def search(self, nums, target): l = 0 r = len(nums) - 1 while l <= r: mid = (l + r) // 2 if target == nums[mid]: return mid elif target < nums[mid]: r = mid - 1 else: l = mid + 1 return -1 ✅ Result Accepted ✅ Runtime: 0 ms (Beats 100%) Memory efficient 🎯 Key Learning Binary Search is a foundational algorithm used in: Search problems Optimization problems Interview questions Mastering it is essential for every developer. 🔖 Hashtags #LeetCode #30DaysOfLeetCode #Day9 #BinarySearch #Python #DSA #ProblemSolving #CodingJourney #SoftwareEngineering
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 23 of my #100DaysOfCode challenge 🚀 Today I implemented Linear Search in Python. Linear Search is the simplest searching algorithm where we check each element one by one until the target element is found. What the program does: • Takes a list and a target value • Iterates through the list sequentially • Returns the index if the target is found • Returns -1 if the target does not exist How the logic works: 1)Define a function linear_search(arr, target) 2)Use a for loop to iterate through the list using indices 3)Compare each element with the target value 4)If a match is found, return the current index 4)If the loop completes without finding the target, return -1 Example: Input: List:[10, 20, 30, 40, 50, 60, 70, 80, 90] Search Target 1: 50 Search Target 2: 100 Output: Target 50 found at index: 4 Target 100 not found in the list. Why Linear Search matters: – Works on both sorted and unsorted arrays – Simple and easy to implement – Time Complexity: O(n) Key learnings from Day 23: – Understanding basic searching algorithms – Comparing Linear Search vs Binary Search – Strengthening fundamentals of iteration – Improving algorithmic thinking #100DaysOfCode #Day23 #Python #PythonProgramming #LinearSearch #Algorithms #DataStructures #ProblemSolving #CodingPractice #LearnByDoing #ComputerScience #InterviewPrep #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
Weekly challenge 5: Recursion To understand recursion, you must first understand recursion. For Week 5 of my algorithm challenge, I decided to tackle a concept that trips up many beginners: Recursive Functions, using the classic Factorial problem. What is Recursion? Instead of using a standard `for` or `while` loop, a recursive function calls **itself** to solve a smaller piece of the problem. It keeps digging deeper until it hits a "Base Case" (the bottom), and then it passes the answers back up the chain. Think of it like a set of Russian nesting dolls. The Trade-off:** > While recursive code is extremely clean and mathematical, it uses more memory. Every time the function calls itself, it adds a new layer to the computer's **Call Stack**. If you forget your Base Case, your program crashes with a "Stack Overflow"! > > I added a visual trace to my Python script so you can literally see the Call Stack growing and shrinking in the console. Check the full code and console output on GitHub: https://lnkd.in/es5TzCUg #Python #Recursion #Algorithms #CodingChallenge #SoftwareEngineering #DataScience
To view or add a comment, sign in
-
🚀 Day 27/60 – Contains Duplicate II | Hashing | O(n) Solution Today I solved Contains Duplicate II using a HashMap (Dictionary) approach. 🧠 Problem Summary: Given an integer array nums and an integer k, determine whether there exist two distinct indices i and j such that: nums[i] == nums[j] |i - j| ≤ k 💡 Approach: Instead of using a brute-force O(n²) solution, I used Hashing to optimize the problem to O(n) time complexity. 🔹 Store value → last seen index in a dictionary 🔹 While iterating: If the value already exists, Check if index difference ≤ k If yes → return True Otherwise update index ⚡ Why Hashing? Dictionary lookup in Python takes O(1) average time. This helps reduce nested loop comparisons and improves efficiency significantly. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(n) 🎯 Key Learning: Always think how to reduce O(n²) to O(n) Hashing is extremely powerful for duplicate / lookup problems Tracking index is sometimes more important than tracking value ABTalksOnAI #60DaysOfDSA #DSA #Hashing #Python #PlacementPreparation #ProblemSolving #LearningInPublic #Abtalks
To view or add a comment, sign in
-
Day 3 of #200DaysOfCode! 🚀 Today, I leveled up from finding pairs to finding triplets with the classic "3Sum" problem (LeetCode 15). The goal is to find all unique triplets in an array that sum up to zero. The naive approach (three nested loops) is a painfully slow O(N^3). To solve this efficiently, I had to combine sorting with the Two Pointer technique. My O(N^2) Approach: Sort First: I started by sorting the array. This is crucial because it allows us to use pointers effectively and easily handle duplicates. Fix One, Solve Two: I iterated through the array with a fixed pointer i. For each number, the problem reduces to finding two other numbers that sum to -nums[i]. The "Two Pointer" Sweep: I placed a left pointer at i+1 and a right pointer at the end. If the sum is too small, move left forward. If the sum is too big, move right backward. The Tricky Part (Duplicates): The real challenge in 3Sum is avoiding duplicate triplets (e.g., [-1, -1, 2] appearing twice). As you can see in my code, I implemented while loops to skip over identical elements for both the fixed number and the pointers. It’s satisfying to see how sorting the data upfront makes a complex problem much more manageable. 3 days down! Consistency is building. 🧱 Has anyone tried extending this logic to "4Sum"? Does the recursion get messy? 👇 #200DaysOfCode #Python #LeetCode #Algorithms #TwoPointers #3Sum #ProblemSolving #CodingChallenge #DeveloperJourney
To view or add a comment, sign in
-
-
✅ First Unique Character in a String | Hash Map | LeetCode Solved the First Unique Character in a String problem using a frequency dictionary approach. 💡 Problem Given a string s, return the index of the first non-repeating character. If it does not exist, return -1. ⚡ Approach — Hash Map • Traverse the string and store character frequencies in a dictionary. • Iterate through the dictionary to find the character with frequency 1. • Return its index using s.index(char). ⏱ Complexity • Time: O(n) • Space: O(1) (since character set is limited) 🧠 Key Learning Frequency counting is a powerful technique for string problems. Hash maps help reduce nested loops and improve efficiency. Small string problems like this build a strong foundation for advanced DSA concepts. #LeetCode #DSA #Python #HashMap #StringProblems #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
LeetCode Problem 160: "Intersection of two Linked Lists": Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null. Approach: Use two pointers technique, maintain one pointer at list A and another at list B, at each iteration check whether pointers point to the same node, if yes then return that node. If pointer A reaches end update it to point to the head of list B and if pointer B reaches end update it to point to the head of list A. In this way each of the pointers traverses both of the lists. Time complexity: O(m+n) Space Complexity: O(1) #LeetCode #LinkedLists #Python #CompetitiveProgramming #ProblemSolving #Algorithms #DataStructures
To view or add a comment, sign in
-
-
Day 25 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to check whether a list is sorted in ascending order. The goal was to verify the order of elements by comparing each element with the next one. What the program does: • Takes a list as input • Compares each element with the next element • Checks if the list follows ascending order • Returns True if sorted, otherwise False How the logic works: 1)A function is_sorted(lst) is defined 2)A loop runs from the first element to the second-last element 3)Each element is compared with the next element (lst[i] and lst[i+1]) 4)If any element is greater than the next one, the list is not sorted 5)The function immediately returns False 6)If the loop finishes without finding such a case, the function returns True Example: List 1: [1, 2, 3, 4, 5] → Sorted → True List 2: [1, 3, 2, 4, 5] → Not Sorted → False List 3: [] → Sorted → True List 4: [7] → Sorted → True Why this works well: – Only one pass through the list – Time Complexity: O(n) – Works for empty and single-element lists Key learnings from Day 25: – Sequential comparison logic – Handling edge cases in lists – Writing efficient list validation functions – Strengthening algorithmic thinking #100DaysOfCode #Day25 #Python #PythonProgramming #Algorithms #ProblemSolving #DataStructures #CodingPractice #LogicBuilding #LearnByDoing #ComputerScience #InterviewPrep #BTech #CSE #AIandML #VITBhopal #TechJourney
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