Peak Index in a Mountain Array || newton school Assignment solution || String 

Peak Index in a Mountain Array

Time Limit: 2 sec
Memory Limit: 128000 kB
Problem Statement
Let's call an array arr a mountain if the following properties hold:
a) arr. length >= 3
b) There exists some i with 0 < i < arr. length - 1 such that:
- arr[0] < arr[1] <. . arr[i-1] < arr[i]
- arr[i] > arr[i+1] >. . > arr[arr. length - 1]
Given an integer array arr that is guaranteed to be a mountain, return any i such that arr[0] < arr[1] <. . arr[i - 1] < arr[i] > arr[i + 1] >. . > arr[arr. length - 1].
Input
First line contains n, length of mountain array.
Next line contains the input of mountain array.

Constraints
3 <= n <= 10000
0 <= arr[i] <= 1000000
arr is guaranteed to be a mountain array.
Output
Print the Peak Index in the Mountain Array
Example
Input :
3
0 1 0

Output :
1
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) {
                      // Your code here
Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] arr = new int[n];
        int max = -1, res = -1;
        for(int i = 0; i< n; i++) {
            arr[i] = sc.nextInt();
            if(max < arr[i]) {
                max = arr[i];
                res = i;
            }
        }
        System.out.print(res);
    }
}

Post a Comment

Previous Post Next Post