|
| 1 | +--- |
| 2 | +id: sorted-arrray-search |
| 3 | +title: Sorted Array Search |
| 4 | +sidebar_label: Sorted-Array-Search |
| 5 | +tags: |
| 6 | + - Searching |
| 7 | + - Binary Search |
| 8 | + - Algorithms |
| 9 | +description: "This tutorial covers the solution to the Sorted Array Search problem from the GeeksforGeeks website." |
| 10 | +--- |
| 11 | +## Problem Description |
| 12 | +Given an array `arr[]` sorted in ascending order of size `N` and an integer `K`. Check if `K` is present in the array or not. |
| 13 | + |
| 14 | +## Examples |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +``` |
| 19 | +Input: |
| 20 | +N = 5, K = 6 |
| 21 | +arr[] = {1,2,3,4,6} |
| 22 | +Output: 1 |
| 23 | +Exlpanation: Since, 6 is present in |
| 24 | +the array at index 4 (0-based indexing), |
| 25 | +output is 1. |
| 26 | +``` |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | + |
| 30 | +``` |
| 31 | +Input: |
| 32 | +N = 5, K = 2 |
| 33 | +arr[] = {1,3,4,5,6} |
| 34 | +Output: -1 |
| 35 | +Exlpanation: Since, 2 is not present |
| 36 | +in the array, output is -1. |
| 37 | +``` |
| 38 | + |
| 39 | +## Your Task |
| 40 | + |
| 41 | +You don't need to read input or print anything. Complete the function searchInSorted() which takes the sorted array arr[], its size N and the element K as input parameters and returns 1 if K is present in the array, else it returns -1. |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +Expected Time Complexity: O(Log N) |
| 46 | + |
| 47 | +Expected Auxiliary Space: O(1) |
| 48 | + |
| 49 | +## Constraints |
| 50 | + |
| 51 | +* `1 <= arr[i] <= 10^6` |
| 52 | + |
| 53 | +## Problem Explanation |
| 54 | + |
| 55 | +The task is to traverse the array and find the reuired element. |
| 56 | + |
| 57 | +## Code Implementation |
| 58 | + |
| 59 | +### C++ Solution |
| 60 | + |
| 61 | +```cpp |
| 62 | +class Solution{ |
| 63 | + public: |
| 64 | + // Function to find element in sorted array |
| 65 | + // arr: input array |
| 66 | + // N: size of array |
| 67 | + // K: element to be searche |
| 68 | + int searchInSorted(int arr[], int N, int K) |
| 69 | + { |
| 70 | + int cnt = 0; |
| 71 | + for(int i=0; i<N; i++){ |
| 72 | + if(arr[i]==K) { |
| 73 | + cnt = 1; |
| 74 | + break; |
| 75 | + |
| 76 | + } |
| 77 | + } |
| 78 | + if(cnt==1) return 1; |
| 79 | + return -1; |
| 80 | + |
| 81 | + } |
| 82 | +}; |
| 83 | +``` |
| 84 | + |
| 85 | +```java |
| 86 | +public class Solution { |
| 87 | + public int searchInSorted(int[] arr, int N, int K) { |
| 88 | + for (int i = 0; i < N; i++) { |
| 89 | + if (arr[i] == K) { |
| 90 | + return 1; |
| 91 | + } |
| 92 | + } |
| 93 | + return -1; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +``` |
| 98 | + |
| 99 | +```python |
| 100 | + |
| 101 | +class Solution: |
| 102 | + def searchInSorted(self, arr, N, K): |
| 103 | + for i in range(N): |
| 104 | + if arr[i] == K: |
| 105 | + return 1 |
| 106 | + return -1 |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | +```javascript |
| 111 | +class Solution { |
| 112 | + searchInSorted(arr, N, K) { |
| 113 | + for (let i = 0; i < N; i++) { |
| 114 | + if (arr[i] === K) { |
| 115 | + return 1; |
| 116 | + } |
| 117 | + } |
| 118 | + return -1; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +``` |
| 123 | + |
| 124 | +```typescript |
| 125 | +class Solution { |
| 126 | + searchInSorted(arr: number[], N: number, K: number): number { |
| 127 | + for (let i = 0; i < N; i++) { |
| 128 | + if (arr[i] === K) { |
| 129 | + return 1; |
| 130 | + } |
| 131 | + } |
| 132 | + return -1; |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +## Solution Logic: |
| 138 | + |
| 139 | +1. Iterate through the sorted array from the beginning to the end. |
| 140 | +2. Check if the current element is equal to the target element (K). |
| 141 | +3. If it is, return 1 to indicate that the element is found. |
| 142 | +4. If the loop completes without finding the element, return -1 to indicate that the element is not found. |
| 143 | + |
| 144 | + |
| 145 | +## Time Complexity |
| 146 | + |
| 147 | +* The time complexity is $O(N)$, the algorithm iterates through the array once, where N is the size of the array.Each iteration performs a constant amount of work, so the time complexity is linear. |
| 148 | + |
| 149 | + |
| 150 | +## Space Complexity |
| 151 | + |
| 152 | +* The auxiliary space complexity is $O(1)$ due to the algorithm only uses a fixed amount of space to store the indices and the target element. |
0 commit comments