From fc87aed9a6dfb8851c13d3abb991f11e83ea8b86 Mon Sep 17 00:00:00 2001 From: Tanyajain2006 Date: Wed, 1 Oct 2025 19:51:56 +0530 Subject: [PATCH 1/2] Single Element in Sorted Array --- SingleElementInSortedArray/SingleElement.java | 0 SingleElementInSortedArray/intuition_algo.md | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 SingleElementInSortedArray/SingleElement.java create mode 100644 SingleElementInSortedArray/intuition_algo.md diff --git a/SingleElementInSortedArray/SingleElement.java b/SingleElementInSortedArray/SingleElement.java new file mode 100644 index 0000000..e69de29 diff --git a/SingleElementInSortedArray/intuition_algo.md b/SingleElementInSortedArray/intuition_algo.md new file mode 100644 index 0000000..e69de29 From a7b60c3b2e4ef635880a492e69d2071b9c316431 Mon Sep 17 00:00:00 2001 From: Tanyajain2006 Date: Wed, 1 Oct 2025 19:55:28 +0530 Subject: [PATCH 2/2] Single Element --- SingleElementInSortedArray/SingleElement.java | 56 +++++++++++++++++++ SingleElementInSortedArray/intuition_algo.md | 40 +++++++++++++ 2 files changed, 96 insertions(+) diff --git a/SingleElementInSortedArray/SingleElement.java b/SingleElementInSortedArray/SingleElement.java index e69de29..23d5990 100644 --- a/SingleElementInSortedArray/SingleElement.java +++ b/SingleElementInSortedArray/SingleElement.java @@ -0,0 +1,56 @@ +public class SingleElement { + public int singleNonDuplicate(int[] nums) { + int n = nums.length; + + // Case 1: Only one element in array + if (n == 1) { + return nums[0]; + } + + // Case 2: Unique element is at the beginning + if (nums[0] != nums[1]) { + return nums[0]; + } + + // Case 3: Unique element is at the end + if (nums[n - 1] != nums[n - 2]) { + return nums[n - 1]; + } + + // Binary search in between + int start = 1; + int end = n - 2; + while (start <= end) { + int mid = start + (end - start) / 2; + + // If nums[mid] is the unique element + if (nums[mid] != nums[mid + 1] && nums[mid] != nums[mid - 1]) { + return nums[mid]; + } + + // If mid is even and matches with next OR mid is odd and matches with previous, + // then unique element lies on the right half + if ((mid % 2 == 0 && nums[mid] == nums[mid + 1]) || + (mid % 2 == 1 && nums[mid] == nums[mid - 1])) { + start = mid + 1; + } + // Otherwise, it lies on the left half + else { + end = mid - 1; + } + } + return -1; // This case won't occur as per problem constraints + } + + // Main method to test + public static void main(String[] args) { + Solution sol = new Solution(); + int[] nums1 = {1, 1, 2, 3, 3, 4, 4, 8, 8}; + int[] nums2 = {3, 3, 7, 7, 10, 11, 11}; + int[] nums3 = {5}; + + System.out.println("Unique element in nums1: " + sol.singleNonDuplicate(nums1)); // Output: 2 + System.out.println("Unique element in nums2: " + sol.singleNonDuplicate(nums2)); // Output: 10 + System.out.println("Unique element in nums3: " + sol.singleNonDuplicate(nums3)); // Output: 5 + } +} diff --git a/SingleElementInSortedArray/intuition_algo.md b/SingleElementInSortedArray/intuition_algo.md index e69de29..f8dd793 100644 --- a/SingleElementInSortedArray/intuition_algo.md +++ b/SingleElementInSortedArray/intuition_algo.md @@ -0,0 +1,40 @@ +# Single Element in a Sorted Array + +## 🧠 Intuition +We are given a **sorted array** where every element appears exactly **twice**, except for **one element** that appears only once. + +Key observations: +1. Since the array is sorted, duplicate elements will always appear **in pairs**. +2. Before the unique element, the **first element of the pair** will appear at an **even index**, and the **second element** will appear at an **odd index**. + - Example: `[1, 1, 2, 2, 3, ...]` β†’ `(0,1), (2,3), ...` +3. After the unique element, this pattern **breaks**. + - Example: `[1, 1, 2, 3, 3, ...]` β†’ At index 2 (even), `2` does **not** have its pair next. + +Using this property, we can apply **binary search** to efficiently find the single element in **O(log n)** time. + +--- + +## βš™οΈ Algorithm + +1. **Handle edge cases**: + - If the array has only **one element**, return it. + - If the unique element is at the **start** or **end**, check the first/last two elements. + +2. **Binary Search**: + - Initialize `start = 1`, `end = n-2`. + - While `start <= end`: + - Find `mid = (start + end) / 2`. + - If `nums[mid]` is **not equal** to both neighbors, it’s the unique element. + - Otherwise, check parity (`mid % 2`) to decide the search direction: + - If `(mid is even and nums[mid] == nums[mid+1])` OR `(mid is odd and nums[mid] == nums[mid-1])`, then the unique element lies on the **right half** β†’ `start = mid + 1`. + - Else, search in the **left half** β†’ `end = mid - 1`. + +3. Return the found element. + +--- + +## ⏱️ Time and Space Complexity +- **Time Complexity**: `O(log n)` (binary search) +- **Space Complexity**: `O(1)` (constant extra space) + +--- \ No newline at end of file