-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonotonicArray.java
50 lines (41 loc) · 926 Bytes
/
MonotonicArray.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
/*
Name: Monotonic Array
Source: LeetCode
Link: https://leetcode.com/problems/monotonic-array/
*/
import java.util.Scanner;
public class MonotonicArray {
public static void main(String [] args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int [] arr = new int[n];
for(int i=0; i<n ; i++)
{
arr[i] = scanner.nextInt();
}
System.out.println(isMonotonic(arr));
}
public static boolean isMonotonic(int[] A) {
int inc = 0, dec =0;
for(int i=1; i<A.length; i++)
{
if(A[i-1]<A[i])
{
inc++;
}
else if(A[i-1]>A[i])
{
dec++;
}
}
if(inc>0 && dec>0)
{
return false;
}
else
{
return true;
}
}
}