Open In App

Count of indices in an array that satisfy the given condition

Last Updated : 01 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 than arr[i].

Examples: 

Input: arr[] = {1, 2, 3, 4} 
Output:
All indices satisfy the given condition.
Input: arr[] = {4, 3, 2, 1} 
Output:
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


Javascript




<script>
// javascript implementation of the approach   
// Function to return the count
    // of indices that satisfy
    // the given condition
    function countIndices(arr , n) {
 
        // To store the result
        var cnt = 0;
 
        // To store the current maximum
        // Initialized to 0 since there are only
        // positive elements in the array
        var max = 0;
        for (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
     
        var arr = [ 1, 2, 3, 4 ];
        var n = arr.length;
 
        document.write(countIndices(arr, n));
 
// This code contributed by aashish1995
</script>


Output: 

4

 

Time Complexity: O(n)

Auxiliary Space: O(1)



Similar Reads

Sum of absolute differences of pairs from the given array that satisfy the given condition
Given an array arr[] of N elements, the task is to find the sum of absolute differences between all pairs (arr[i], arr[j]) such that i &lt; j and (j - i) is prime.Example: Input: arr[] = {1, 2, 3, 5, 7, 12} Output: 45 All valid index pairs are: (5, 0) -&gt; abs(12 - 1) = 11 (3, 0) -&gt; abs(5 - 1) = 4 (2, 0) -&gt; abs(3 - 1) = 2 (4, 1) -&gt; abs(7
12 min read
Count of sub-sequences which satisfy the given condition
Given a string str consisting of digits, the task is to find the number of possible 4 digit sub-sequences which are of the form (x, x, x + 1, x + 1) where x can be from the range [0, 8]. Examples: Input: str = "1122" Output: 1 Only one sub-sequence is valid, i.e the entire string itself. Input: str = "13134422" Output: 2 Two Valid sub-sequences are
10 min read
Count sub-sets that satisfy the given condition
Given an array arr[] and an integer x, the task is to count the number of sub-sets of arr[] sum of all of whose sub-sets (individually) is divisible by x. Examples: Input: arr[] = {2, 4, 3, 7}, x = 2 Output: 3 All valid sub-sets are {2}, {4} and {2, 4} {2} =&gt; 2 is divisible by 2 {4} =&gt; 4 is divisible by 2 {2, 4} =&gt; 2, 4 and 6 are all divis
5 min read
Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
Given N points in 2 dimensional space. The task is to count the number of triplets pairs (A, B, C) such that point B is the midpoint of line segment formed by joining points A and C.Examples: Input: points = {{1, 1}, {2, 2}, {3, 3}} Output: 1 The point (2, 2) is the midpoint of the line segment joining points (1, 1) and (3, 3).Input: points = {{1,
6 min read
Count all possible N digit numbers that satisfy the given condition
Given an integer N, the task is to count all possible N digit numbers such that A + reverse(A) = 10N - 1 where A is an N digit number and reverse(A) is reverse of A. A shouldn't have any leading 0s.Examples: Input: N = 2 Output: 9 All possible 2 digit numbers are 90, 81, 72, 63, 54, 45, 36, 27 and 18.Input: N = 4 Output: 90 Approach: First we have
4 min read
Count index pairs which satisfy the given condition
Given a permutation P of first N natural numbers, the task is to count the index pairs (i, j) such that P[i] + P[j] = max(P[x]) where i ? x ? j.Examples: Input: P[] = {3, 4, 1, 5, 2} Output: 2 Only valid index pairs are (0, 4) and (0, 2)Input: P[] = {1, 3, 2} Output: 1 Naive approach: We can solve this problem by iterating for all possible pairs (i
8 min read
Count pairs of indices having sum of indices same as the sum of elements at those indices
Given an array arr[] consisting of N integers, the task is to find the number of pairs (i, j) whose sum of indices is the same as the sum elements at the indices. Examples: Input: arr[] = {0, 1, 7, 4, 3, 2}Output: 1Explanation: There exists only pair that satisfies the condition is {(0, 1)}. Input: arr[] = {1, 6, 2, 4, 5, 6}Output: 0 Naive Approach
9 min read
Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices
Given an array arr[] consisting of N positive integers, the task is to find the number of pairs (i, j) such that i &lt; j and the product of elements at these indices is equal to the absolute difference of their indices. Examples: Input: arr[] = {1, 1, 2, 4}Output: 2Explanation:Following are the possible pairs: (0, 1): The sum of these indices is 0
10 min read
Count of unordered pair of indices such that ratio of elements at these indices is same as ratio of indices
Given an array arr[] of N integers, the task is to find the number of unordered pairs (i, j) in the array such that the ratio of elements at these indices is the same as the ratio of indices (arr[j]/arr[i] = j/i). Examples: Input: arr[] = {4, 5, 12, 10, 6}Output: 2Explanation: The pairs that follow the given condition are: (1, 3) as arr[3] / arr[1]
10 min read
Pairs from an array that satisfy the given condition
Given an array arr[], the task is to count all the valid pairs from the array. A pair (arr[i], arr[j]) is said to be valid if func( arr[i] ) + func( arr[j] ) = func( XOR(arr[i], arr[j]) ) where func(x) returns the number of set bits in x. Examples: Input: arr[] = {2, 3, 4, 5, 6} Output: 3 (2, 4), (2, 5) and (3, 4) are the only valid pairs. Input: a
9 min read