Type Here to Get Search Results !

Day 14: HackerRank 30 Days Of Code Solution By CodingHumans | Scope |

1


Day 14: Scope

Problem

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 Style

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 Style

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 .



Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.

Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.

HAPPY CODING 😁






Solution:
( 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 . 

Post a Comment

1 Comments
  1. please provide it's solution in javascript

    ReplyDelete
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad