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:
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- HackerRank Day 8 Solution in Python: Dictionaries and Maps
- HackerRank Day 7 Solution in Python: Arrays
- HackerRank Day 6 Solution in Python: Let’s review
- HackerRank Day 5 Solution in Python: Loops
- HackerRank Day 4 Solution in Python: Class vs Instance
- HackerRank Day 3 Solution in Python: Intro to Conditional Statements
- HackerRank Day 2 Solution in Python: Operators
- HackerRank Day 1 Solution in Python: Data Types
- HackerRank Day 0 Solution in Python: Hello World
- HackerRank Day 29 Solution in Python: Bitwise AND
- HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases
- HackerRank Day 27 Solution in Python: Testing
- HackerRank Day 26 Solution in Python: Nested Logic
- HackerRank Day 25 Solution in Python: Running Time and Complexity
- HackerRank Day 24 Solution in Python: More Linked Lists
- HackerRank Day 23 Solution in Python: BST Level Order Traversal
- HackerRank Day 22 Solution in Python: Binary Search Trees
- Find Peak Element LeetCode 162
- HackerRank Day 20 Solution in Python: Sorting
- HackerRank Day 19 Solution in Python: Interfaces
- HackerRank Day 18 Solution in Python: Queues and Stacks
- HackerRank Day 17 Solution in Python: More Exceptions
- HackerRank Day 16 Solution: Exceptions – String to Integer
- Explained: Allocate minimum number of pages
- HackerRank Day 15 Solution in Python: Linked List
- Search a 2D matrix: leetcode 74
- Maximum Subarray Sum: Kadane’s Algorithm
- HackerRank Day 13 Solution in Python: Abstract Classes
- HackerRank Day 14 Solution in Python: Scope