Skip to content

Commit a8dbf0f

Browse files
Monotonic Array
1 parent 166a0c4 commit a8dbf0f

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

10X/Python/Array/Monotonic_Array.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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)

0 commit comments

Comments
 (0)