-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrongestNeighbour.java
More file actions
39 lines (34 loc) · 891 Bytes
/
StrongestNeighbour.java
File metadata and controls
39 lines (34 loc) · 891 Bytes
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
public class StrongestNeighbour
{
static void maximumAdjacent(int sizeOfArray, int arr[])
{
for(int i=1;i<sizeOfArray;i++)
{
if(arr[i]>=arr[i-1])
{
System.out.print(arr[i]+" ");
}
else if(arr[i]<=arr[i-1])
{
System.out.print(arr[i-1]+" ");
/*
78,78,46,30
output:78,78,46
*/
}
}
}
//TC:O(N)
public static void main(String[] args)
{
int[] a={1,2,2,3,4,5};
int n=a.length;
maximumAdjacent(n, a);
/*
Maximum of arr[0] and arr[1]
is 2, that of arr[1] and arr[2] is 2, ...
and so on. For last two elements, maximum
is 5.
*/
}
}