Skip to content

Commit a56c163

Browse files
authored
Merge pull request #791 from Manasi2001/issue-790
Maximum Difference
2 parents e0c5e36 + 32177d1 commit a56c163

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Arrays/Maximum_Difference.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
From all the positive integers entered in a list, the aim of the program is
3+
to subtract any two integers such that the result/output is the maximum
4+
possible difference.
5+
6+
'''
7+
8+
# class to compute the difference
9+
class Difference:
10+
def __init__(self, a):
11+
# getting all elements from the entered list
12+
self.__elements = a
13+
def computeDifference(self):
14+
# maximum difference would be the difference between the largest and the smallest integer
15+
Difference.maximumDifference = max(self.__elements)-min(self.__elements)
16+
return Difference.maximumDifference
17+
# end of Difference class
18+
19+
# getting the input
20+
_ = input()
21+
a = [int(e) for e in input().split(' ')]
22+
23+
# creating an object of the class
24+
d = Difference(a)
25+
# calling function 'computeDifference' to compute the difference
26+
d.computeDifference()
27+
28+
# printing the result
29+
print(d.maximumDifference)
30+
31+
'''
32+
33+
COMPLEXITY:
34+
35+
Time Complexity -> O(N)
36+
Space Complexity -> O(1)
37+
38+
Sample Input:
39+
3
40+
1 2 5
41+
42+
Sample Output:
43+
4
44+
45+
Explanation:
46+
Integer with max value--> 5
47+
Integer with min value--> 1
48+
Hence, maximum difference--> 5-1 = 4
49+
50+
'''

0 commit comments

Comments
 (0)