Array Rotation using Reverse Algorithm (Java) I recently solved an interesting array problem where the goal was to rotate an array efficiently without using extra space. Instead of shifting elements multiple times, I implemented the Reverse Algorithm, which optimizes the solution to O(n) time complexity and O(1) space. 🔹 Approach Used: 1 Reverse first d elements 2 Reverse remaining elements 3 Reverse the entire array
Java Array Rotation with Reverse Algorithm
More Relevant Posts
-
𝐃𝐚𝐲 𝟔𝟔/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐄𝐪𝐮𝐚𝐥 𝐒𝐮𝐦 𝐆𝐫𝐢𝐝 𝐏𝐚𝐫𝐭𝐢𝐭𝐢𝐨𝐧 𝐈 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. 💪 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: First, compute the total sum of the grid. If it's odd, partition is not possible. Then try splitting: Horizontally → accumulate row sums Vertically → accumulate column sums Check if any split gives half of the total sum. 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Prefix sum + greedy partition check. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐦 × 𝐧) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝟏) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: When partitioning grids, checking prefix sums along rows or columns can quickly determine valid splits. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #Matrix #PrefixSum #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
Day 30/75 — Remove Nth Node From End of List Today's problem focused on linked list manipulation using the two-pointer technique. Goal: Remove the nth node from the end of a singly linked list. Approach: • Use a dummy node to simplify edge cases • Move a fast pointer n + 1 steps ahead • Move both pointers together until fast reaches the end • The slow pointer will be just before the node to remove Then update: slow.next = slow.next.next Time Complexity: O(n) Space Complexity: O(1) A classic example of how the two-pointer technique simplifies linked list problems. 30/75 🚀 #Day30 #DSA #LinkedList #TwoPointers #Java #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 41 of #100DaysOfCode Solved 189. Rotate Array on LeetCode 🔄 🧠 Key Insight: Rotating an array by k steps to the right means the last k elements move to the front, while the remaining elements shift to the right. ⚙️ Approach: 1️⃣ Compute effective rotations using k % n 2️⃣ Store the last k elements in a temporary array 3️⃣ Shift the remaining n-k elements to the right 4️⃣ Copy the stored elements back to the beginning of the array This ensures the rotation happens in-place with controlled extra space. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(k) #100DaysOfCode #LeetCode #DSA #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
LeetCode 24 — Swap Nodes in Pairs This problem gives the head of a linked list. The task is to swap every two adjacent nodes and return the head of the modified list. One important constraint is that the node values cannot be modified — only the nodes themselves can be rearranged. Example : Input : 1 → 2 → 3 → 4 Output : 2 → 1 → 4 → 3 Each pair of adjacent nodes is swapped while keeping the rest of the list structure intact. Approach used — Pairwise Pointer Manipulation A dummy node is placed before the head to simplify pointer operations, especially for the first pair. During traversal : - first represents the first node of the pair - second represents the second node of the pair For every pair : - The first node is connected to the node after the pair. - The second node is linked before the first node. - The previous node is connected to the new head of the pair. After swapping, the pointer moves forward to process the next pair. This process continues until fewer than two nodes remain. This approach swaps nodes in O(n) time with O(1) extra space. #leetcode #datastructures #linkedlist #java #problemSolving
To view or add a comment, sign in
-
-
Optimizing Sorting Logic Using Bit Manipulation (Java) Today I solved a problem that required sorting integers based on the number of set bits in their binary representation. Instead of using a direct sorting approach, I implemented a custom comparator with a PriorityQueue and used Brian Kernighan’s algorithm to count set bits efficiently: n = n & (n - 1) This reduces time complexity for counting bits to O(number of set bits). 📊 Result: • 77/77 test cases passed • Runtime: 7ms • Beat 78% of submissions Key takeaway: Optimization is not just about solving — it’s about choosing the right approach and reducing unnecessary operations. Bit manipulation continues to be one of the most powerful tools in algorithm design. #DSA #Java #BitManipulation #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 38 of DSA — Merge Two Sorted Lists 🔗 Today’s problem strengthened my understanding of pointer control in Linked Lists. 🔹 Problem: Merge two sorted linked lists into one sorted list. Instead of creating new nodes, I learned to: Compare values Rewire next pointers carefully Use a dummy node to simplify edge cases 💡 Key Insight: The dummy node is not part of the final answer. It simply preserves the starting reference while another pointer builds the list. Example: 1 → 2 → 4 1 → 3 → 4 Merged: 1 → 1 → 2 → 3 → 4 → 4 ⏱ Time Complexity: O(n + m) 📦 Space Complexity: O(1) #Day38 #DSA #LinkedList #Java #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Garbage Collection Does NOT Prevent Memory Leaks Biggest myth: “Java has GC, so no memory leaks.” Wrong. This leaks memory: static List<Object> cache = new ArrayList<>(); public void add() { cache.add(new Object()); // never released } GC only removes: - Unreachable objects It cannot remove: - Objects you still reference Real-world leaks: - Static collections - Listeners not removed - ThreadLocal misuse 💡 Takeaway: GC frees memory. It doesn’t fix bad design. #Java #GarbageCollection #MemoryLeaks #JVM
To view or add a comment, sign in
-
🚀 Day 10 – Find the Closest Pair from Two Arrays Today’s problem: Find a pair (one element from each sorted array) whose sum is closest to x. 🧠 Key Idea Since both arrays are sorted, we can use the Two Pointer Technique instead of brute force. 🔹 Approach Start i = 0 (beginning of first array) Start j = m - 1 (end of second array) Compute sum Track minimum difference |sum - x| If sum > x → move j-- Else → move i++ #geekstreak60 #npci #Java #geekforgeeks
To view or add a comment, sign in
-
-
Day 14/30 – Rotate Image 🚀 Problem: Rotate an n × n matrix by 90° clockwise (in-place). Trick: Rotation = 1️⃣ Transpose the matrix 2️⃣ Reverse each row No extra space used. Pure in-place transformation. Time Complexity: O(n²) Space Complexity: O(1) Matrix manipulation feels satisfying 🔥 #30DaysOfCode #Java #DSA #Matrix #ProblemSolving #InterviewPrep
To view or add a comment, sign in
-
-
Day 32/75 — Add Two Numbers (Linked List) Today's problem was about adding two numbers represented as linked lists. Each node contains a digit, and digits are stored in reverse order. Approach: • Traverse both lists simultaneously • Add corresponding digits along with carry • Store result digit in a new list • Continue until both lists and carry are exhausted Key condition: while (l1 != null || l2 != null || carry != 0) Time Complexity: O(max(n, m)) Space Complexity: O(max(n, m)) A classic problem combining linked list traversal + basic math logic. 32/75 🚀 #Day32 #DSA #LinkedList #Java #Algorithms #LeetCode
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