Day 14: Scope HackerRank Solution


Day 14: Scope HackerRank Solution
Source : https://www.hackerrank.com/challenges/30-scope



Source : https://www.hackerrank.com/challenges/30-scope


Solution


// Karthikalapati.blogspot.com
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Difference {
private int[] elements;
public int maximumDifference;
Difference (int [] elements) {
this.elements = elements;
}
// Runtime: O(n)
// Space Complexity: O(1)
void computeDifference() {
int max = Arrays.stream(elements).max().getAsInt();
int min = Arrays.stream(elements).min().getAsInt();
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);
}
}

No comments:

Post a Comment