🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/diBESK6A 💡 My thought process: The solution checks every possible k × k submatrix in the grid to determine whether it forms a magic square. For a given starting position (row, col) and size k, it first validates that all row sums within the submatrix are equal by computing each row’s sum and comparing it against a reference value. It then verifies column consistency by ensuring every column sum matches the same reference row sum. Once row and column validation succeed, the algorithm computes both diagonals of the submatrix. The main diagonal is traversed from the top-left to the bottom-right of the submatrix, while the anti-diagonal is calculated using an offset-based index to correctly map the bottom-left to top-right traversal. The submatrix is considered a valid magic square only if both diagonal sums match each other and equal the row and column sums. 👉 My Solution: https://lnkd.in/dWn-uGVD If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
LeetCode Magic Square Validation Solution
More Relevant Posts
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/dvQUTQac 💡 My thought process: This solution calculates the minimum value of an array, allowing each element to be modified individually using bits. Each number in a list of input is checked to see if it’s odd or even. Even numbers can’t form a valid output, hence they are assigned -1. For odd numbers, the answer looks at bit positions from least significant to most significant to identify the first unset bit (0). Once obtained, it toggles the lowest bit below it (j-1) by performing an XOR operation, ensuring that the obtained number is the least integer that fulfills the specific condition. This algorithm requires a time complexity of O(n * 32) and additional space of O(n) for the size of the input list n. 👉 My Solution: https://lnkd.in/duUgT7Ag If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gxntCVfP 💡 My thought process: I used Dijkstra’s algorithm to find the minimum cost path because all edge weights are non-negative. To manage edge reversals, I modeled the graph by adding: - the original directed edge with cost w - a reverse edge with cost 2w, which represents the reversal operation This converts the problem into a standard shortest-path graph, eliminating the need for extra state handling. An adjacency list and a min-heap priority queue make traversal efficient, while a visited array tracks the minimum cost to reach each node. This method is straightforward, efficient, and works well for large graphs. 👉 My Solution: https://lnkd.in/gBkufJ5G If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
Day 19 Of #100DaysOfCode: #DSA: Solve leetcode problem with Merge Intervals Pattern - Insert Intervals(medium 57) Approach: - Sort the array by start time - create a temp vector/array - now iterate over intervals and check for each interval if its start time is greater then the newInterval start time if it is then add newInterval into the temp else add curr interval - now merge the intervals that are stored in temp vector/array Time Complexity: O(n logn) Space Complexity: O(n) #Coding #LearnInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gXwweaP3 💡 My thought process: This solution finds the longest substring where the characters 'a', 'b', and 'c' appear the same number of times. It approaches the problem by breaking it into three separate cases: substrings with one character, substrings with two distinct characters, and substrings with all three characters. In the single-character case, the method scans the string and tracks the length of the longest continuous run of the same character. This addresses substrings where balance is easily met, as only one character is present. For the two-character case, the technique uses a prefix-difference approach. While scanning the string, it counts the two selected characters and calculates their difference. If the same difference has occurred before, the segment between the last index and the current index must have equal counts of the two characters, making its length a valid option. When a third character appears, it resets the counts and the hash map to prevent invalid matches across segments. In the three-character case, the prefix concept is extended to two dimensions. Instead of tracking one difference, the algorithm focuses on two differences: (count(a) − count(b)) and (count(a) − count(c)). If the pair of differences appears again, it indicates that the counts of all three characters between those indices are equal, creating a balanced substring. A hash map keeps the earliest index for each difference pair, allowing for quick length updates in a single pass through the string. 👉 My Solution: https://lnkd.in/gc_vMxFG If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
Proud to present our Compiler Construction project on Intermediate Code Generation (ICG) using Three Address Code (TAC). Through this project, we explored how high-level conditional statements such as if and if–else are translated into intermediate representations using labels, jumps, and backpatching techniques. It significantly improved our understanding of control flow representation, execution paths, and the internal working of compilers in a machine-independent way. I am truly grateful to our respected teacher Zobia Zafar for the guidance and insightful feedback. Special thanks to my teammates Ali Ashiq and Syed Taha Nasir for their dedication and teamwork, which made this project a valuable learning experience. #CompilerConstruction #CompilerDesign #IntermediateCodeGeneration #ThreeAddressCode #Coding #ComputerScience #ConditionalExpressions
To view or add a comment, sign in
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/djbQcgTX 💡 My thought process: Problem is same as the before, the constraints are just a bit higher. We store the current values in temp (as long long) and simulate deletions using prevIndex/nextIndex as a doubly linked list, so after merging/removing an element we can find and update neighbors in O(1) without shifting the array. All current adjacent pairs are kept in an ordered set as (pairSum, leftIndex), which gives the minimum-sum adjacent pair in O(1) (begin()) and supports updates in O(log n). When we merge the chosen pair (first, second), only pairs involving first_left–first and second–second_right can change, so we erase those (if they exist), relink pointers to remove second, update temp[first] += temp[second], and insert the new affected pairs. We also maintain badPairs (count of adjacent inversions) and update it using only local comparisons around the merge boundary, since no other adjacencies are affected. Bad pairs will never increase than n-1. 👉 My Solution: https://lnkd.in/dcxVVSjV If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
LeetCode Daily Problem (19th Jan 2026) Educational Insight - The "1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold" problem demonstrates the power of Prefix Sum combined with Binary Search. By precomputing a 2D prefix sum matrix, we can evaluate any square submatrix sum in O(1) time. Binary search on the side length helps efficiently determine the largest valid square under the given threshold. Key Implementation Details • Build a 2D prefix sum matrix for fast sum queries • Apply binary search on the possible side lengths • Validate each candidate square in constant time • Reduce overall complexity compared to brute force Full platform dropping soon. Stay tuned. Join the waitlist: https://lnkd.in/gvSvUmEZ #DSA #LeetCode #Coding #VisuallyInclined
To view or add a comment, sign in
-
Day 13/45: Remove K Digits 🚀 The Concept: 🛒 Imagine a Price Tag with a character limit. To get the lowest price, you don't just remove the biggest numbers—you remove the "peaks" that appear early on. What I Learnt: 🧠 Monotonic Stack: The best way to "look back" and remove larger numbers in O(n) time. Greedy Logic: Always prioritize fixing the leftmost digits to minimize the total value. How I Did It: 🛠️ Used a string-based stack to maintain an increasing order. Popped the stack when a smaller digit arrived. Handled leading zeros and empty results. Stats: 1ms (Top 75%) | Memory: Top 90%! #100DaysOfCode #LeetCode #Coding #CPP
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Challenge 🔗 Problem: https://lnkd.in/gfWSucCh 💡 My thought process: This solution calculates the minimum cost to change a `source` string into a `target` string using allowed substring transformation rules that have specific costs. The transformations form a weighted directed graph, where each substring is a node, and each transformation is an edge with a cost. To manage indirect transformations effectively, Dijkstra’s algorithm finds the shortest path between two substrings. Since the same substring conversions can be requested multiple times, the algorithm saves the shortest-path results to avoid repeating graph searches. The overall solution employs top-down dynamic programming, with each state showing the minimum cost to transform the string starting from a given index. If the characters at the current position match, the algorithm moves forward at no cost. If they do not match, it examines all valid substring lengths and adds the transformation cost to the result of the remaining suffix. This mix of dynamic programming, shortest-path computation, and saved results ensures both accuracy and efficiency. 👉 My Solution: https://lnkd.in/gCv3cXCf If you found this breakdown helpful, feel free to ⭐ the repo or connect with me on LinkedIn 🙂🚀 #️⃣ #leetcode #cpp #dsa #coding #problemsolving #engineering #BDRM #BackendDevWithRahulMaheswari
To view or add a comment, sign in
-
-
Day 18 of #100DaysOfCode: #DSA: Learned the Merge Intervals Pattern Use it when you’re dealing with ranges/intervals in an array, especially when: - you need to merge overlapping intervals - you need to detect conflicts or overlaps - you are asked to insert a new interval Core Idea: - Sort intervals based on start time. - Compare current interval with the last merged interval. - If they overlap then merge them. - If not then add it as a new interval. #Problem: Solve Leetcode problem using this pattern - Merge Intervals(56) Time complexity: O(n logn) - Because we are doing sorting and sorting will take O(nlogn) time complexity in the worst case. If we talk about the overall time complexity then it is O(n logn) + O(n) where n is the length of intervals array. Space complexity: O(n) - Because we are using vector/array to store the result. And it will take O(n) in worst case if none of the intervals are overlapping. #DSA #Coding #LearnInPublic #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