This post explains the HackerRank 30 Days of Code problem Scope step by step: the problem statement, input and output format, constraints, a sample test case and a working solution in Java that you can study and run.

Day 14: Scope
Problem Statement
Scope
Coders today we're discussing about scope.
This term refers to the region of the program to which an identifier applies. While it is not good practice, you can declare multiple variables within a program that use the same identifier as long as the identifiers have differing scopes; some exceptions to this are:
A constructor or method parameter will often have the same name as a class field it's intended to initialize or modify.
It is customary to use i as the condition variable in a for-loop (and, in cases of nested for-loops, to use j as the condition variable for the inner loop).
Aim
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 non-negative N integers, and a public integer (maximumDifference) for storing the maximum absolute difference.
Task assigned
You need to 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 element instance variable.
A 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 your editor reads in lines of input; the first line contains , and the second line describes the array.
Constraints
1<= N <=10
1<= elements[i] <=100, where
0<=i<=N-1
Output Format
Here you are not responsible for printing any output; the Solution class will print the value of the maximumDifference instance variable.
Sample Input
3
1 2 5
Sample Output
4
Explanation
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 code wrriten by you then prints the value stored as ,maximumDifference which is 4 .
Scope Solution in Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Difference {
private int[] elements;
public int maximumDifference;
public Difference(int []elements)
{
this.elements=elements;
}
void computeDifference(){
int n=elements.length;
Arrays.sort(elements);
maximumDifference=Math.abs(elements[n-1]-elements[0]);
}
} // End of Difference class
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
sc.close();
Difference difference = new Difference(a);
difference.computeDifference();
System.out.print(difference.maximumDifference);
}
}
If you have any doubts regarding this problem or need the solution in other programming languages then leave a comment down below .
Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.
More HackerRank solutions All coding problems MCQ practice
See everything in one place: Coding Problems and Solutions
please provide it's solution in javascript
ReplyDelete