Count of Array elements greater than all elements on its left and at least K elements on its right | C ++ Program to implement the above appraoch ; Structure of an AVL Tree Node ; Size of the tree rooted with this node ; Utility function to get maximum of two integers ; Utility function to get height of the tree rooted with N ; Utility function to find size of the tree rooted with N ; Utility function to get maximum of two integers ; Helper function to allocates a new node with the given key ; Utility function to right rotate subtree rooted with y ; Perform rotation ; Update heights ; Update sizes ; Return new root ; Utility function to left rotate subtree rooted with x ; Perform rotation ; Update heights ; Update sizes ; Return new root ; Function to obtain Balance factor of node N ; Function to insert a new key to the tree rooted with node ; Perform the normal BST rotation ; Update count of smaller elements ; Update height and size of the ancestor ; Get the balance factor of the ancestor ; Left Left Case ; Right Right Case ; Left Right Case ; Right Left Case ; Function to generate an array which contains count of smaller elements on the right ; Insert all elements in the AVL Tree and get the count of smaller elements ; Function to find the number of elements which are greater than all elements on its left and K elements on its right ; Stores the count of smaller elements on its right ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int key ; struct node * left ; struct node * right ; int height ; int size ; } ; int max ( int a , int b ) ; int height ( struct node * N ) { if ( N == NULL ) return 0 ; return N -> height ; } int size ( struct node * N ) { if ( N == NULL ) return 0 ; return N -> size ; } int max ( int a , int b ) { return ( a > b ) ? a : b ; } struct node * newNode ( int key ) { struct node * node = ( struct node * ) malloc ( sizeof ( struct node ) ) ; node -> key = key ; node -> left = NULL ; node -> right = NULL ; node -> height = 1 ; node -> size = 1 ; return ( node ) ; } struct node * rightRotate ( struct node * y ) { struct node * x = y -> left ; struct node * T2 = x -> right ; x -> right = y ; y -> left = T2 ; y -> height = max ( height ( y -> left ) , height ( y -> right ) ) + 1 ; x -> height = max ( height ( x -> left ) , height ( x -> right ) ) + 1 ; y -> size = size ( y -> left ) + size ( y -> right ) + 1 ; x -> size = size ( x -> left ) + size ( x -> right ) + 1 ; return x ; } struct node * leftRotate ( struct node * x ) { struct node * y = x -> right ; struct node * T2 = y -> left ; y -> left = x ; x -> right = T2 ; x -> height = max ( height ( x -> left ) , height ( x -> right ) ) + 1 ; y -> height = max ( height ( y -> left ) , height ( y -> right ) ) + 1 ; x -> size = size ( x -> left ) + size ( x -> right ) + 1 ; y -> size = size ( y -> left ) + size ( y -> right ) + 1 ; return y ; } int getBalance ( struct node * N ) { if ( N == NULL ) return 0 ; return height ( N -> left ) - height ( N -> right ) ; } struct node * insert ( struct node * node , int key , int * count ) { if ( node == NULL ) return ( newNode ( key ) ) ; if ( key < node -> key ) node -> left = insert ( node -> left , key , count ) ; else { node -> right = insert ( node -> right , key , count ) ; * count = * count + size ( node -> left ) + 1 ; } node -> height = max ( height ( node -> left ) , height ( node -> right ) ) + 1 ; node -> size = size ( node -> left ) + size ( node -> right ) + 1 ; int balance = getBalance ( node ) ; if ( balance > 1 && key < node -> left -> key ) return rightRotate ( node ) ; if ( balance < -1 && key > node -> right -> key ) return leftRotate ( node ) ; if ( balance > 1 && key > node -> left -> key ) { node -> left = leftRotate ( node -> left ) ; return rightRotate ( node ) ; } if ( balance < -1 && key < node -> right -> key ) { node -> right = rightRotate ( node -> right ) ; return leftRotate ( node ) ; } return node ; } void constructLowerArray ( int arr [ ] , int countSmaller [ ] , int n ) { int i , j ; struct node * root = NULL ; for ( i = 0 ; i < n ; i ++ ) countSmaller [ i ] = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { root = insert ( root , arr [ i ] , & countSmaller [ i ] ) ; } } int countElements ( int A [ ] , int n , int K ) { int count = 0 ; int * countSmaller = ( int * ) malloc ( sizeof ( int ) * n ) ; constructLowerArray ( A , countSmaller , n ) ; int maxi = INT_MIN ; for ( int i = 0 ; i <= ( n - K - 1 ) ; i ++ ) { if ( A [ i ] > maxi && countSmaller [ i ] >= K ) { count ++ ; maxi = A [ i ] ; } } return count ; } int main ( ) { int A [ ] = { 2 , 5 , 1 , 7 , 3 , 4 , 0 } ; int n = sizeof ( A ) / sizeof ( int ) ; int K = 3 ; cout << countElements ( A , n , K ) ; return 0 ; }