알고리즘/Hackerrank

[Hackerrank] Scope

비독개 2021. 11. 7. 00:04

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.
  • A computeDifference method that finds the maximum absolute difference between any  numbers in  and stores it in the  instance variable.Input FormatSample Input
  • 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).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 .
  • To find the maximum difference, computeDifference checks each element in the array and finds the maximum difference between any  elements: 

  • STDIN Function ----- -------- 3 __elements[] size N = 3 1 2 5 __elements = [1, 2, 5]
  • Sample Output
  • 4
  • Output Format
  • You are not responsible for printing any output; the Solution class will print the value of the  instance variable.
  • 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
     

풀이코드

더보기
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;


class Difference {
  	private int[] elements;
  	public int maximumDifference;

	// Add your code here
    public Difference(int[] a) {
        this.elements = a;
    }
    
    public void computeDifference() {
        int min = 101;
        int max = 0;
        
        for(int num : elements) {
            min = Math.min(min, num);
            max = Math.max(max, num);
        }
        
        maximumDifference = max - min;
    }
    
} // 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);
    }
}

풀이방식

scope를 얼마나 이해하고 활용하고있는지 확인하는 문제이다.

  1. Solution Class 에서 입력받은 a라는 배열을가지고 Difference 클래스를 생성자를 통하여 초기화 해주고있다.
  2. Solution Class 에서 실행되는 computeDifference라는 메소드를 구현해준다.
  3. 해당 메소드는 a라는 배열값들을 비교하여 최소값과 최대값을 찾고 그 둘의 차이를 구해줘야한다.
  4. for 문을 통해 배열을 탐색하여 최소,최대값을 찾고 두 차이를 Solution Class 에서 출력해주는 maximumDifference 라는 변수에 값을 담아준다.