Skip to content

Greater than All

Problem Description

Given an integer array A.

Find the count of elements whose value is greater than all of its previous elements.

Note: Since there are no elements before first element so it should be considered in our answer.

Problem Constraints

1 <= |A| <= 10^5
1 <= Ai <= 10^9
1 <= |A| <= 10^5
1 <= Ai <= 10^9

Input Format

Given an integer array A.
Given an integer array A.

Output Format

Return an integer.
Return an integer.

Example Input

Input 1:
A = [1, 2, 3, 4]

Input 2:
A = [1, 1, 2, 2]
Input 1:
A = [1, 2, 3, 4]

Input 2:
A = [1, 1, 2, 2]

Example Output

Output 1:
4
Output 2:
2
Output 1:
4
Output 2:
2

Example Explanation

Explanation 1:
All elements are greater than all of its prior elements.

Explanation 2:
Index 1 will be considerd in answer.
Also Elements at index 3 is greater than all of it's previous elements.
Explanation 1:
All elements are greater than all of its prior elements.

Explanation 2:
Index 1 will be considerd in answer.
Also Elements at index 3 is greater than all of it's previous elements.

Solution

swift
import Foundation

class Solution {
	func solve(_ A: inout [Int]) -> Int {
        var ret = 0
        var max = Int.min
        for v in A {
            if v > max {
                ret += 1
                max = v
            }
        }
        return ret
	}
}
import Foundation

class Solution {
	func solve(_ A: inout [Int]) -> Int {
        var ret = 0
        var max = Int.min
        for v in A {
            if v > max {
                ret += 1
                max = v
            }
        }
        return ret
	}
}

References