Today I tackled the Sequence Equation problem, which involves working with permutations and index mapping. 🔍 Problem Insight: Given a permutation p, find values of y such that: 👉 p[p[y]] = x for all x from 1 to n 💡 Key Idea: Instead of brute force, I used: Position mapping for fast lookup Double indexing trick (pos[pos[x]]) 🚀 Why this works: It reduces complexity from O(n²) to O(n), making it highly efficient. 🧠 What I learned: Importance of inverse mapping Optimizing lookup operations Writing clean and efficient Java code 📌 Example: Input: [5, 2, 1, 3, 4] Output: [4, 2, 5, 1, 3] 🔖 Tags #Java #DataStructures #Algorithms #CodingChallenge #ProblemSolving #100DaysOfCode #DeveloperJourney #SoftwareDeveloper #Hackerrank #CodingPractice #TechLearning
Solving Sequence Equation with Efficient Java Algorithm
More Relevant Posts
-
#2553 — Separate the Digits in an Array (Easy) ✅ Problem: Given an array of positive integers, decompose each integer into its individual decimal digits and return them in a single array, preserving the original order of both numbers and digits. Example: nums = [13, 25, 83, 77] → [1, 3, 2, 5, 8, 3, 7, 7] Approach: Iterate through the input array, convert each integer to a string, extract every character, and map it back to its numeric value by subtracting the character '0'. Append the results sequentially. Implementation insight: The logic can be expressed in two equivalent styles — an explicit loop with an intermediate List, or a single Java stream pipeline using flatMap to flatten each number into its digit stream. Both achieve O(n × d) time and O(n × d) space, where d ≤ 6 (max digits per number). Key takeaways: String conversion keeps digit extraction clean and readable. The ordering guarantee (number order → digit order) is straightforward to maintain with a linear traversal. Stream pipelines can reduce boilerplate without sacrificing clarity when used judiciously. 🔗 Solution: https://lnkd.in/gG6cYw6Z #LeetCode #Java #Coding #Algorithms #ProblemSolving
To view or add a comment, sign in
-
-
✳️Day 51 of #100DaysOfCode✳️ 🚀 Cracking the Sliding Window: LeetCode 1004 (Max Consecutive Ones III) I recently tackled this popular interview question using an efficient Sliding Window approach in Java. 🔹The challenge: Find the maximum number of consecutive 1s in a binary array if you can flip at most k 0s. ⭐Here is the step-by-step breakdown of how my solution works: 🔸Initialize Two Pointers: Use a fast pointer (j) to expand the window and a slow pointer (i) to contract it, keeping track of the maxLen and the number of zeroes encountered. 🔸Expand the Window: Iterate through the array with j. If nums[j] == 0, increment the zero counter. 🔹Shrink When Invalid: If the number of zeros exceeds k, move the left pointer i forward. If the element leaving the window (nums[i]) is a 0, decrement the zero counter. 🔹Update Max Length: At each step, calculate the valid window size using j - i + 1 and update the maximum length found so far. 🔹Complexity: Time Complexity: O(N) – Each element is visited at most twice. Space Complexity: O(1) – Optimal memory usage with no extra data structures. Always satisfying to see all test cases pass with clean, readable code! 💻✨ #LeetCode #Java #DataStructures #Algorithms #SlidingWindow #CodingLife #SoftwareEngineering
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 30 🧩 Problem Solved: Longest Repeating Character Replacement (LeetCode #424) 💭 What I Learned: Used the Sliding Window technique with a HashMap to find the longest substring that can be transformed into repeating characters after at most k replacements. Key idea: ��� A window is valid if: (window size − frequency of most common character) ≤ k Approach: ✔️ Expanded the window using the right pointer ✔️ Tracked character frequencies using HashMap ✔️ Maintained the count of the most frequent character ✔️ Shrunk the window whenever replacements required exceeded k This helped me understand how frequency tracking can optimize substring problems efficiently. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(1) (only uppercase English letters) ⚡ Key Takeaways: ✔️ Sliding Window becomes powerful when combined with frequency analysis ✔️ Maintaining the maximum frequency avoids unnecessary recomputation ✔️ Many substring optimization problems follow a “valid window” pattern 💻 Language Used: Java ☕ 📘 Concepts: Sliding Window · HashMap · Frequency Counting · Strings · Two Pointers #CodeEveryday #DSA #LeetCode #Java #SlidingWindow #Strings #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
Binary search on a sorted array is easy. Binary search on a rotated sorted array is where it gets interesting. Day 36 of #100DaysOfCode Today I solved LeetCode 33 - Search in Rotated Sorted Array, and this is one of those problems that every developer preparing for interviews must know. The problem: A sorted array has been rotated at some unknown pivot. Given a target, find its index in O(log n) time or return -1. How I approached it: The key observation is that even after rotation, at least one half of the array is always perfectly sorted. I used this fact to decide which half to search. At every step I calculated mid and checked two things. First, if nums[low] <= nums[mid], the left half is sorted. In that case if target lies within nums[low] and nums[mid], I searched left, otherwise I moved to the right half. If the left half is not sorted, the right half must be sorted. Then if target lies within nums[mid] and nums[high], I searched right, otherwise I moved to the left half. This gives a clean O(log n) solution with no extra space. What I learned from this problem: The real insight here is not binary search itself, it is knowing which half is still sorted at every step. Once you identify the sorted half, the rest of the logic is exactly like standard binary search. This problem teaches you to extract useful information from partial structure in data, which is a skill that goes far beyond just this one problem. 0ms runtime beating 100% was a great result today. #100DaysOfCode #LeetCode #DSA #Java #BinarySearch #Programming #SoftwareEngineering #CodingChallenge #InterviewPrep
To view or add a comment, sign in
-
-
#CodeEveryday — My DSA Journey | Day 27 🧩 Problem Solved: Construct Binary Search Tree from Preorder Traversal (LeetCode #1008) 💭 What I Learned: Constructed a Binary Search Tree (BST) using preorder traversal by leveraging the relationship between: 👉 Preorder Traversal = Root → Left → Right 👉 Inorder Traversal of BST = Sorted Order Approach: ✔️ Created inorder traversal by sorting the preorder array ✔️ Used a HashMap to store inorder indices for fast lookup ✔️ Recursively built left and right subtrees using preorder + inorder ranges This strengthened my understanding of tree construction using traversal techniques. ⏱ Time Complexity: O(n log n) (due to sorting) 🧠 Space Complexity: O(n) ⚡ Key Takeaways: ✔️ BST inorder traversal is always sorted ✔️ Tree construction problems rely heavily on traversal properties ✔️ HashMap helps optimize inorder index lookup from O(n) → O(1) 💻 Language Used: Java ☕ 📘 Concepts: BST · Tree Construction · Recursion · Preorder Traversal · Inorder Traversal #CodeEveryday #DSA #LeetCode #Java #BST #Trees #Recursion #ProblemSolving #Algorithms #CodingJourney #Consistency 🚀
To view or add a comment, sign in
-
-
Nested encoded strings can look complex at first, but stacks make the decoding process systematic and manageable. 🚀 Day 144/365 — DSA Challenge Solved: Decode String Problem idea: Given an encoded string in the format: k[encoded_string] Repeat the string inside brackets exactly k times and return the fully decoded result. Efficient approach: 1. Traverse the string character by character 2. Build numbers for repeat counts 3. Use stacks to store: - Previous strings - Repeat counts 4. When ] appears: - Repeat current substring - Merge it with previous stored string Key Learning: - Stack-based string decoding - Handling nested patterns - Managing multiple states simultaneously - Parsing strings efficiently ⏱ Time: O(n) 📦 Space: O(n) Day 144/365 complete. 💻 221 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #Stack #String #LeetCode #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟮/𝟳𝟱 𝗗𝗮𝘆𝘀 𝗼𝗳 #𝗖𝗼𝗱𝗲𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Today's problem: 𝗚𝗿𝗲𝗮𝘁𝗲𝘀𝘁 𝗖𝗼𝗺𝗺𝗼𝗻 𝗗𝗶𝘃𝗶𝘀𝗼𝗿 𝗼𝗳 𝗦𝘁𝗿𝗶𝗻𝗴𝘀 (𝗟𝗲𝗲𝘁𝗰𝗼𝗱𝗲 𝟭𝟬𝟳𝟭) This problem helped me understand how mathematical concepts like 𝗚𝗖𝗗 can be applied in 𝘀𝘁𝗿𝗶𝗻𝗴 𝗺𝗮𝗻𝗶𝗽𝘂𝗹𝗮𝘁𝗶𝗼𝗻 and pattern matching problems. 𝗞𝗲𝘆 𝗖𝗼𝗻𝗰𝗲𝗽𝘁: Two strings can have a common divisor string only if both strings can be formed by repeating the same pattern multiple times. 𝗪𝗵𝗮𝘁 𝗜 𝗟𝗲𝗮𝗿𝗻𝗲𝗱: • Applying the concept of 𝗚𝗿𝗲𝗮𝘁𝗲𝘀𝘁 𝗖𝗼𝗺𝗺𝗼𝗻 𝗗𝗶𝘃𝗶𝘀𝗼𝗿 in strings • Understanding ��𝘁𝗿𝗶𝗻𝗴 𝗽𝗮𝘁𝘁𝗲𝗿𝗻 𝘃𝗮𝗹𝗶𝗱𝗮𝘁𝗶𝗼𝗻 using concatenation • Using 𝗿𝗲𝗰𝘂𝗿𝘀𝗶𝗼𝗻 to implement the Euclidean GCD algorithm • Improving problem-solving skills with 𝗼𝗽𝘁𝗶𝗺𝗶𝘇𝗲𝗱 𝗹𝗼𝗴𝗶𝗰 • Handling cases where no common divisor string exists 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: • Check whether both concatenated strings are equal • If not equal, return an empty string • Find the 𝗚𝗖𝗗 of both string lengths • Return the substring from index 0 to the GCD length • Use recursion for efficient GCD calculation 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 𝗔𝗻𝗮𝗹𝘆𝘀𝗶𝘀: 𝗧𝗶𝗺𝗲 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: O(n + m) → Traversing and comparing both strings 𝗦𝗽𝗮𝗰𝗲 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: O(1) → Constant extra space used Successfully solved and understood how mathematical logic can simplify string-based problems. Consistency and daily practice are helping me strengthen my 𝗗𝗦𝗔 and 𝗽𝗿𝗼𝗯𝗹𝗲𝗺-𝘀𝗼𝗹𝘃𝗶𝗻𝗴 mindset step by step. 𝗟𝗮𝗻𝗴𝘂𝗮𝗴𝗲 𝗨𝘀𝗲𝗱: Java 𝗥𝘂𝗻𝘁𝗶𝗺𝗲 𝗔𝗰𝗵𝗶𝗲𝘃𝗲𝗱: 1 ms #Day2 #75DaysOfCode #Java #DSA #LeetCode #CodingJourney #ProblemSolving #Programming #SoftwareEngineering #TechLearning
To view or add a comment, sign in
-
-
🚀 Building my own JVM targeted programming language This diagram shows the full journey from writing code --> running it on the Java Virtual Machine. • Lexer → Converts source code into tokens (example: int x = 10 → keywords, identifiers, symbols) • Parser → Takes tokens and builds structure • AST (Abstract Syntax Tree) → A tree representation of the program logic (this is the “brain” of the code structure) • Type Checker → Validates types (optional but powerful) • Optimizer → Improves performance before execution • Bytecode Generator → Converts AST → JVM bytecode (.class file) JVM Execution Flow: • Class Loader → Loads compiled classes • Bytecode Verifier → Ensures safety + correctness • Runtime Data Areas → Heap, Stack, Method Area • Execution Engine → → Interpreter (runs code line by line) → JIT Compiler (converts hot code to native machine code) These components represent the core architecture of a programming language that targets the JVM. 🫡🫡 #ProgrammingLanguages #JVM #CompilerDesign #BuildInPublic #SoftwareEngineering #systemdesing
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟏𝟑𝟎/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐋𝐨𝐧𝐠𝐞𝐬𝐭 𝐂𝐨𝐦𝐦𝐨𝐧 𝐒𝐮𝐟𝐟𝐢𝐱 𝐐𝐮𝐞𝐫𝐢𝐞𝐬 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: • Build a Trie using reversed words from wordsContainer • Each Trie node stores: - shortest word length - corresponding smallest index • Traverse query strings in reverse order • Follow longest matching suffix path in Trie • Return best matching index stored at deepest valid node 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦: • Trie + String Processing ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: • O(total characters in wordsContainer + wordsQuery) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: • O(total characters in Trie) 📈 𝐊𝐞𝐲 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲: • Reversing strings transforms suffix problems into prefix matching problems 🔗 Problem Link: https://lnkd.in/dXYjF78v #LeetCode #LeetcodePOTD #365DaysOfCode #DSA #Java #Trie #Strings #ProblemSolving
To view or add a comment, sign in
-
-
🚀 DAY 125/150 — BINARY SEARCH ON ROTATED ARRAY! 🔍📊 Day 125 of my 150 Days DSA Challenge in Java and today I solved a problem that strengthens understanding of Binary Search and sorted array observation 💻🧠 📌 Problem Solved: Find Minimum in Rotated Sorted Array 📌 LeetCode: #153 📌 Difficulty: Medium 🔹 Problem Insight We are given a sorted array that has been rotated at some pivot. 👉 Example: [4,5,6,7,0,1,2] 🎯 Goal: Find the minimum element in O(log n) time. 🔹 Approach Used I used a Binary Search approach: 🔁 Key Observation: • One half of the array is always sorted • The minimum element lies in the unsorted part ⚡ Logic: • Compare mid with right If nums[mid] > nums[right] → minimum lies on right side Else → minimum lies on left side including mid ✔️ Continue until left == right ⏱ Complexity Time Complexity: O(log n) Space Complexity: O(1) 🧠 What I Learned • Binary Search is not only for searching elements • Observing sorted and unsorted portions simplifies problems • Pattern recognition is very important in optimized solutions 💡 Key Takeaway This problem taught me: How to apply Binary Search creatively Identifying pivot behavior in rotated arrays Reducing brute force solutions to logarithmic time 🌱 Learning Insight Now I’m improving at: 👉 Solving Binary Search pattern problems 👉 Thinking in terms of optimization and observations 🚀 �� Day 125 completed 🚀 25 days to go 🔗 Java Solution on GitHub: 👉 https://lnkd.in/gdyaDxge 💡 The key to Binary Search is recognizing the pattern, not memorizing the code. #DSAChallenge #Java #LeetCode #BinarySearch #Arrays #Algorithms #150DaysOfCode #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-