-
Notifications
You must be signed in to change notification settings - Fork 30
/
Clippd 2.py
104 lines (59 loc) · 1.42 KB
/
Clippd 2.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
Problem Description
Given an integer array A of size N. Return 1 if the array can be rearranged to form an arithmetic progression, otherwise, return 0.
A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.
Problem Constraints
2 <= N <= 105
-109 <= A[i] <= 109
Input Format
First and only argument is an integer array A of size N.
Output Format
Return 1 if the array can be rearranged to form an arithmetic progression, otherwise, return 0
Example Input
Input 1:
A = [3, 5, 1]
Input 2:
A = [2, 4, 1]
Example Output
Output 1:
1
Output 2:
0
Example Explanation
Explanation 1:
We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.
Explanation 2:
There is no way to reorder the elements to obtain an arithmetic progression.
"""
[1,2,4,3,5,2]
def quick(s,e,A):
if(s >= e):
return [ A[s] ]
x = A[e]
i =0
j = e-1
a = []
b = []
while(i<j):
if(A[i]>x):
a.append(A[i])
else:
b.append(A[i])
a = quick(s,i-1,a)
b = quick(i+1,e,b)
# print(a)
a.append(x)
return a+b
A = [1, 2, 4, 3]
A = quick(0,len(A)-1,A)
print(A)
d = A[1]-A[0]
f = False
for i in range(2,len(A)):
if (d != A[i]-A[i-1]):
f = True
break
if f:
print(0)
else:
print(1)