Given an array arr[] of N positive integers, the task is to find the count of indices i such that all the elements from arr[0] to arr[i – 1] are smaller then arr[i].
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: 4
All indices satify the given condition.Input: arr[] = {4, 3, 2, 1}
Output: 1
Only i = 0 is the valid index.
Approach: The idea is to traverse the array from left to right and keep track of the current maximum, whenever this maximum changes then the current index is a valid index so increment the resulting counter.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count // of indices that satisfy // the given condition int countIndices(int arr[], int n) { // To store the result int cnt = 0; // To store the current maximum // Initialized to 0 since there are only // positive elements in the array int max = 0; for (int i = 0; i < n; i++) { // i is a valid index if (max < arr[i]) { // Update the maximum so far max = arr[i]; // Increment the counter cnt++; } } return cnt; } // Driver code int main() { int arr[] = { 1, 2, 3, 4 }; int n = sizeof(arr) / sizeof(int); cout << countIndices(arr, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the count // of indices that satisfy // the given condition static int countIndices(int arr[], int n) { // To store the result int cnt = 0; // To store the current maximum // Initialized to 0 since there are only // positive elements in the array int max = 0; for (int i = 0; i < n; i++) { // i is a valid index if (max < arr[i]) { // Update the maximum so far max = arr[i]; // Increment the counter cnt++; } } return cnt; } // Driver code public static void main(String[] args) { int arr[] = { 1, 2, 3, 4 }; int n = arr.length; System.out.println(countIndices(arr, n)); } } // This code is contributed by Rajput-Ji |
Python3
# Python implementation of the approach # Function to return the count # of indices that satisfy # the given condition def countIndices(arr, n): # To store the result cnt = 0; # To store the current maximum # Initialized to 0 since there are only # positive elements in the array max = 0; for i in range(n): # i is a valid index if (max < arr[i]): # Update the maximum so far max = arr[i]; # Increment the counter cnt += 1; return cnt; # Driver code if __name__ == '__main__': arr = [ 1, 2, 3, 4 ]; n = len(arr); print(countIndices(arr, n)); # This code is contributed by 29AjayKumar |
C#
// C# implementation of the approach using System; class GFG { // Function to return the count // of indices that satisfy // the given condition static int countIndices(int []arr, int n) { // To store the result int cnt = 0; // To store the current maximum // Initialized to 0 since there are only // positive elements in the array int max = 0; for (int i = 0; i < n; i++) { // i is a valid index if (max < arr[i]) { // Update the maximum so far max = arr[i]; // Increment the counter cnt++; } } return cnt; } // Driver code public static void Main(String[] args) { int []arr = { 1, 2, 3, 4 }; int n = arr.Length; Console.WriteLine(countIndices(arr, n)); } } // This code is contributed by PrinciRaj1992 |
4
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count sub-sets that satisfy the given condition
- Count of sub-sequences which satisfy the given condition
- Count all possible N digit numbers that satisfy the given condition
- Count index pairs which satisfy the given condition
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Pairs from an array that satisfy the given condition
- Append two elements to make the array satisfy the given condition
- Find the Number of Permutations that satisfy the given condition in an array
- Sum of absolute differences of pairs from the given array that satisfy the given condition
- Count of triplets in an array that satisfy the given conditions
- Count of unordered pairs (x, y) of Array which satisfy given equation
- Minimum operations required to make the string satisfy the given condition
- Count the pairs in an array such that the difference between them and their indices is equal
- Find the count of unvisited indices in an infinite array
- Choose X elements from A[] and Y elements from B[] which satisfy the given condition
- Count of triplets that satisfy the given equation
- Count of Fibonacci pairs which satisfy the given equation
- Count of N digit numbers possible which satisfy the given conditions
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n
- Count triplet of indices (i, j, k) such that XOR of elements between [i, j) equals [j, k]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

