-
Notifications
You must be signed in to change notification settings - Fork 3
/
Problem-3.java
72 lines (43 loc) · 1.81 KB
/
Problem-3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* @author AkashGoyal
*/
/**
--------------------- Problem----------->> Searching in an array where adjacent differ by at most k
Problem Link :- https://practice.geeksforgeeks.org/problems/searching-in-an-array-where-adjacent-differ-by-at-most-k0456/1
Reference:- https://www.geeksforgeeks.org/searching-array-adjacent-differ-k/
*/
// Method:- 1
class Complete{
// Function for finding maximum and value pair
public static int search (int arr[], int n, int x, int k) {
//Complete the function
int i=0;
while(i<n)
{
if(arr[i]==x)
{
return i;
}
// we are adding (x-arr[i])/k in i because it gives us like min index for i where number can be present
//if we take max difference that is k.
i+=Math.max(1,(x-arr[i])/k);
}
return -1;
}
}
// ------------------------------------------------------------------------------------------------------------------------------------------
// Method:- 2
class Complete{
// Function for finding maximum and value pair
public static int search (int arr[], int n, int x, int k) {
//Complete the function
for(int i=0;i<n;i++)
{
if(x==arr[i])
{
return i;
}
}
return -1;
}
}