Hackerrank 30 days of code Solution: Day 14: Scope:
The next problem is based on Scope. In our solution to this problem we have used bubble sort to calculate the maximum absolute difference. You can use any sorting technique in which you are more comfortable like quick sort, merger sort etc..
Complete the Difference class by writing the following:
- A class constructor that takes an array of integers as a parameter and saves it to the instance variable.
- A computeDifference method that finds the maximum absolute difference between any numbers in and stores it in the instance variable.
3 1 2 5
Scope HackerRank solution in Python :
class Difference: def __init__(self, a): self.__elements = a def computeDifference(self): maximum = 0 for i in range(len(self.__elements)): for j in range(len(self.__elements)): absolute = abs(self.__elements[i] - self.__elements[j]) if absolute > maximum: maximum = absolute self.maximumDifference = maximum _ = input() a = [int(e) for e in input().split(' ')] d = Difference(a) d.computeDifference() print(d.maximumDifference)
Tip:- Always try to implement the own logic this will help you to solve and building a logic. Before copy the program I recommended please read this full article, this will help you to build your own logic.