Count of indices in an array that satisfy the given condition Last Updated : 01 Mar, 2022 Summarize Comments Improve Suggest changes Share Like Article Like 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: 4 All indices satisfy 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 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) Comment More infoAdvertise with us Next Article Count of indices in an array that satisfy the given condition G gp6 Follow Improve Article Tags : Mathematical DSA Arrays Constructive Algorithms Practice Tags : ArraysMathematical Similar Reads Count of triplets in an array that satisfy the given conditions Given an array arr[] of N elements, the task is to find the count of triplets (arr[i], arr[j], arr[k]) such that (arr[i] + arr[j] + arr[k] = L) and (L % arr[i] = L % arr[j] = L % arr[k] = 0.Examples: Input: arr[] = {2, 4, 5, 6, 7} Output: 1 Only possible triplet is {2, 4, 6}Input: arr[] = {4, 4, 4, 13 min read Count number of coordinates from an array satisfying the given conditions Given an array arr[] consisting of N coordinates in the Cartesian Plane, the task is to find the number of coordinates, such as (X, Y), that satisfies all the following conditions: All possible arr[i][0] must be less than X and arr[i][1] must be equal to Y.All possible arr[i][0] must be greater than 10 min read Count of elements that are Kth powers of their indices in given Array Given an array arr[] with N non-negative integers, the task is to find the number of elements that are Kth powers of their indices, where K is a non-negative number. arr[i] = iK Example: Input: arr = [1, 1, 4, 3, 16, 125, 1], K = 0Output: 3Explanation: 3 elements are Kth powers of their indices:00 i 9 min read Count pairs in given Array having sum of index and value at that index equal Given an array arr[] containing positive integers, count the total number of pairs for which arr[i]+i = arr[j]+j such that 0â¤i<jâ¤n-1. Examples: Input: arr[] = { 6, 1, 4, 3 }Output: 3Explanation: The elements at index 0, 2, 3 has same value of a[i]+i as all sum to 6 {(6+0), (4+2), (3+3)}. Input: a 8 min read Count of elements A[i] such that A[i] + 1 is also present in the Array Given an integer array arr the task is to count the number of elements 'A[i]', such that A[i] + 1 is also present in the array.Note: If there are duplicates in the array, count them separately.Examples: Input: arr = [1, 2, 3] Output: 2 Explanation: 1 and 2 are counted cause 2 and 3 are in arr.Input: 11 min read Like