Leveling Up Every Day Continuing my #100DaysOfLeetCode journey! #Day 34/100 LeetCode Challenge: Problem : Next Permutation ###31 LeetCode : https://lnkd.in/gCgwuccf Today I solved one of the classic problems in array manipulation — Next Permutation. The goal is to rearrange the numbers to form the next lexicographically greater permutation of the sequence. If such a rearrangement isn’t possible (the array is in descending order), we simply return the smallest possible order (ascending). Understanding the Logic Let’s say we have an array: [1, 2, 3] The next permutation would be [1, 3, 2] If the array is [3, 2, 1], it’s already the highest permutation — so we reverse it to [1, 2, 3]. Step-by-Step Approach Find the first decreasing element when traversing from the right. → This identifies the pivot point where the next permutation change must occur. Find the element just greater than this pivot (on the right side). → Swap these two elements to slightly increase the sequence lexicographically. Reverse the part after the pivot → This ensures the remaining sequence is the smallest possible order after the swap. Key Insights Runs in O(n) time — just a few traversals of the array. Uses O(1) extra space — all operations are in-place. A perfect example of logic, observation, and attention to detail working together. Personal Note This problem tested my understanding of how permutations and lexicographical ordering actually work behind the scenes. It’s a great exercise to sharpen problem-solving and array manipulation skills in Java. #100DaysOfCode #100DaysOfLeetCode #Java #ProblemSolving #DSA #CodingJourney #LeetCode #CodingChallenge #Programming #SoftwareEngineering #DataStructures #Algorithms #TechLearning #Math #CodeEveryday #JavaProgramming #LearnToCode #DeveloperJourney #DailyCoding #Developerlife #Coderlife #LearnToCode #ConsistendLearning #LearningEveryday
Solved Next Permutation problem in Java with #100DaysOfLeetCode
More Relevant Posts
-
📌 Day 154 of Coding - Check If Digits Are Equal in String After Operations I (LeetCode - Medium) 🎯 Goal: Given a numeric string s, repeatedly replace it with a new string formed by taking the sum (mod 10) of every pair of adjacent digits, until only two characters remain. Return whether the final two digits are equal. 💡Approach & Debugging: Used a simple iterative simulation approach. For each iteration, formed a new string res by summing adjacent digits modulo 10. Replaced s with res and continued until the string length dropped to 2. Finally, compared both digits for equality. ✔️ Time Complexity: O(N*N) - since the string shrinks gradually each iteration. ✔️ Space Complexity: O(N) - temporary strings. 🧠 Key Takeaways: Some problems test simulation and string manipulation more than algorithms. Always track how input size evolves after each iteration. #Day154 #LeetCode #StringManipulation #Java #ProblemSolving #DSA #CodingChallenge #Algorithms #100DaysOfCode #InterviewPrep #SoftwareEngineering #DataStructures #CodeNewbie #ProgrammersLife
To view or add a comment, sign in
-
-
📌 Day 153 of Coding - Maximum Frequency of an Element After Operations I (LeetCode - Medium) 🎯 Goal: Given an integer array nums, an integer range k, and a number of operations, determine the maximum possible frequency of any number after performing up to numOperations modifications - where each operation can change a number by at most k. 💡Approach & Debugging: Found the maximum value in the array and created a frequency prefix array. For each possible target value i, calculated the total numbers within the [i - k, i + k] range. The frequency of i could be increased using available operations on nearby elements. Kept track of the best achievable frequency. ✔️ Time Complexity: O(N + M) - where M is the value range up to max(nums) + k. ✔️ Space Complexity: O(M) 🧠Key Takeaways: Prefix sums + frequency arrays are powerful for range-based computations. Always optimize loops around range-based frequency calculations. #Day153 #LeetCode #Arrays #PrefixSum #Frequency #Java #ProblemSolving #DSA #CodingChallenge #Algorithms #100DaysOfCode #InterviewPrep #SoftwareEngineering #DataStructures #CodeNewbie #ProgrammersLife
To view or add a comment, sign in
-
-
🔥 Day 108 of My DSA Challenge – Merge Sorted Array 🔷 Problem : 88. Merge Sorted Array 🔷 Goal : Merge two sorted arrays into one sorted array in-place inside nums1. The tricky part? nums1 already has extra space at the end — and we need to fill it without using extra arrays. 🔷 Key Insight : Use the two-pointer technique starting from the end — this avoids overwriting values in nums1. Pointer p1 → end of valid elements in nums1 Pointer p2 → end of nums2 Pointer idx → last index in final merged array Place the largest element first by comparing from the end. 🔷 Approach : 1️⃣ Start from the last elements of both arrays 2️⃣ Compare and place the greater one at the end 3️⃣ Move pointers backwards 4️⃣ If nums2 still has elements, copy them This gives us O(m + n) time and O(1) extra space ✅ Sometimes the most elegant solutions are not about building new structures but smartly reusing what you already have. Two-pointer technique continues to be a powerful tool in array problems 💪 #Day108 #100DaysOfCode #LeetCode #DSA #Java #TwoPointers #Arrays #CodingChallenge #Algorithm #InPlaceAlgorithms #ProblemSolving #Programming #SoftwareEngineering #TechJourney #DeveloperJourney #CodeEveryday #LearnDSA #GrowthMindset #EngineerMindset #LogicBuilding
To view or add a comment, sign in
-
-
🚀 DSA Progress – Day 94 ✅ Problem #338: Counting Bits 🧠 Difficulty: Easy | Topics: Bit Manipulation, Dynamic Programming 🔍 Approach: Implemented a Dynamic Programming approach to efficiently count the number of 1 bits in every number from 0 to n without converting numbers to binary strings. Step 1 (Initialization): Create an array result of size n + 1, initialized to zeros. Base cases: result[0] = 0 → Binary of 0 is 0, so 0 ones. result[1] = 1 → Binary of 1 is 1, so 1 one. Step 2 (Even–Odd Relation): For every number i from 2 to n: If i is even, the number of 1s = result[i // 2] (same as half of i, because last bit is 0). If i is odd, the number of 1s = result[i // 2] + 1 (same as half, plus 1 for the last bit). Step 3 (Iterative Build): Use the above relation to fill the result array for all numbers up to n. 🕒 Time Complexity: O(n) Each number is processed once. 💾 Space Complexity: O(n) We store bit counts for all numbers from 0 to n. 📁 File: https://lnkd.in/ghuw8Vea 📚 Repo: https://lnkd.in/g8Cn-EwH 💡 Learned: This problem deepened my understanding of bitwise patterns and how small observations (like even–odd relationships) can lead to elegant DP-based optimizations. It showed how dynamic programming can simplify repetitive bit-counting logic and achieve linear time solutions. ✅ Day 94 complete — counted every single bit of progress, one 1️⃣ at a time! 💡⚙️💻✨ #LeetCode #DSA #Python #DynamicProgramming #BitManipulation #CountBits #100DaysOfCode #DailyCoding #InterviewPrep #GitHubJourney
To view or add a comment, sign in
-
🚀 Day-75 of #100DaysCodeOfChallenge 💡 LeetCode Problem: 1526. Minimum Number of Increments on Subarrays to Form a Target Array (Hard) 🧠 Concepts Practiced: Greedy Algorithms | Array Manipulation | Difference Computation Today’s challenge was a Hard-level problem, focusing on finding the minimum number of operations required to transform an initial array of zeros into a given target array — using only subarray increments. The key insight here was realizing that each increase in value from one element to the next represents a required new operation on LeetCode. Instead of simulating every subarray operation, we can simply sum up the positive differences between consecutive elements — a great example of mathematical optimization through observation 💭 🔹 Intuition: Only when a number increases compared to the previous one, a new increment operation is needed. ⚙️ Language: Java ⚡ Runtime: 3 ms (Beats 100%) 💾 Memory: 56.82 MB (Beats 61.41%) ✅ Result: 129 / 129 test cases passed — Accepted! 🎯 Each “hard” problem solved adds one more layer of confidence — and reminds me that persistence pays off 💪 #100DaysOfCode #LeetCode #Java #ProblemSolving #Algorithms #CodingChallenge #GreedyAlgorithm #LearningJourney #TechMindset #KeepCoding #SoftwareEngineering #DeveloperLife #LogicBuilding
To view or add a comment, sign in
-
-
Day 25 of #LeetCode Challenge 🔹 Problem: 3354. Make Array Elements Equal to Zero 🔹 Difficulty: Easy 🔹 Topic: Array | Simulation | Prefix Sum Today’s problem was a fun logic-based simulation involving movement and balance between left and right sums. The goal was to find how many starting positions (and directions) could lead to every element becoming zero. The trick here is realizing that you don’t actually need to simulate every move — just track prefix sums and compare left and right totals. If they’re equal (or differ by 1), it’s a valid starting point. A neat mix of math and logic! #LeetCode #100DaysOfCode #Day25 #Java #CodingChallenge #ProblemSolving #LearnToCode #LeetCodeDaily #ProgrammingJourney #DSA #Algorithms
To view or add a comment, sign in
-
-
💡 Day 19 of My LeetCode Journey — 3Sum Closest Today’s problem was “3Sum Closest”, a slight twist on the classic 3Sum challenge. 🧩 Problem Statement: Given an integer array nums and a target value, find three integers whose sum is closest to the target. Return the sum of those three numbers. Example: Input: nums = [-1, 2, 1, -4], target = 1 Output: 2 Explanation: (-1 + 2 + 1 = 2) ⚙️ Approach: Sort the array for easy pointer movement. Use a for loop to fix one element at a time. Apply the two-pointer technique to find the closest sum. Compare differences using Math.abs() to track which combination is nearer to the target. 📘 Key Learning: 🔹 Sorting simplifies pointer logic. 🔹 Math.abs() is crucial for measuring closeness. 🔹 Sometimes, you don’t need the exact answer — just the closest one. #30DaysOfCode #LeetCode #Java #CodingJourney #ProblemSolving #DataStructures #Algorithms #TwoPointers #TechLearning
To view or add a comment, sign in
-
-
🚀 Day 8 of #45DaysOfLeetCode Challenge 😎 📌Today's problem: Palindrome Number (LeetCode #9) 💡 🔹 Concept: Check whether a given integer reads the same backward and forward — without converting it to a string! 🔹 Logic Used: Instead of reversing the entire number, I reversed only half of it to improve efficiency. This avoids unnecessary computation and eliminates integer overflow risks. 🔹 Key Learnings: ✅ Optimized logic using mathematical manipulation ✅ Improved understanding of number reversal techniques ✅ Importance of edge case handling (negative numbers, trailing zeros) ⚙️ Result: 💻 Runtime: 4 ms (Beats 100%) 💾 Memory: 44.84 MB 😎Small optimizations make a big difference in performance! 💪 📌Problem: https://lnkd.in/ef6AC2j6 🔥Each day is a step closer to writing cleaner and more optimized code. ✨ Let’s keep pushing forward and refining our problem-solving skills! 💻🔥 #LeetCode #Day8 #100DaysOfCode #ProblemSolving #Java #CodingChallenge #PalindromeNumber #DataStructures #Algorithms #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 47 of My LeetCode Journey 🚀 Problem: Sum of Square Numbers Topic: Math / Two Pointers 🧠 Approach: To check if a number c can be expressed as the sum of squares of two integers (a² + b² = c), we use the two-pointer technique: Start a = 0 and b = √c Calculate sum = a² + b² If sum == c → ✅ return true If sum < c → increase a If sum > c → decrease b Repeat until a <= b. ⏱️ Complexity: Time: O(√c) Space: O(1) This problem beautifully combines mathematical logic with an efficient two-pointer approach — clean and elegant! 💡 #100DaysOfCode #LeetCode #Java #CodingChallenge #ProblemSolving #DSA
To view or add a comment, sign in
-
-
✨ Day 101 of My DSA Challenge – Magnetic Force Between Two Balls 🔷 Problem: 1552. Magnetic Force Between Two Balls 🔷 Goal: Place m balls into sorted basket positions such that the minimum magnetic force (i.e., minimum distance between any two balls) is as large as possible. 🔷 Key Insight: This is another brilliant Binary Search on the Answer problem. Instead of directly computing distances, we binary search for the maximum possible minimum distance that still allows placing all balls. Here’s how the logic works: 1️⃣ Sort all basket positions. 2️⃣ Use binary search on the range of possible distances (0 to max(position) - min(position)). 3️⃣ For each middle distance mid, check feasibility using a greedy approach — place balls while maintaining at least mid distance apart. 4️⃣ If it’s possible → try a larger distance. If not → reduce the distance. 🔷 My Java Approach: Implemented a helper function isPossible() to check if we can place all balls for a given minimum distance. Used Binary Search to maximize that minimum distance. 🔷 Complexity: Time → O(n × log(maxDist)) Space → O(1) This problem beautifully blends sorting, binary search, and greedy placement — showing how abstract concepts like “searching on the answer” translate into real algorithmic reasoning. Every new day strengthens my foundation in Binary Search patterns, sharpening both logic and implementation clarity. 🚀 #Day101 #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #BinarySearch #GreedyAlgorithm #CodingChallenge #Programming #SoftwareEngineering #Algorithms #DataStructures #TechJourney #LearnToCode #CodeEveryday #EngineerMindset #DeveloperJourney #GrowthMindset #KeepLearning #ApnaCollege #AlphaBatch #ShraddhaKhapra
To view or add a comment, sign in
-
Explore related topics
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