Count of indices in an array that satisfy the given condition

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.