Skip to content

Commit fbd1bea

Browse files
authored
Merge branch 'main' into main
2 parents 551af9e + d191a7f commit fbd1bea

File tree

8 files changed

+552
-3
lines changed

8 files changed

+552
-3
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/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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
id: fair-candy-swap
3+
title: Fair Candy Swap
4+
sidebar_label: 888- Fair Candy Swap
5+
tags:
6+
- Array
7+
- Hash Table
8+
- Sorting
9+
description: Find one pair of candy boxes, one from Alice and one from Bob, to swap so they both end up with the same total number of candies.
10+
sidebar_position: 0888
11+
---
12+
13+
## Problem Description
14+
15+
Alice and Bob have a different total number of candies. You are given two integer arrays `aliceSizes` and bobSizes where `aliceSizes[i]` is the number of candies of the ith box of candy that Alice has and `bobSizes[j]` is the number of candies of the `jth` box of candy that Bob has.
16+
17+
Since they are friends, they would like to exchange one candy box each so that after the exchange, they both have the same total amount of candy. The total amount of candy a person has is the sum of the number of candies in each box they have.
18+
19+
Return an integer array answer where `answer[0]` is the number of candies in the box that Alice must exchange, and `answer[1]` is the number of candies in the box that Bob must exchange. If there are multiple answers, you may return any one of them. It is guaranteed that at least one answer exists.
20+
21+
### Example 1
22+
23+
- **Input:** `aliceSizes = [1,1], bobSizes = [2,2]`
24+
- **Output:** `[1,2]`
25+
26+
27+
### Constraints
28+
29+
- `1 <= aliceSizes.length, bobSizes.length <= 104`
30+
- `1 <= aliceSizes[i], bobSizes[j] <= 105`
31+
32+
## Approach
33+
34+
The solution first calculates the total number of candies each Alice and Bob have. Then, it sorts Bob's candy boxes for efficient searching. It iterates over Alice's candy boxes, checking if swapping a box with Bob can balance their total candies. For each box of Alice, it calculates the required box from Bob and uses binary search to find it in Bob's sorted array. If a valid swap is found, it updates the answer and returns it.
35+
36+
#### Java
37+
```Java
38+
class Solution {
39+
public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
40+
Arrays.sort(bobSizes);
41+
int aliceSum=0;
42+
int bobSum=0;
43+
for(int i=0;i<aliceSizes.length;i++){
44+
aliceSum+=aliceSizes[i];
45+
}
46+
for(int i=0;i<bobSizes.length;i++){
47+
bobSum+=bobSizes[i];
48+
}
49+
int [] ans=new int [2];
50+
51+
for(int i=0;i<aliceSizes.length;i++){
52+
if((bobSum-aliceSum+(2*aliceSizes[i]))%2!=0){
53+
break;
54+
}
55+
if(Arrays.binarySearch(bobSizes,(bobSum-aliceSum+(2*aliceSizes[i]))/2)>=0){
56+
ans[0]=aliceSizes[i];
57+
ans[1]=(bobSum-aliceSum+(2*aliceSizes[i]))/2;
58+
}
59+
else if(aliceSizes[i]==(bobSum-aliceSum+(2*aliceSizes[i]))/2){
60+
ans[0]=aliceSizes[i];
61+
ans[1]=aliceSizes[i];
62+
}
63+
}
64+
return ans;
65+
}
66+
}
67+
```
68+
69+
- Time Complexity
70+
The time complexity is $o(n)$.
71+
72+
- Space Complexity
73+
The space complexity is $O(1)$.

0 commit comments

Comments
 (0)