🚀 #100DaysOfCode – Day 37 Update Today I solved an array problem using a brute force approach. 🔹 Problem: Leaders in an Array (A leader is an element greater than all elements to its right.) ⸻ ✅ Approach — Brute Force 📌 Core Idea: For every element, check if there is any greater element on its right side. ⸻ ✅ Algorithm Steps: 1️⃣ Run an outer loop from i = 0 to i < n. 2️⃣ Assume current element is a leader → flag = true. 3️⃣ Run an inner loop from j = i + 1 to j < n. 4️⃣ If arr[i] < arr[j] → 👉 Set flag = false and break the loop. 5️⃣ After inner loop, if flag == true → 👉 arr[i] is a leader, print/store it. ⸻ ⏱ Time Complexity: O(N²) 💾 Space Complexity: O(1) ⸻ 💡 Key Learning: Brute force helps in understanding the problem clearly. Next step is to optimize it using a right-to-left traversal approach. #Day37 #100DaysOfCode #DSA #Java #Arrays #ProblemSolving #CodingJourney
Leaders in Array: Brute Force Approach
More Relevant Posts
-
🚀 #100DaysOfCode – Day 39 Update Today I solved an interesting array problem. 🔹 Problem: Longest Consecutive Sequence in an Array ⸻ ✅ Approach 1 — Brute Force 📌 Core Idea: • Pick each element and try to build a consecutive sequence from it. • For every element x, check if x + 1, x + 2, … exists in the array. ⸻ ✅ Algorithm Steps: 1️⃣ Traverse each element in the array. 2️⃣ For every element, initialize count = 1 and current = arr[i]. 3️⃣ Run a loop to check if current + 1 exists in the array. 4️⃣ If found: • Increment count • Update current = current + 1 5️⃣ After the loop, update the maximum length. ⸻ ⏱ Time Complexity: O(N²) (due to repeated searching) 💾 Space Complexity: O(1) ⸻ 💡 Key Learning: Brute force works but is inefficient due to repeated searches. Next step is to optimize using sorting or HashSet for better performance. #Day39 #100DaysOfCode #DSA #Java #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
🧠 Day 206 — Minimum Distance to Target 🎯📍 Today solved a simple yet important problem and did some revision alongside. 📌 Problem Goal Given an array, a target value, and a starting index: ✔️ Find the minimum distance between the start index and any index where the target exists 🔹 Core Idea Traverse the array and: ✔️ Check if current element matches the target ✔️ Calculate distance from the start index ✔️ Keep updating the minimum distance 🔹 Key Observation ✔️ Only indices with the target matter ✔️ Distance = absolute difference between indices ✔️ Simple linear scan gives optimal solution 🧠 Key Learning ✔️ Even easy problems strengthen fundamentals ✔️ Brute-force with clarity > overthinking ✔️ Absolute difference patterns are very common in arrays 💡 Today’s Realization Not every day needs to be a hard problem. Consistency with simple + revision days builds stronger intuition. 🚀 Momentum Status: Staying consistent and sharpening basics. On to Day 207. #DSA #Arrays #ProblemSolving #LeetCode #Java #CodingJourney #ConsistencyWins
To view or add a comment, sign in
-
𝐃𝐚𝐲 𝟖𝟏/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐗𝐎𝐑 𝐀𝐟𝐭𝐞𝐫 𝐑𝐚𝐧𝐠𝐞 𝐌𝐮𝐥𝐭𝐢𝐩𝐥𝐢𝐜𝐚𝐭𝐢𝐨𝐧 𝐐𝐮𝐞𝐫𝐢𝐞𝐬 𝐈𝐈 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. 💪 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Group queries based on step size k. For small k, use prefix multiplication (range product trick) to apply updates efficiently. For large k, process directly since updates are fewer. Maintain a multiplier array and apply all updates at the end. Finally compute XOR of the transformed array. 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Sqrt decomposition + prefix product + modular inverse + hashing. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: ~𝐎(𝐧 √𝐧 + 𝐪 √𝐧) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: When operations vary by step size, splitting into small and large cases (sqrt decomposition) can drastically optimize performance. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #Optimization #Math #PrefixSum #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟔𝟔 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on counting all prime numbers less than n efficiently. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Count Primes 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐒𝐢𝐞𝐯𝐞 𝐨𝐟 𝐄𝐫𝐚𝐭𝐨𝐬𝐭𝐡𝐞𝐧𝐞𝐬 • Created a boolean array to mark non-prime numbers • Started from 2 and marked all its multiples as non-prime • Repeated the process for each number up to √n • Counted numbers that remained unmarked 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Precomputation helps solve problems efficiently • Marking multiples avoids repeated checks • Only iterating till √n reduces unnecessary work • Classic algorithms are still highly relevant 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n log log n) • Space: O(n) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Optimization is not always about complex logic — sometimes it’s about eliminating unnecessary work early. 66 days consistent 🚀 On to Day 67. #DSA #Arrays #Math #Sieve #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
Day 65/100 | #100DaysOfDSA 🧩🚀 Today’s problem: N-Queens A classic hard backtracking problem. Problem idea: Place N queens on an N×N chessboard such that no two queens attack each other. Key idea: Use backtracking with efficient conflict checking. Why? • We must explore all valid board configurations • Reject placements that cause conflicts • Continue building only valid states How it works: • Place one queen per row • Track columns, diagonals, anti-diagonals • Use bit masking for faster checks ⚡ • Try placing → recurse → backtrack Optimization: Using bitmasks drastically reduces time compared to naive checking. Time Complexity: Exponential (but optimized with pruning) Space Complexity: O(n) recursion depth Big takeaway: Efficient state tracking (cols + diagonals) turns a brute-force problem into a much faster solution. 🔥 One of the most important backtracking problems to master! Day 65 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #Backtracking #Recursion #BitManipulation #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 70/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Reverse Linked List II A neat variation of linked list reversal where only a specific portion of the list is reversed. Problem idea: Reverse a linked list from position left to right, keeping the rest of the list unchanged. Key idea: In-place reversal using pointer manipulation. Why? • We don’t reverse the whole list, only a segment • Need to reconnect the reversed part correctly • Must carefully track boundaries (left and right) How it works: • Use a dummy node to handle edge cases • Move a pointer to the node just before left • Start reversing nodes one by one within the range • Adjust links to insert nodes at the front of the sublist • Reconnect the reversed portion with remaining list Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Partial reversal in linked lists requires precise pointer updates, not extra space. This builds strong intuition for advanced linked list problems. 🔥 Day 70 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #LinkedList #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 68/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Add Two Numbers A fundamental linked list problem that mimics real-life addition. Problem idea: Add two numbers represented by linked lists (digits stored in reverse order). Key idea: Linked list traversal + carry handling. Why? • We process digits one by one (like manual addition) • Need to handle carry at each step • Lists can have different lengths How it works: • Use a dummy node to build the result • Traverse both lists simultaneously • Add values + carry • Create new node with (sum % 10) • Update carry = sum / 10 • Move pointers forward • Continue until both lists and carry are done Time Complexity: O(max(m, n)) Space Complexity: O(max(m, n)) Big takeaway: Using a dummy node simplifies linked list construction and avoids edge cases. This pattern is very common in linked list problems. 🔥 Day 68 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #LinkedList #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟒𝟗 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on checking whether a string can be segmented into valid dictionary words. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Word Break 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐃𝐅𝐒 + 𝐌𝐞𝐦𝐨𝐢𝐳𝐚𝐭𝐢𝐨𝐧 • Converted the word list into a HashSet for fast lookup • Used recursion to try breaking the string into prefixes • If prefix exists in dictionary, recursively check the remaining string • Stored results in a memo map to avoid recomputation This avoids exponential re-checking of the same substrings. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Memoization helps optimize recursive solutions • Breaking problems into smaller substrings is a common pattern • HashSet improves lookup efficiency • DP problems often start with recursion and get optimized 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n²) (with memoization) • Space: O(n) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 When recursion feels slow, adding memoization can transform it into an efficient solution. 49 days consistent 🚀 On to Day 50. #DSA #Arrays #DynamicProgramming #Recursion #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
💡 LeetCode 191 — Number of 1 Bits (Hamming Weight) Recently solved an interesting bit manipulation problem that highlights the power of low-level optimization 🚀 🔍 Problem Statement: Given an unsigned integer, count the number of set bits (1s) in its binary representation. 🧠 Key Insight: Instead of checking every bit individually, we can use a clever trick: 👉 n & (n - 1) removes the rightmost set bit in each operation. This allows us to count only the set bits, making the solution more efficient. ⚙️ Approach Used (Brian Kernighan’s Algorithm): Initialize a counter Repeatedly apply n = n & (n - 1) Increment count until n becomes 0 📈 Time Complexity: O(k), where k = number of set bits (faster than checking all 32 bits) 📦 Space Complexity: O(1) ✨ Why this problem is important: Strengthens understanding of bit manipulation Frequently asked in interviews Useful in low-level optimization and system design 💬 Takeaway: Sometimes the best solutions come from understanding how data is represented at the binary level. Small tricks can lead to big optimizations! #LeetCode #DSA #CodingInterview #Java #BitManipulation #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
🧠 Day 183 — Reverse Submatrix 🔄📊 Today solved a clean and intuitive matrix manipulation problem: Reverse a Submatrix. 📌 Problem Goal Given a matrix and parameters (x, y, k): ✔️ Select a k × k submatrix starting from (x, y) ✔️ Reverse it vertically (top ↔ bottom rows) 🔹 Core Idea Instead of touching every element randomly, we: 👉 Focus only on the selected submatrix boundaries 👉 Swap rows from top to bottom This works like reversing an array, but applied to rows inside a matrix window. 🔹 Approach 1️⃣ Set two pointers: • One at the top row • One at the bottom row 2️⃣ Swap entire rows (column by column) within the submatrix 3️⃣ Move inward until all rows are reversed 🔹 Why It Works We are effectively reversing the order of rows in the submatrix while keeping column positions intact. This ensures: ✔️ Correct transformation ✔️ In-place modification ✔️ Efficient traversal 🧠 Key Learning ✔️ Matrix problems often reduce to controlled pointer movement ✔️ Think in terms of sub-boundaries instead of full matrix ✔️ Row/column swaps are powerful tools for transformations 🚀 Momentum Status: Strong consistency with arrays, matrices, and trees. On to Day 184. #DSA #Matrix #ProblemSolving #Java #CodingJourney #LeetCode #ConsistencyWins
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