# C++ Program to count of array elements greater than all elements on its left and at least K elements on its right

A string is an object, which represents a sequence of data characters. The strings are the data container which always represented as a text format. It also used to concept, compare, split, join, replace, trim, length, intern, equals, comparison, substring operation. K largest(or smallest) elements in an array using Quick Sort partitioning algorithm.

Here is an array R[] with N number of distinct integers. The task is to find that particular element which are strictly greater than all the elements preceding it and strictly greater than at least K elements on its right. The problem states that an array arr[ ] (array R) of N distinct elements and integer K; we have to find out a number of elements greater than all elements on its left and at least K elements on its right.

Input: R[] = {16,07,10,22,2001,1997}, K = 3
Output: 16
Therefore, the count is 2.


There are two types of approach to find out the array elements greater than all elements on its left and at least K elements on its right.

• Naive Approach − It is the simplest approach to traverse a particular array. Here in this method we have to traverse all the elements to the left side to check it is smaller or not. Else, traverse them to right to check the least K element is smaller or not. For this approach, the time complexity is O(N2) and auxiliary space is O(1).

• Efficient Approach − This is an optimization process can be done by Self-Balancing BST. Traverse the array one by one from right to left by AVL Tree. AVL Tree generates an array countSmaller[]. Here the time complexity is O(NlogN) and auxiliary space is O(N). For every element performing the satisfying conditions, increase count.

Let’s find out the number of array elements greater than all on its left and at least K elements on its right.

### Algorithm to count the array elements greater than all elements:-

Here in this algorithm we will follow the step by step process to count an array elements. By this we will build some C++ code to find the greatest element of all.

• Step 1 − Start.

• Step 2 − Traverse the array from right to left.

• Step 3 − Insert all elements in an AVL Tree.

• Step 4 − By using the AVL Tree generate an array countSmaller[].

• Step 5 − It contains the count of smaller elements on the right of every array element.

• Step 6 − Traverse the array and for every element.

• Step 7 − Check if it is the maximum obtained so far and countSmaller[i] is greater than or equal to K.

• Step 8 − If the condition satisfied, increase the count.

• Step 9 − Print the final value of count as the answer.

• Step 10 − Terminate.

### Syntax

for (i = k; i < array.length; i++){
minIndex = 0;
for (int j = 0; j < k; j++){
if(array[j] < array[minIndex]){
minIndex = j;
array[minIndex] = array[j];
}
}
if (array[minIndex] < array[i]){
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}


Here is an integer array num and integer is K. It will return Kth element in the array. We have to solve it in O(n) time complexity.

### Approach

• Approach 1 − Find K largest(or smallest) elements using sorting.

• Approach 2 − Efficient approach to find K largest(or smallest) element in an array.

## Find K largest (or smallest) element using sorting

By using the sorting method we can get a result of this problem. Here are the steps −

• Descending order element sorting.

• Print the first K numbers from that sorted array.

### Example 1

#include <bits/stdc++.h>
using namespace std;
void kLargest(int arr[], int a, int b){
sort(arr, arr + a, greater());
for (int i = 0; i < b; i++)
cout << arr[i] << " ";
}
int main(){
int arr[] = { 10, 16, 07, 2001, 1997, 2022, 50 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 3;
kLargest(arr, n, k);
}


### Output

2022 2001 1997


## Efficient approach to find K largest (or smallest) element in an array

Here in this approach we will follow these steps to find out the result −

• Start.

• Traverse the array from right to left.

• Insert all elements in the AVL tree.

• Generate array countSmaller[].

• Count of smaller elements on the right of every array element.

• If it is the maximum, countSmaller[i] is greater than or equal to K.

• Then increment the count.

• Print the value.

• End.

### Example 2

#include <bits/stdc++.h>
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[] = { 16, 10, 2022, 1997, 7, 2001, 0 };
int n = sizeof(A) / sizeof(int);
int K = 3;
cout << countElements(A, n, K);
return 0;
}


### Output

2


## Conclusion

Thus we come to know how to write a C++ code to count of array elements greater than all elements on its left and at least K elements on its right.

Updated on: 06-Apr-2023

335 Views