Skip to content

Commit cbf5d0f

Browse files
authored
Merge branch 'main' into main
2 parents 7d1b927 + 88db8ed commit cbf5d0f

File tree

12 files changed

+697
-23
lines changed

12 files changed

+697
-23
lines changed

assets/Jump_Search.jpg

18.4 KB
Loading

docs/NextJs/_category_.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"label": "Next",
2+
"label": "NextJs",
33
"position": 12,
44
"link": {
55
"type": "generated-index",

docs/tailwind/_category_.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"label": "TailwindCSS",
2+
"label": "Tailwind",
33
"position": 8,
44
"link": {
55
"type": "generated-index",

dsa-problems/leetcode-problems/0100-0199.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export const problems = [
386386
"problemName": "162. Find Peak Element",
387387
"difficulty": "Medium",
388388
"leetCodeLink": "https://leetcode.com/problems/find-peak-element/",
389-
"solutionLink": "#"
389+
"solutionLink": "/dsa-solutions/lc-solutions/0100-0199/Find-Peak-Element"
390390
},
391391
{
392392
"problemName": "163. Missing Ranges Premium",

dsa-problems/leetcode-problems/0700-0799.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ export const problems = [
440440
"problemName": "771. Jewels and Stones",
441441
"difficulty": "Easy",
442442
"leetCodeLink": "https://leetcode.com/problems/jewels-and-stones",
443-
"solutionLink": "#"
443+
"solutionLink": "/dsa-solutions/lc-solutions/0700-0799/jewels-and-stones"
444444
},
445445
{
446446
"problemName": "772. Basic Calculator III",

dsa-problems/leetcode-problems/2200-2299.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export const problems = [
224224
"problemName": "2235. Add Two Integers",
225225
"difficulty": "Easy",
226226
"leetCodeLink": "https://leetcode.com/problems/add-two-integers",
227-
"solutionLink": "#"
227+
"solutionLink": "/dsa-solutions/lc-solutions/2200-2299/add-two-integers"
228228
},
229229
{
230230
"problemName": "2236. Root Equals Sum of Children",
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
---
2+
id: Jump-Search
3+
title: Jump Search (Geeks for Geeks)
4+
sidebar_label: Jump Search
5+
tags:
6+
- Intermediate
7+
- Search Algorithms
8+
- Geeks for Geeks
9+
- CPP
10+
- Python
11+
- Java
12+
- JavaScript
13+
- DSA
14+
description: "This is a solution to the Jump Search problem."
15+
---
16+
17+
## What is Jump Search?
18+
19+
Jump Search is an efficient search algorithm for sorted arrays. It works by jumping ahead by fixed steps and then performing a linear search within a block, making it faster than linear search but less complex than binary search.
20+
21+
## Algorithm for Jump Search
22+
23+
1. Calculate the optimal step size $\sqrt{N}$, where $N$ is the length of the list.
24+
2. Start from the first element and jump ahead by the step size until the target element is greater than or equal to the current element.
25+
3. Perform a linear search within the identified block.
26+
4. If the target element is found, return its index.
27+
5. If the target element is not found, return -1.
28+
29+
## How does Jump Search work?
30+
31+
- It calculates a jump step based on the length of the list.
32+
- It jumps ahead in blocks, comparing the target value with the current element at each step.
33+
- Once the block where the target might be located is identified, a linear search within the block is performed.
34+
35+
![Example for Jump Search](../../assets/Jump_Search.jpg)
36+
37+
## Problem Description
38+
39+
Given a sorted list and a target element, implement the Jump Search algorithm to find the index of the target element in the list. If the element is not present, return -1.
40+
41+
## Examples
42+
43+
**Example 1:**
44+
Input:
45+
list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
46+
target = 6
47+
Output: 6
48+
49+
50+
**Example 2:**
51+
Input:
52+
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
53+
target = 15
54+
Output: -1
55+
56+
57+
## Your Task:
58+
59+
You don't need to read input or print anything. Complete the function jump_search() which takes arr[], N and K as input parameters and returns the index of K in the array. If K is not present in the array, return -1.
60+
61+
Expected Time Complexity: $O(\sqrt{N})$
62+
Expected Auxiliary Space: $O(1)$
63+
64+
## Constraints
65+
66+
- $1 <= N <= 10^5$
67+
- $1 <= arr[i] <= 10^6$
68+
- $1 <= K <= 10^6$
69+
70+
## Implementation
71+
72+
<Tabs>
73+
<TabItem value="Python" label="Python" default>
74+
75+
```python
76+
import math
77+
78+
def jump_search(lst, target):
79+
length = len(lst)
80+
step = int(math.sqrt(length))
81+
prev = 0
82+
83+
while lst[min(step, length) - 1] < target:
84+
prev = step
85+
step += int(math.sqrt(length))
86+
if prev >= length:
87+
return -1
88+
89+
for i in range(prev, min(step, length)):
90+
if lst[i] == target:
91+
return i
92+
return -1
93+
```
94+
</TabItem>
95+
<TabItem value="C++" label="C++">
96+
97+
```cpp
98+
#include <iostream>
99+
#include <cmath>
100+
#include <vector>
101+
int jump_search(const std::vector<int>& lst, int target) {
102+
int length = lst.size();
103+
int step = sqrt(length);
104+
int prev = 0;
105+
while (lst[std::min(step, length) - 1] < target) {
106+
prev = step;
107+
step += sqrt(length);
108+
if (prev >= length) {
109+
return -1;
110+
}
111+
}
112+
113+
for (int i = prev; i < std::min(step, length); ++i) {
114+
if (lst[i] == target) {
115+
return i;
116+
}
117+
}
118+
return -1;
119+
}
120+
121+
int main() {
122+
std::vector<int> lst = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
123+
int target = 6;
124+
std::cout << "Index: " << jump_search(lst, target) << std::endl;
125+
return 0;
126+
}
127+
128+
```
129+
</TabItem>
130+
131+
<TabItem value="Java" label="Java">
132+
133+
```java
134+
import java.util.Arrays;
135+
136+
public class JumpSearch {
137+
public static int jumpSearch(int[] lst, int target) {
138+
int length = lst.length;
139+
int step = (int) Math.sqrt(length);
140+
int prev = 0;
141+
142+
while (lst[Math.min(step, length) - 1] < target) {
143+
prev = step;
144+
step += (int) Math.sqrt(length);
145+
if (prev >= length) {
146+
return -1;
147+
}
148+
}
149+
150+
for (int i = prev; i < Math.min(step, length); i++) {
151+
if (lst[i] == target) {
152+
return i;
153+
}
154+
}
155+
return -1;
156+
}
157+
158+
public static void main(String[] args) {
159+
int[] lst = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
160+
int target = 6;
161+
System.out.println("Index: " + jumpSearch(lst, target));
162+
}
163+
}
164+
165+
```
166+
167+
</TabItem>
168+
<TabItem value="JavaScript" label="JavaScript">
169+
170+
```javascript
171+
function jumpSearch(lst, target) {
172+
let length = lst.length;
173+
let step = Math.floor(Math.sqrt(length));
174+
let prev = 0;
175+
while (lst[Math.min(step, length) - 1] < target) {
176+
prev = step;
177+
step += Math.floor(Math.sqrt(length));
178+
if (prev >= length) {
179+
return -1;
180+
}
181+
}
182+
183+
for (let i = prev; i < Math.min(step, length); i++) {
184+
if (lst[i] === target) {
185+
return i;
186+
}
187+
}
188+
return -1;
189+
}
190+
191+
const lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
192+
const target = 6;
193+
console.log("Index:", jumpSearch(lst, target));
194+
```
195+
196+
</TabItem>
197+
</Tabs>
198+
199+
## Complexity Analysis
200+
201+
- **Time Complexity**: $O(\sqrt{n})$, where $n$ is the number of elements in the list. The list is divided into blocks, leading to a root-time complexity.
202+
- **Space Complexity**: $O(1)$, as no extra space is required apart from the input list.
203+
204+
## Advantages and Disadvantages
205+
206+
**Advantages:**
207+
- Faster than linear search for large sorted lists.
208+
- Simpler than binary search while still being efficient.
209+
210+
**Disadvantages:**
211+
- Requires the list to be sorted.
212+
- Less efficient compared to binary search in terms of time complexity.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
id: Find-Peak-Element
3+
title: Find Peak Element
4+
sidebar_label: 0162-Find-Peak-Element
5+
tags:
6+
- Array
7+
- Binary Search
8+
description: "A peak element is an element that is strictly greater than its neighbors.
9+
10+
Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.
11+
12+
You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.
13+
14+
You must write an algorithm that runs in O(log n) time."
15+
---
16+
17+
18+
### Problem Description
19+
20+
A peak element is an element that is strictly greater than its neighbors.
21+
22+
Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.
23+
24+
You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.
25+
26+
You must write an algorithm that runs in O(log n) time.
27+
28+
### Examples
29+
30+
#### Example 1
31+
32+
- **Input:** `nums = [1,2,3,1]`
33+
- **Output:** `2`
34+
35+
#### Example 2
36+
37+
- **Input:** `nums = [1,2,1,3,5,6,4]`
38+
- **Output:** `5`
39+
40+
### Constraints
41+
42+
- `1 <= nums.length <= 1000`
43+
- `-231 <= nums[i] <= 231 - 1`
44+
- `nums[i] != nums[i + 1]` for all valid `i`.
45+
46+
### Solution Code
47+
48+
#### Python
49+
50+
```python
51+
class Solution:
52+
def findPeakElement(self, nums: list[int]) -> int:
53+
left, right = 0, len(nums) - 1
54+
55+
while left < right:
56+
mid = (left + right) >> 1
57+
58+
if nums[mid - 1] <= nums[mid] >= nums[mid + 1]:
59+
return mid
60+
elif nums[mid] < nums[mid + 1]:
61+
left = mid + 1
62+
else:
63+
right = mid
64+
65+
return left
66+
```
67+
68+
#### Java
69+
70+
```java
71+
class Solution {
72+
public int findPeakElement(int[] nums) {
73+
int n=nums.length;
74+
if(n==1)return 0;
75+
if(nums[0]>nums[1])return 0;
76+
if(nums[n-1]>nums[n-2])return n-1;
77+
int low=1,high=n-2;
78+
while(low<=high){
79+
int mid=(low+high)/2;
80+
if(nums[mid]>nums[mid-1] && nums[mid]>nums[mid+1])return mid;
81+
else if(nums[mid]>nums[mid-1])low=mid+1;
82+
else high=mid-1;
83+
}
84+
return -1;
85+
}
86+
}
87+
```
88+
89+
#### C++
90+
91+
```cpp
92+
93+
class Solution {
94+
public:
95+
int findPeakElement(vector<int>& nums) {
96+
int low = 0, high = nums.size() - 1;
97+
while (low < high) {
98+
int mid = low + (high - low) / 2;
99+
if (nums[mid] < nums[mid + 1]) {
100+
low = mid + 1;
101+
} else
102+
high = mid;
103+
}
104+
return low;
105+
}
106+
};
107+
```
108+
#### Javascript
109+
110+
```javascript
111+
class Solution {
112+
public int findPeakElement(int[] nums) {
113+
int n = nums.length;
114+
if(n == 1)
115+
return 0;
116+
else if(nums[0] > nums[1])
117+
return 0;
118+
else if(nums[n-1] > nums[n-2])
119+
return n-1;
120+
int low = 1, high = n-2;
121+
while(low <= high) {
122+
int mid = low + (high - low)/2;
123+
if(nums[mid] > nums[mid-1] && nums[mid] > nums[mid+1])
124+
return mid;
125+
else if(nums[mid] < nums[mid-1])
126+
high = mid-1;
127+
else
128+
low = mid+1;
129+
}
130+
return -1;
131+
}
132+
}
133+
```

0 commit comments

Comments
 (0)