From 126b135d6945db5b89220e6ff1dfe36513994591 Mon Sep 17 00:00:00 2001 From: Prasad Honrao Date: Thu, 6 Jun 2019 04:19:13 +0000 Subject: [PATCH] Add breaking the records java solution --- .../breaking-the-records.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 problem-solving/algorithms/implementation/breaking-the-records/breaking-the-records.java diff --git a/problem-solving/algorithms/implementation/breaking-the-records/breaking-the-records.java b/problem-solving/algorithms/implementation/breaking-the-records/breaking-the-records.java new file mode 100644 index 0000000..5b9f766 --- /dev/null +++ b/problem-solving/algorithms/implementation/breaking-the-records/breaking-the-records.java @@ -0,0 +1,65 @@ +import java.io.*; +import java.math.*; +import java.security.*; +import java.text.*; +import java.util.*; +import java.util.concurrent.*; +import java.util.regex.*; + +public class Solution { + + // Complete the breakingRecords function below. + static int[] breakingRecords(int[] scores) { + int min = scores[0]; + int max = scores[0]; + int c1 = 0; + int c2 = 0; + for (int i = 0; i < n; i++) + { + if (scores[i] > max) { + c1+=1; + max=scores[i]; + } + if (scores[i] < min) { + c2+=1; + min=scores[i]; + } + } + return new int[] {c1, c2}; + } + + private static final Scanner scanner = new Scanner(System.in); + + public static void main(String[] args) throws IOException { + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); + + int n = scanner.nextInt(); + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); + + int[] scores = new int[n]; + + String[] scoresItems = scanner.nextLine().split(" "); + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); + + for (int i = 0; i < n; i++) { + int scoresItem = Integer.parseInt(scoresItems[i]); + scores[i] = scoresItem; + } + + int[] result = breakingRecords(scores); + + for (int i = 0; i < result.length; i++) { + bufferedWriter.write(String.valueOf(result[i])); + + if (i != result.length - 1) { + bufferedWriter.write(" "); + } + } + + bufferedWriter.newLine(); + + bufferedWriter.close(); + + scanner.close(); + } +}