Home>Article>Backend Development> C++ program to count the number of array elements that are greater than all elements to its left and have at least K elements to its right
A string is an object that represents a sequence of data characters. Strings are data containers that are always represented in text format. It is also used for concept, comparison, split, join, replace, trim, length, interning, equals, comparison, substring operations. The K largest (or smallest) elements in an array using the quicksort partitioning algorithm.
This is an array R[] containing N different integers. The task is to find that specific element that is strictly greater than all elements before it and strictly greater than at least K elements to its right. The question states that an array arr[ ] (array R) consists of N distinct elements and an integer K; we must find the number of elements that are greater than all elements on its left side and have at least K elements on its right side .
Input: R[] = {16,07,10,22,2001,1997}, K = 3 Output: 16 Therefore, the count is 2.
There are two ways to find array elements that are greater than all elements on the left and at least K elements on the right.
Naive method- This is the simplest way to iterate over a specific array. In this method we have to traverse all elements to the left to check if it is smaller. Otherwise, traverse to the right and check if the smallest K elements are smaller. For this approach, the time complexity is O(N2) and the auxiliary space is O(1).
Efficient Method- This is an optimization process that can be done with a self-balancing BST. Traverse the array one by one from right to left through the AVL Tree. AVL Tree generates an array countSmaller[]. Here the time complexity is O(NlogN) and the auxiliary space is O(N). For each element that satisfies the condition, increment the count.
Let us find the number of array elements greater than all elements to its left and at least K elements to its right.
In this algorithm, we will follow the step-by-step process to count the number of array elements. With this, we will build some C code to find the largest element among all elements.
Step 1 - Get Started.
Step 2 - Traverse the array from right to left.
Step 3 - Insert all elements into the AVL tree.
Step 4 - Generate the array countSmaller[] by using an AVL tree.
Step 5 - It contains the count of the smaller elements to the right of each array element.
Step 6 - Loop through the array and find each element.
Step 7 - Check if this is the maximum value obtained so far and if countSmaller[i] is greater than or equal to K.
Step 8 - If the condition is met, increment the count.
Step 9 - Print the final value of count as the answer.
Step 10 - Termination.
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, the integer is K. It will return the Kth element in the array. We have to solve it in O(n) time complexity.
Method 1 - Find the K largest (or smallest) elements using sorting.
Method 2 - Efficient way to find the K largest (or smallest) elements in an array.
We can get the result of this problem by sorting. Here are the steps -
Sort elements in descending order.
Print the first K numbers in the sorted array.
#includeusing 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); }
2022 2001 1997
In this method we will follow the following steps to find out the result -
start.
Traverse the array from right to left.
Insert all elements in the AVL tree.
Generate array countSmaller[].
The count of the smaller element to the right of each array element.
If it is the maximum value, countSmaller[i] is greater than or equal to K.
Then increment the count.
Print the value.
Finish.
#includeusing 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; }
2
So we know how to write C code to count the number of array elements greater than all elements to its left and at least K elements to its right.
The above is the detailed content of C++ program to count the number of array elements that are greater than all elements to its left and have at least K elements to its right. For more information, please follow other related articles on the PHP Chinese website!