Day 14: Scope In Java | 30 Days Of Code | Hackerrank Programming Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of 30 Days Of Code, HackerRank. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Day 14: Scope in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Link for the ProblemDay 14: Scope – Hacker Rank Solution

Day 14: Scope – Hacker Rank Solution

Problem:

Objective
Today we’re discussing scope. Check out the Tutorial tab for learning materials and an instructional video!


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

The Difference class is started for you in the editor. It has a private integer array () for storing  non-negative integers, and a public integer () 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  instance variable.
  • computeDifference method that finds the maximum absolute difference between any  numbers in  and stores it in the  instance variable.

Input Format

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

Constraints

  • , where 

Output Format

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

Sample Input

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

Sample Output

4

Explanation

The scope of the  array and  integer is the entire class instance. The class constructor saves the argument passed to the constructor as the  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  elements: 

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

Day 14: Scope – Hacker Rank Solution
public class Day14Scope {

	/**
	 * 
	 * @param elements
	 
Difference(int [] elements){
    this.elements=elements;
}
    
 void computeDifference() {
		int maxNum = elements[0];
		int minNum = maxNum;
		for (int i = 1; i < elements.length; i++) {
			
			maxNum=elements[i]>maxNum?elements[i]:maxNum;
			minNum=elements[i]>minNum?minNum:elements[i];
			
		}
		maximumDifference=Math.abs(maxNum-minNum);
	}
	*/
}

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.