File tree 1 file changed +58
-0
lines changed
1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Monotonic Array
3
+ Given as array containing n integers, verify if the array is Monotonic.
4
+
5
+ An array is called monotonic if it is either monotone increasing or monotone decreasing. An array A is
6
+ monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j,
7
+ A[i] >= A[j].
8
+
9
+ Input
10
+ First line contains n, number of elements in the array.
11
+
12
+ Next n lines contains an integer in each line.
13
+
14
+ Output
15
+ Print True if given integers are Monotonic
16
+
17
+ Print False if given integers are not Monotonic
18
+
19
+ Example
20
+ Input:
21
+
22
+ 5
23
+
24
+ 3
25
+
26
+ 12
27
+
28
+ 34
29
+
30
+ 34
31
+
32
+ 56
33
+
34
+ Output:
35
+
36
+ True
37
+
38
+ """
39
+ n = int (input ())
40
+ a = []
41
+ incr = True
42
+ flag = True
43
+ for i in range (n ):
44
+ a .append (int (input ()))
45
+ for i in range (1 ,n ):
46
+ if (a [i - 1 ]<= a [i ]):
47
+ incr = True
48
+ else :
49
+ incr = False
50
+ if incr :
51
+ flag = True
52
+ else :
53
+ for i in range (1 ,n ):
54
+ if (a [i - 1 ]>= a [i ]):
55
+ flag = True
56
+ else :
57
+ flag = False
58
+ print (flag )
You can’t perform that action at this time.
0 commit comments