Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone
Hackerrank day 14 scope problem solution

Hackerrank Day 14 scope 30 days of code solution

YASH PAL, 31 July 20247 February 2026

In this HackerRank Day 14 scope 30 days of code problem, we have given A class constructor that takes an array of integers as a parameter and saves it to the instance variable. A computeDifference method finds the maximum absolute difference between any numbers and stores it in the instance variable.

Objective
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.

Task
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.
A 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.

Constraints

  • 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.

Problem solution in Python 2.

	# Add your code here
    def computeDifference(self):
        maxDiff = 0
        arr = self.__elements
        for i in range(len(arr)):
            for j in range(i+1, len(arr)):
                if abs(arr[j] - arr[i]) > maxDiff:
                    maxDiff = abs(arr[j] - arr[i])
        self.maximumDifference = maxDiff

Problem 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

Problem solution in java.

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

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

Problem 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]);
    }

30 days of code coding problems solutions Hackerrank Problems Solutions HackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes