HackerRank Day 14 Solution in Python: Scope

Today we will see the HackerRank Day 14 Solution in Python. The problem is named Scope, part of 30 Days of code on HackerRank. Let’s get started!

Day 14: Scope Problem statement

We are supposed to complete the Difference class by writing a computeDifference method that finds the maximum absolute difference(i.e., the difference between the maximum and minimum element of the array) between any numbers and stores it in the instance variable.

Sample Input

3      
1 2 5  

Sample Output

4

You can solve the problem here.

HackerRank Day 14 Solution in Python

class Difference:
    #Constructor of Difference class
    def __init__(self, a):
        self.__elements = a

	#Method to calculate maximum absolute difference
    def computeDifference(self):
        self.maximumDifference = abs(max(self.__elements) - min(self.__elements))
# End of Difference class

#User input for number of elements in array
_ = input()
#User input of array elements
a = [int(e) for e in input().split(' ')]

#Object of Difference class
d = Difference(a)
#Calling computeDifference method
d.computeDifference()

print(d.maximumDifference)

Code Explanation:

  • We create a Constructor for Difference class with the parameter as an array
  • Then we create a method computeDifference to calculate the absolute difference in the array elements
  • Then we print the absolute difference value as output

Also Read:

Share:

Author: Keerthana Buvaneshwaran