Skip to content

Commit 9d21f0c

Browse files
committed
solved 704. Binary Search
Signed-off-by: rajput-hemant <hemant.rajput_cs20@gla.ac.in>
1 parent bf6cfd7 commit 9d21f0c

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

TOPICWISE.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
| **0051** | [N-Queens][51] | Array, Backtracking | ![][hard] | |
2323
| **0075** | [Sort Colors][75] | Array, Two Pointers, Sorting | ![][medium] | |
2424
| **0283** | [Move Zeroes][283] | Array, Two Pointers | ![][easy] | |
25+
| **0704** | [Binary Search][704] | Array, Binary Search | ![][easy] | |
2526
| **1232** | [Check If It Is a Straight Line][1232] | Array, Math, Geometry | ![][easy] | |
2627
| **1480** | [Running Sum of 1d Array][1480] | Array, Prefix Sum | ![][easy] | |
2728
| **1537** | [Get the Maximum Score][1537] | Array, Two Pointer, Dynamic Programming, Greedy | ![][hard] | |
@@ -126,6 +127,7 @@
126127
| **0222** | [Count Complete Tree Nodes][222] | Binary Search, Tree, DFS, Binary Tree | ![][medium] | |
127128
| **0278** | [First Bad Version][278] | Binary Search, Interactive | ![][easy] | |
128129
| **0367** | [Valid Perfect Square][367] | Math, Binary Search | ![][easy] | |
130+
| **0704** | [Binary Search][704] | Array, Binary Search | ![][easy] | |
129131

130132
## Matrix
131133

@@ -572,6 +574,7 @@
572574
[369]: ./src/0301-0400/369%20-%20Plus%20One%20Linked%20List/
573575
[543]: ./src/0501-0600/543%20-%20Diameter%20of%20Binary%20Tree/
574576
[653]: ./src/0601-0700/653%20-%20Two%20Sum%20IV%20-%20Input%20is%20a%20BST/
577+
[704]: ./src/0701-0800/704%20-%20Binary%20Search/
575578
[1047]: ./src/1001-1100/1047%20-%20Remove%20All%20Adjacent%20Duplicates%20In%20String/
576579
[1232]: ./src/1201-1300/1232%20-%20Check%20If%20It%20Is%20a%20Straight%20Line/
577580
[1461]: ./src/1401-1500/1461%20-%20Check%20If%20a%20String%20Contains%20All%20Binary%20Codes%20of%20Size%20K/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class BinarySearch {
2+
public int search(int[] nums, int target) {
3+
int low = 0,
4+
mid = 0,
5+
high = nums.length - 1;
6+
while (low <= high) {
7+
mid = low + (high - low) / 2;
8+
if (nums[mid] == target)
9+
return mid;
10+
else if (nums[mid] < target)
11+
low = mid + 1;
12+
else
13+
high = mid - 1;
14+
}
15+
return -1;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 704. Binary Search [![share]](https://leetcode.com/problems/binary-search)
2+
3+
## Problem Statement:
4+
5+
Given an array of integers `nums` which is sorted in ascending order, and an integer `target`, write a function to search `target` in `nums`. If `target` exists, then return its index. Otherwise, return `-1`.
6+
7+
You must write an algorithm with `O(log n)` runtime complexity.
8+
9+
### Example 1:
10+
11+
```Input: nums = [-1,0,3,5,9,12], target = 9
12+
Output: 4
13+
Explanation: 9 exists in nums and its index is 4
14+
```
15+
16+
### Example 2:
17+
18+
```
19+
Input: nums = [-1,0,3,5,9,12], target = 2
20+
Output: -1
21+
Explanation: 2 does not exist in nums so return -1
22+
```
23+
24+
### Constraints:
25+
26+
- 1 <= nums.length <= 10<sup>4</sup>
27+
- -10<sup>4</sup> < nums[i], target < 10<sup>4</sup>
28+
- All the integers in nums are unique.
29+
- nums is sorted in ascending order.
30+
31+
## Solution:
32+
33+
### [_Java_](./BinarySearch.java)
34+
35+
```java
36+
public int search(int[] nums, int target) {
37+
int low = 0,
38+
mid = 0,
39+
high = nums.length - 1;
40+
while (low <= high) {
41+
mid = low + (high - low) / 2;
42+
if (nums[mid] == target)
43+
return mid;
44+
else if (nums[mid] < target)
45+
low = mid + 1;
46+
else
47+
high = mid - 1;
48+
}
49+
return -1;
50+
}
51+
```
52+
53+
### [_..._]()
54+
55+
```
56+
57+
```
58+
59+
<!----------------------------------{ link }--------------------------------->
60+
61+
[share]: https://img.icons8.com/external-anggara-blue-anggara-putra/20/000000/external-share-user-interface-basic-anggara-blue-anggara-putra-2.png
62+
[easy]: https://img.shields.io/badge/Difficulty-Easy-yellow.svg

src/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
| **0369** | [Plus One Linked List][369] | Linked List, Math | ![][medium] | 🔒 |
3838
| **0543** | [Diameter of Binary Tree][543] | Tree, DFS, Binary Tree | ![][easy] | |
3939
| **0653** | [Two Sum IV - Input is a BST][653] | Tree, DFS, BST, Binary Tree | ![][easy] | |
40+
| **0704** | [Binary Search][704] | Array, Binary Search | ![][easy] | |
4041
| **1047** | [Remove All Adjacent Duplicates In String][1047] | String, Stack | ![][easy] | |
4142
| **1232** | [Check If It Is a Straight Line][1232] | Array, Math, Geometry | ![][easy] | |
4243
| **1461** | [Check If a String Contains All Binary Codes of Size K][1461] | String, Hash Table, Bit Manipulation | ![][medium] | |
@@ -68,10 +69,11 @@
6869
[278]: ./0201-0300/278%20-%20First%20Bad%20Version/
6970
[283]: ./0201-0300/283%20-%20Move%20Zeroes/
7071
[344]: ./0301-0400/344%20-%20Reverse%20String/
71-
[367]:./0301-0400/367%20-%20Valid%20Perfect%20Square/
72+
[367]: ./0301-0400/367%20-%20Valid%20Perfect%20Square/
7273
[369]: ./0301-0400/369%20-%20Plus%20One%20Linked%20List/
7374
[543]: ./0501-0600/543%20-%20Diameter%20of%20Binary%20Tree/
7475
[653]: ./0601-0700/653%20-%20Two%20Sum%20IV%20-%20Input%20is%20a%20BST/
76+
[704]: ./0701-0800/704%20-%20Binary%20Search/
7577
[1047]: ./1001-1100/1047%20-%20Remove%20All%20Adjacent%20Duplicates%20In%20String/
7678
[1232]: ./1201-1300/1232%20-%20Check%20If%20It%20Is%20a%20Straight%20Line/
7779
[1461]: ./1401-1500/1461%20-%20Check%20If%20a%20String%20Contains%20All%20Binary%20Codes%20of%20Size%20K/

0 commit comments

Comments
 (0)