Home30 Days Of CodeHackerrank Day 14 : scope 30 days of code solution

Hackerrank Day 14 : scope 30 days of code solution

Today we are going to solve Hackerrank Day 14 : scope 30 days of code solution in C++ , Java & Python.


Today we’re discussing scope.

The absolute difference between two integers, a and b, is written as |a – b|. The maximum absolute difference between two integers in a set of positive integers, elements, is the largest absolute difference between any two integers in elements.

The Difference class is started for you in the editor. It has a private integer array (elements) for storing N non-negative integers, and a public integer (maximumDifference) for storing the maximum absolute difference.


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 _elements instance variable.
  • computeDifference method that finds the maximum absolute difference between any 2 numbers in _elements and stores it in the maximumDifference instance variable.

Input Format

You are not responsible for reading any input from stdin. The locked Solution class in the editor reads in 2 lines of input. The first line contains N, the size of the elements array. The second line has N space-separated integers that describe the _elements array.


  • 1 <= N <= 10
  • 1 <= _elements[i] <= 100, where 0 <= i <= N – 1

Output Format

You are not responsible for printing any output; the Solution class will print the value of the maximumDifference instance variable.

Sample Input

STDIN   Function
-----   --------
3       __elements[] size N = 3
1 2 5   __elements = [1, 2, 5]

Sample Output



The scope of the _elements array and maximumDifference integer is the entire class instance. The class constructor saves the argument passed to the constructor as the _elements instance variable (where the computeDifference method can access it).

To find the maximum difference, computeDifference checks each element in the array and finds the maximum difference between any 2 elements: |1 – 2| = 1
|1 – 5| = 4
|2 – 5| = 3

The maximum of these differences is 4, so it saves the value 4 as the maximumDifference instance variable. The locked stub code in the editor then prints the value stored as maximumDifference, which is 4.

Hackerrank Day 14 : scope 30 days of code solution

Scope HackerRank Solution in C ++

// Add your code here
    Difference(vector<int> arr){
        elements = arr;
        sort(elements.begin(), elements.end());

    void computeDifference(){
        maximumDifference = abs(elements[elements.size()-1] - elements[0]);

Scope HackerRank Solution in Java

// Add your code here
    public Difference(int[] nums) {
        elements = nums;

    public void computeDifference() {
        maximumDifference = elements[elements.length - 1] - elements[0];

Scope HackerRank Solution in Python 3

 self.maximumDifference = 0
    def computeDifference(self):
        x = 101
        y = 0

        for item in self.__elements:
            if item < x:
                x = item
            if item > y:
                y = item

        self.maximumDifference = y - x 
	# Add your code here

NEXT : Hackerrank Day 15 : Linked List 30 days of code solution

30 Days of Code HackerRank Solutions List – Day 0 to Day 29

Read More –



Please enter your comment!
Please enter your name here

Most Popular

- Advertisment -