Score bar || newton school assignment  solution || DSA contest 

Score bar

Time Limit: 2 sec
Memory Limit: 128000 kB
Problem Statement
You take part in N quizzes and in the ith quiz you get a score of Qi. Your friends are very competitive with you and they ask you the strength of your quiz scores. Strength of an array is defined as the following:

The maximum growth Qj - Qi (Qj > Qi) between two quizzes i and j such that i < j and there is no quiz k such that i < k < j and Qk > Qi.
If there is no such pair of indexes, print -1.

Print the strength of your quiz marks in order to impress your friends.
Input
First line contains a single integer N, the number of quizzes.
The second line contains N space seperated integers Q1, Q2,. , QN the score in each quiz.

Constraints:
1 <= N <= 105
1 <= Qi <= 109
Output
Print the strength of your quiz marks.
Example
Sample Input:
6
7 10 7 2 1 8

Sample Output:
7

Explaination:
There is a growth of 7 from Q5 (= 1) to Q6(= 8). There is no growth in the array greater than this.

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int res = 0, diff;
        int n = sc.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i ++) {
            arr[i] = sc.nextInt();
        }
        for(int j = 1; j <n; j++) {
            diff = arr[j] - arr[- 1];
            if(res < diff) {
                res = diff;
            }
        }

        
        System.out.println(res);
        sc.close();
    }
}

Post a Comment

Previous Post Next Post