It took a lot of time and 2 rough pages to solve 😭 . 𝐃𝐚𝐲 𝟕𝟐/𝟏𝟎𝟎 – Competitive Programming (CodeForces) Solved: 1731B – Kill Demodogs A math-heavy problem since n is huge, so brute force / DP on grid is not possible. -- Path is fixed in structure → from (1,1) → (1,n) → (n,n) -- Each cell contributes i * j, so we need to derive a closed-form sum -- Final formula comes out to: 2022 × n(n+1)(4n−1) / 6 Then just compute it carefully using modular arithmetic. #Codeforces #CompetitiveProgramming #100DaysOfCode #Math #NumberTheory #ProblemSolving #DSA #Cpp #Algorithms
Solving 1731B Kill Demodogs with Modular Arithmetic
More Relevant Posts
-
𝐃𝐚𝐲 𝟗𝟐/𝟏𝟎𝟎 – Competitive Programming (CodeForces) Solved: 977C - Less or Equal, 1872D - Plus Minus Permutation --977C was simple greedy: sort the array and check the k-th position carefully to satisfy the condition. --1872D was more math + greedy: instead of building permutation, count multiples using LCM, assign largest values to x-multiples and smallest to y-multiples, then compute score. #Codeforces #CompetitiveProgramming #100DaysOfCode #Greedy #Math #NumberTheory #ProblemSolving #DSA #Cpp #Algorithms
To view or add a comment, sign in
-
-
Day 38 #SDE Today I worked on backtracking with duplicates and game theory / dynamic programming. Solved: • Permutations II • Predict the Winner Key Learning: • “Permutations II” highlights how to handle duplicates in backtracking by sorting and skipping repeated elements to avoid duplicate permutations. • “Predict the Winner” introduces a game theory perspective, where we use recursion + DP to simulate optimal choices and maximize score difference. #LeetCode #DSA #Backtracking #DynamicProgramming #Algorithms #Java #SoftwareEngineering
To view or add a comment, sign in
-
Day 22 of #30DaysOfLeetCode Solved LeetCode Problem #66 – Plus One using C. Problem: You are given a large integer represented as an array of digits. Increment the number by one and return the resulting array of digits. Approach I used: • Started from the last digit and moved backwards • If the digit is less than 9 → simply increment and return • If the digit is 9 → convert it to 0 and continue checking • If all digits are 9 → create a new array like [1,0,0,...] Performance: • Runtime: 0 ms (Beats 100%) • Memory: 10.06 MB What I learned today: • How to handle carry operations in arrays • Improved understanding of edge cases • How to dynamically allocate memory in C • Better confidence in array-based problems #LeetCode #DSA #Algorithms #Arrays #CProgramming #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 69 of #100DaysOfCode 🔥 Problem: Majority Element II (LeetCode 229) Today’s challenge was all about finding elements that appear more than ⌊n/3⌋ times in an array. Sounds simple, but the twist is doing it efficiently! 💡 Key Insight: Instead of counting frequencies using extra space, we can use an optimized approach based on the Boyer-Moore Voting Algorithm. ✨ Approach Highlights: • There can be at most 2 majority elements (> n/3) • Maintain 2 candidates and their counts • First pass → find potential candidates • Second pass → verify their frequency ⚡ Why this works? Because any element appearing more than n/3 times will survive the elimination process. 📈 Complexity: • Time: O(n) • Space: O(1) 🎯 Takeaway: Smart algorithms > brute force. Understanding patterns like voting algorithms can save both time and space! #DSA #LeetCode #CodingJourney #ProblemSolving #100DaysOfCode #Algorithms #Programming
To view or add a comment, sign in
-
-
Day 43 of #GeekStreak60 – Consecutive 1's Not Allowed Solved a classic Dynamic Programming problem: counting binary strings of length n without consecutive 1s. Approach: Used a Fibonacci-style DP pattern, where each state depends on the previous ones based on whether the string ends with 0 or 1. Key Insight: A string ending with 1 must follow a 0, which naturally leads to a Fibonacci sequence. Great practice for strengthening DP pattern recognition! #DataStructures #DynamicProgramming #Coding #ProblemSolving #LearningJourney
To view or add a comment, sign in
-
-
🚀 Day 28 | 100 Days of Coding Challenge #DrGViswanathanChallenge 📘 Problem Solved: Single Number III (LeetCode) 🔧 Approach Used (Brute Force): • Iterated through each element • Counted its frequency using nested loops • Stored elements that appeared only once ⏳ Complexity: Time: O(n²) Space: O(1) (excluding output) 🧠 Key Learning: Brute force helps build understanding, but constraints push us toward better solutions. 💡 Optimal Insight: This problem can be solved in O(n) using Bit Manipulation (XOR) by separating numbers based on differing bits. 📂 Topics Covered: Arrays, Brute Force, Bit Manipulation #100DaysOfCode #DSA #CPP #LeetCode #CodingJourney #CompetitiveProgramming
To view or add a comment, sign in
-
-
Day 14 of #30DaysOfLeetCode Solved #28 – Find the Index of the First Occurrence in a String using C. Problem: Given two strings (haystack and needle), return the index of the first occurrence of the needle in the haystack. If it doesn't exist, return -1. Approach I used: • Used a simple string matching technique • Compared the needle with the haystack character by character • If characters match, continued checking the next characters • If not, moved to the next starting index • Returned the first matching position Performance: • Runtime: 136 ms • Memory: 8.84 MB What I learned today: • How string comparison works internally • Better understanding of nested loops in real problems • Learned how to search substrings without using built-in functions • Strengthened problem-solving consistency #LeetCode #DSA #Algorithms #Strings #CProgramming #ProblemSolving
To view or add a comment, sign in
-
-
Day: 93/365 📌 LeetCode POTD: XOR After Range Multiplication Queries II Hard Key takeaways/Learnings from this problem: 1. This one shows how combining math properties (XOR) with range updates can simplify what looks complex at first. 2. Instead of applying each query directly, thinking in terms of difference arrays / prefix impact saves a lot of time. 3. Key learning: XOR has nice cancellation properties, so patterns matter more than brute force simulation. 4. Overall, it’s a great example of turning repeated operations into a smart aggregated computation. #POTD #365DaysOfCode #DSA #Java #ProblemSolving #LearningInPublic #Consistency 🥷
To view or add a comment, sign in
-
-
Day 6 🚀 Solved: Maximum Depth of Binary Tree (LeetCode 104) This is a classic recursion problem on trees. 💡 Key idea: The depth of a tree is 1 + the maximum depth of its left and right subtrees. 🔹 Approach: If the node is null → depth is 0 Recursively calculate depth of left and right Take the maximum and add 1 ⏱️ Time Complexity: O(n) 🔗 GitHub: https://lnkd.in/gz5mBpDx #DSA #LeetCode #Coding
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