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:
풀이코드
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를 얼마나 이해하고 활용하고있는지 확인하는 문제이다.
[Hackerrank] Abstract Classes (0) | 2021.10.11 |
---|---|
[Hackerrank] Ingeritance (0) | 2021.10.10 |
[Hackerrank] 2D Arrays (0) | 2021.10.10 |
[Hackerrank] Binary Numbers (0) | 2021.10.10 |
[Hackerrank] Recursion 3 (0) | 2021.10.10 |