#Coding5 Q: Find the Missing Number in an Array (Java) Example: int[] arr = {0, 1, 3, 4}; Output → 2 I explored 3 different approaches: ✅ 1. Using Sum Formula (O(n)) int n = arr.length; int expectedSum = n * (n + 1) / 2; int actualSum = 0; for(int num : arr) { actualSum += num; } int missing = expectedSum - actualSum; ✅ 2. Using XOR (O(n), avoids overflow) int xor = 0; for(int i = 0; i <= arr.length; i++) { xor ^= i; } for(int num : arr) { xor ^= num; } int missing = xor; ✅ 3. Using Sorting (O(n log n)) Arrays.sort(arr); for(int i = 0; i < arr.length; i++) { if(arr[i] != i) { System.out.println(i); break; } } Key Learnings: Sum and XOR approaches are interview preferred XOR avoids integer overflow Sorting is easier to understand but less optimal Practicing DSA daily to strengthen problem-solving skills #DSA #Java #Arrays #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic #LinkedInDSA
Find Missing Number in Java Array with 3 Approaches
More Relevant Posts
-
#Coding6 Q: Remove Duplicates from Sorted Array (Java) Example: int[] arr = {1, 1, 2, 2, 3, 4, 4}; Output → {1, 2, 3, 4} Count → 4 I explored 2 different approaches: ✅ 1. Two Pointer Technique (Optimal – O(n), O(1)) static int removeDuplicates(int[] arr) { if(arr.length == 0) return 0; int j = 1; for(int i = 1; i < arr.length; i++) { if(arr[i] != arr[i - 1]) { arr[j++] = arr[i]; } } return j; // number of unique elements } ✅ 2. Using HashSet (Extra Space) Set<Integer> set = new LinkedHashSet<>(); for(int num : arr) set.add(num); int k = 0; for(int num : set) { arr[k++] = num; } Key Learnings: Two-pointer approach is interview preferred (in-place, no extra memory) HashSet approach is simpler but uses extra space Sorted property makes duplicate removal efficient Practicing DSA daily to strengthen problem-solving skills #DSA #Java #Arrays #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic #LinkedInDSA
To view or add a comment, sign in
-
#Coding3 Q: Reverse an array(Java). Example: int[] arr = {2, 7, 4, 3, 9}; Output → {9, 3, 4, 7, 2} I explored 3 different approaches: ✅ 1. Two Pointer Technique (Optimal – O(n), O(1)) int left = 0; int right = arr.length - 1; while(left < right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } ✅ 2. Using Extra Array (O(n) space) int[] arr = {2, 7, 4, 3, 9}; int[] newArray = new int[arr.length]; int j = 0; for(int i = arr.length - 1; i >= 0; i--) { newArray[j++] = arr[i]; } ✅ 3. Using Streams + Collections List<Integer> newArray = Arrays.stream(arr) .boxed() .collect(Collectors.toList()); Collections.reverse(newArray); Key Learnings: Two-pointer approach is interview preferred (no extra memory) Extra array is simple but uses additional space Streams approach is clean but not optimal Practicing fundamentals consistently to strengthen problem-solving skills #DSA #Java #Arrays #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic #LinkedInDSA
To view or add a comment, sign in
-
#Coding4 Q: Check whether a given array is sorted in ascending order. Example: {1, 3, 5, 7, 9} → ✅ Sorted {1, 5, 3, 7} → ❌ Not Sorted I explored 3 different approaches: ✅ 1. Single Pass (Optimal – O(n)) static boolean isSorted(int[] arr) { for(int i = 0; i < arr.length - 1; i++) { if(arr[i] > arr[i + 1]) return false; } return true; } ✅ 2. Using Streams boolean sorted = IntStream.range(0, arr.length - 1).allMatch(i -> arr[i] <= arr[i + 1]); ✅ 3. Compare with Sorted Copy int[] copy = arr.clone(); Arrays.sort(copy); boolean isSorted = Arrays.equals(copy, arr); Key Learnings: Single-pass solution is interview preferred (O(n), O(1)) Streams provide a functional style Sorting approach is simpler but less optimal Practicing fundamentals daily to strengthen problem-solving skills #DSA #Java #Arrays #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic #LinkedInDSA
To view or add a comment, sign in
-
Day 24 – Search in Rotated Sorted Array Worked on searching a target in a rotated sorted array using modified Binary Search. Key Learnings: Identifying which half of the array is sorted Deciding the correct side to continue search Maintaining O(log n) efficiency #DSA #RotatedArray #BinarySearch #CodingPractice #Java
To view or add a comment, sign in
-
-
#Coding10 👉 Q: Remove Duplicates from a Sorted Array (Two Pointer Technique – Java). Example: int[] arr = {1, 1, 2, 2, 3, 4, 4}; Output → {1, 2, 3, 4} Count → 4 ✅ Two Pointer Technique (Optimal – O(n), O(1)) static int removeDuplicates(int[] arr) { if(arr.length == 0) return 0; int j = 1; // pointer for next unique position for(int i = 1; i < arr.length; i++) { if(arr[i] != arr[i - 1]) { arr[j] = arr[i]; j++; } } return j; // number of unique elements } How It Works Since the array is already sorted, duplicates are adjacent i scans the array j tracks the position to place the next unique element When a new element is found → place it at index j Complexity Analysis • Time Complexity → O(n) • Space Complexity → O(1) (In-place solution) Two-pointer pattern is extremely powerful for array problems. #DSA #Java #Arrays #TwoPointers #ProblemSolving #InterviewPrep #LearningInPublic #LinkedInDSA
To view or add a comment, sign in
-
#Coding7 Q: Rotate an Array (Java) Example: int[] arr = {1, 2, 3, 4, 5, 6, 7} k = 3 Output → {5, 6, 7, 1, 2, 3, 4} I explored 3 different approaches: ✅ 1. Reverse Method (Optimal – O(n), O(1)) static void rotate(int[] arr, int k) { k = k % arr.length; reverse(arr, 0, arr.length - 1); reverse(arr, 0, k - 1); reverse(arr, k, arr.length - 1); } static void reverse(int[] arr, int l, int r) { while(l < r) { int temp = arr[l]; arr[l] = arr[r]; arr[r] = temp; l++; r--; } } ✅ 2. Using Extra Array (O(n) space) int n = arr.length; int[] temp = new int[n]; for(int i = 0; i < n; i++) { temp[(i + k) % n] = arr[i]; } arr = temp; ✅ 3. Brute Force Approach (O(n × k)) for(int i = 0; i < k; i++) { int last = arr[arr.length - 1]; for(int j = arr.length - 1; j > 0; j--) { arr[j] = arr[j - 1]; } arr[0] = last; } Key Learnings: Reverse method is interview preferred (in-place, no extra memory) Extra array approach is easier but uses additional space Brute force works but is inefficient for large k #DSA #Java #Arrays #ProblemSolving #CodingJourney #InterviewPrep #LearningInPublic #LinkedInDSA
To view or add a comment, sign in
-
Day 25 – Find Minimum in Rotated Sorted Array Solved this rotated array problem using a modified Binary Search approach in O(log n). Key Learning: By comparing mid with right, I identified which half contains the minimum and reduced the search space efficiently. #DSA #BinarySearch #RotatedArray #Java #InterviewPrep
To view or add a comment, sign in
-
-
🔁 Reversing a Linked List in Java — Understanding the Logic Behind It While practicing DSA, I worked on a classic but powerful problem: Reversing a Singly Linked List 🧠 Problem Insight In a singly linked list, each node points only to the next node. To reverse it, we need every node to instead point to its previous node — without losing the rest of the list during the process. ⚙️ Approach (Iterative, In-Place) I solved this using three pointers: 1️⃣ Previous Pointer (prev) Keeps track of the node that should come *before* the current node in the reversed list. 2️⃣ Current Pointer (curr) The node we are currently processing. 3️⃣ Next Pointer (next) Temporarily stores the next node so we don’t lose the remaining list when we change links. 🔄 Step-by-Step Logic * Start with `prev = null` and `curr = head` * For each node: * Save the next node * Reverse the link (point current node to previous) * Move previous forward * Move current forward * When the loop ends, *)prev becomes the new head of the reversed list 🚀 Key Takeaways ✔ Strengthened my understanding of pointer manipulation ✔ Learned how to update links without using extra memory ✔ Reinforced the importance of step-by-step state tracking in DSA problems Sometimes the biggest growth comes from deeply understanding a “simple” problem. Always open to discussions on better or alternative approaches! 🙌 #Java #DataStructures #LinkedList #DSA #ProblemSolving #CodingJourney #SoftwareEngineering #LearningInPublic
To view or add a comment, sign in
-
𝗗𝗮𝘆 𝟳/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲🎯 Solved Longest Valid Parentheses ➤ Problem: Given a string of ‘(’ and ‘)’, determine the length of the longest well-formed (valid) parentheses substring. ➤ Approach: Used a clean and structured stack-based technique to identify valid boundaries efficiently. • Initialize the stack with -1 as the base index. • Push every '(' index onto the stack. • On encountering ')', pop to attempt a match. • If the stack becomes empty, push the current index as the new starting point. • Otherwise, compute the valid substring length using currentIndex − stackTop. Continuously update the maximum length. This ensures each character is processed in a single pass, maintaining optimal efficiency. #LeetCode #Java #DSA #TwoPointers #ArrayManipulation #ProblemSolving #20DaysChallenge #Consistency
To view or add a comment, sign in
-
-
#Coding9 Q: Move Zeros to the End (Two Pointer Technique – Java) Example: int[] arr = {0, 1, 0, 3, 12}; Output → {1, 3, 12, 0, 0} ✅ Two Pointer Technique (Optimal – O(n), O(1)) static void moveZeros(int[] arr) { int j = 0; // position to place next non-zero for(int i = 0; i < arr.length; i++) { if(arr[i] != 0) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; j++; } } } How It Works i scans the entire array j tracks where the next non-zero should go When a non-zero is found → swap with index j Zeros automatically move toward the end Complexity Analysis • Time Complexity → O(n) • Space Complexity → O(1) (In-place solution) This approach is interview-preferred because it is efficient and preserves order. #DSA #Java #Arrays #TwoPointers #ProblemSolving #InterviewPrep #LearningInPublic #LinkedInDSA
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