- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this tutorial, we will be discussing a program to find the number of elements in the array which appears at least K times after their first occurrence.

For this we will be provided with an integer array and a value k. Our task is to count all the elements occurring k times among the elements after the element in consideration.

#include <iostream> #include <map> using namespace std; //returning the count of elements int calc_count(int n, int arr[], int k){ int cnt, ans = 0; //avoiding duplicates map<int, bool> hash; for (int i = 0; i < n; i++) { cnt = 0; if (hash[arr[i]] == true) continue; hash[arr[i]] = true; for (int j = i + 1; j < n; j++) { if (arr[j] == arr[i]) cnt++; //if k elements are present if (cnt >= k) break; } if (cnt >= k) ans++; } return ans; } int main(){ int arr[] = { 1, 2, 1, 3 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 1; cout << calc_count(n, arr, k); return 0; }

1

- Related Questions & Answers
- Find all elements in array which have at-least two greater elements in C++
- Program to find elements from list which have occurred at least k times in Python
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Maximum sum subsequence with at-least k distant elements in C++
- Maximum sum subsequence with at-least k distant elements in C++ program
- Count elements that are divisible by at-least one element in another array in C++
- Program to find k where k elements have value at least k in Python
- Count all pairs of an array which differ in K bits in C++
- Count the number of elements in an array which are divisible by k in C++
- Count substrings with each character occurring at most k times in C++
- Unique element in an array where all elements occur k times except one in C++
- Shortest Subarray with Sum at Least K in C++
- Count subarrays with all elements greater than K in C++
- Longest Substring with At Least K Repeating Characters in C++

Advertisements