🚀 100 Days LeetCode Challenge – Day 33 🚀 Solved LeetCode 1572: Matrix Diagonal Sum ✅ Today’s problem was simple in concept but a good reminder of index relationships in matrices. 💡 Core Idea: In a square matrix: Primary diagonal → i == j Secondary diagonal → i + j == n - 1 Traverse the matrix and: Add elements where i == j Add elements where j == n - i - 1 Make sure not to double count the center element (when n is odd) 🧠 Key Learnings: Understanding diagonal patterns is important for matrix problems Index math (i + j = n - 1) is extremely useful Avoid unnecessary nested loops — this can be optimized to O(n) 👉 Instead of O(n²), we can directly compute using: mat[i][i] mat[i][n-1-i] ⏱️ Complexity: Time: O(n²) Space: O(1) #100DaysOfCode #LeetCode #Matrix #Cpp #ProblemSolving #DSA
LeetCode 1572 Matrix Diagonal Sum Solution
More Relevant Posts
-
Day 20 of LeetCode Challenge 🚀 Problem Solved: Partitioning Into Minimum Number of Deci-Binary Numbers Language Used: C++ Total Solved: 84/100 Today’s problem was based on observation and greedy thinking. The task was to find the minimum number of deci-binary numbers required to sum up to a given decimal number. A deci-binary number can only contain digits 0 and 1. Instead of constructing the numbers manually, I focused on understanding the pattern. Since each deci-binary number can contribute at most 1 to any digit position, the minimum number required will always be equal to the maximum digit present in the given number. Time Complexity: O(n) Space Complexity: O(1) Day20 completed, On to Day21;
To view or add a comment, sign in
-
-
Day 10 | LeetCode Learning Journal 🚀 Today I practiced Binary Tree Preorder Traversal (Problem 144) on LeetCode. This problem helped me understand how tree traversal works when we visit the root before its subtrees. 🔑 Key Points: • Traversal order: Root → Left → Right • Visit the current node first • Recursively traverse the left subtree • Then recursively traverse the right subtree • Can also be implemented using a stack (iterative approach) 🌱 What I Learned: • Basics of tree traversal techniques • How recursion naturally fits tree structures • Difference between recursive and iterative traversal • Importance of traversal order in trees • Strengthened understanding of tree fundamentals #LeetCode #100DaysOfCode #DSA #BinaryTree #PreorderTraversal #Day10
To view or add a comment, sign in
-
-
Day 9 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Remove Duplicates from a Sorted Array The array was already sorted. We had to keep only unique elements. And do it in-place. Let’s understand with real life example ◾️Imagine you are arranging certificates in a file. ◾️Some certificates are duplicated. ◾️You only keep one copy of each. ◾️You arrange them neatly at the front. ◾️Whatever remains at the back does not matter. That’s the idea. 💻Optimal Approach (Using Set) 🔹️Create a set. 🔹️Insert all elements of the array into the set. 🔹️Set automatically removes duplicates. 🔹️Size of set gives number of unique elements (k). 🔹️Traverse the set. 🔹️Fill first k indices of the array with unique elements. Clean and simple. 📊 Complexity Analysis ▫️Time Complexity: O(N) We traverse the array once. ▫️Space Complexity: O(1) Constant extra space used. 📚 What I learned today: ▫️Sorted arrays make duplicate problems easier. ▫️Set is very useful for handling uniqueness. ▫️Understanding constraints helps in choosing approach. #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 46/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 367 – Valid Perfect Square (Easy) 🧠 Approach: Compute the integer square root of the number and check if its square equals the original number. If yes, it’s a perfect square. 💻 Solution: class Solution: def isPerfectSquare(self, num: int) -> bool: root = int(num ** 0.5) return root * root == num ⏱ Time | Space: O(1) | O(1) 📌 Key Takeaway: Mathematical observations can often simplify problems and eliminate the need for iterative checks. #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
Day 61/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 41 – First Missing Positive (Hard) 🧠 Approach: Place each number x at index x - 1 if it’s within the range 1 to n. After rearranging, the first index where nums[i] != i + 1 gives the missing positive number. 💻 Solution: class Solution: def firstMissingPositive(self, nums: List[int]) -> int: n = len(nums) for i in range(n): while 1 <= nums[i] <= n and nums[nums[i] - 1] != nums[i]: correct_index = nums[i] - 1 nums[i], nums[correct_index] = nums[correct_index], nums[i] for i in range(n): if nums[i] != i + 1: return i + 1 return n + 1 ⏱ Time | Space: O(n) | O(1) 📌 Key Takeaway: Index-based placement is a powerful technique for solving array problems in linear time without extra space. #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
Day 57/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 260 – Single Number III (Medium) 🧠 Approach: Since every element appears twice except two numbers, return the numbers whose frequency is 1. 💻 Solution: class Solution: def singleNumber(self, nums: List[int]) -> List[int]: if len(nums)==2: return nums ele = [] d = {} for i in nums: d[i] = d.get(i,0)+1 for i in d: if d[i]==1: ele.append(i) return ele ⏱ Time | Space: O(n) | O(n) 📌 Key Takeaway: Frequency counting is a straightforward way to identify unique elements in an array. #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
🚀 100 Days LeetCode Challenge – Day 37 Solved LeetCode 324: Wiggle Sort II ✅ Goal: Rearrange array such that: nums[0] < nums[1] > nums[2] < nums[3] > nums[4] ... 💡 Core Idea: Copy array into temp. Sort the temp array. Split sorted array into: Smaller half Larger half Fill: Even indices from smaller half (backwards) Odd indices from larger half (backwards) Filling the array backwards was important to properly handle duplicate elements. 🧠 Key Learnings: Pattern-based rearrangement problems often need: Sorting + smart indexing Reverse filling prevents equal elements from breaking wiggle condition. Even/Odd index control is a powerful technique. This was more about index manipulation strategy than brute logic. ⏱️ Complexity: Time: O(n log n) (due to sorting) Space: O(n) (temp array) #100DaysOfCode #LeetCode #WiggleSort #Arrays #CPlusPlus #ProblemSolving #DSA
To view or add a comment, sign in
-
-
🚀 March LeetCode Challenge: Day 05/31 | The Power of Parity in the March LeetCode Challenge! One thing I love about competitive programming is finding "The Shortcut." Today, for LeetCode 1758: Minimum Changes To Make Alternating Binary String, I was able to turn a string manipulation problem into a simple mathematical observation. 🧩 The Challenge Given a binary string, what is the minimum number of character flips needed to make it "alternating" (e.g., 0101... or 1010...)? 💡 My Logic: One Loop, Two Results Initially, you might think you need to check the string against two different patterns separately. But there's a more elegant way: The Pattern Rule: In an alternating string starting with 0, the character at index i should be 0 if i is even, and 1 if i is odd. This can be represented simply as i % 2. The Complement: If it takes X operations to make the string start with 0, it will take exactly (Total Length - X) operations to make it start with 1. By calculating one, I automatically knew the other. Taking the min() of these two gave me the optimal answer in a single pass! 📊 Performance Reflection Time Complexity: O(N) – A single traversal of the string. Space Complexity: O(1) – No extra strings created, just a single counter. #LeetCode #CodingChallenge #DynamicProgramming #Cpp #SoftwareEngineering #ProblemSolving #MarchCodingChallenge #VITBhopal #DataStructures #DrGViswanathan #VIT
To view or add a comment, sign in
-
-
LeetCode Practice: Today I solved Problem no.4(Median of Two Sorted Arrays) So, The Problem states that we have given 2 different array and we have to return the median of those sorted arrays. Approach (Brute Force) Although the optimal solution uses Binary Search, I first implemented a brute force approach to understand the problem better. Key Idea: Combine both arrays into a single array, sort it, and then calculate the median using the standard median formula. I Used- - push_back to push elements into one single array. - Sorted that array does this look good suggest me if i should change something -Applied the median formula depending on whether the total number of elements is odd or even. Time Complexity: O((n + m) log(n + m)) Space Complexity: O(1) (since we reuse the same array) #LeetCode #DSA #ProblemSolving #Cpp #CodingPractice
To view or add a comment, sign in
-
-
LeetCode 70 — Climbing Stairs Worked on finding the number of distinct ways to reach the top when you can climb either 1 or 2 steps at a time. Initially, it looks like a simple counting problem. But structurally, it follows a clear recurrence pattern. Approach — Recurrence Relation - If n ≤ 1 → only one possible way - From step n, you can: - Take 1 step → reduce to (n - 1) - Take 2 steps → reduce to (n - 2) - Total ways = ways(n - 1) + ways(n - 2) This is essentially the Fibonacci pattern in a different form. While testing with small inputs, the recursive solution worked correctly. However, on submission it resulted in Time Limit Exceeded for larger inputs. That made one thing very clear :- The logic is correct, but overlapping subproblems make the pure recursive approach inefficient. Key learning :- Correct logic is not enough. Efficiency matters. Recognizing when recursion needs optimization (memoization / DP) is just as important as writing it. #leetcode #recursion #dynamicprogramming #dsa #algorithms #problemSolving #codingjourney
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