Elements appear more than n/k times in array

Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

In the given array of size n, find the elements which appear more than n/k times. Where k is the input value


a) Input array: [1, 3, 4, 7, 4, 7, 5], k = 6
    Output: [4, 7]
    Size of array (n) = 7, n/k = 1
    Therefore, we need to find elements which are coming more than 1 time.
    Therefore, output: [4, 7].

b) Input array: [1, 1, 1, 2, 2, 3, 3, 4, 4, 5], k = 5
    Output: [1]
    Size of array (n) =10, n/k = 2
    Therefore, we need to find elements which are coming more than 2 times.
    Therefore, output: [1].


Time complexity: O(nk)

a. Create a structure (struct) which stores the value of the element and its count in the given array

b. Create a temporary array of size k – 1 to store these structs.

c. Traverse through the input array and update temp[] for every element.

    1) If it is new element add this element and initialize count equal to 1.
    2) If it is existing an element then update count by incrementing by 1.

d. If temp[] array is full and we see a new element then,

   1)    We decrease count of every element by 1 in temp[].
   2)    Ignore the current element.

e. Iterate through the final temp[] and check for every element if count more than n/k.

Algorithm working

C++ Program

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
// A structure to store an element and its current count
struct Element
    int value;  // Element
    int count;  // Count
//Function to print elements whose occurences is:
//Greater than n/k
void CountGreaterThan(int array[], int n, int k)
    //k must be greater than 1
    if (k <= 1)
    //Temporary array is temp and size is k-1
    //Initialize all elemets count equal to 0
    struct Element temp[k-1];
    for (int i=0; i<k-1; i++)
        temp[i].count = 0;
    //If element already present then increment count
    //If not present and position available then:
    //add element with count equal to 1
    //If there is new element and no available space then:
    //decrese count of all elements by 1 
    for (int i = 0; i < n; i++)
        int j;
        for (j=0; j<k-1; j++)
            if (temp[j].value == array[i])
                 temp[j].count = temp[j].count + 1;
        //new element comes
        //position available
        if (j == k-1)
            int p;
            for (p=0; p<k-1; p++)
                if (temp[p].count == 0)
                    temp[p].value = array[i];
                    temp[p].count = 1;
            //new element comes
            //no available space to add element
            if (p == k-1)
                for (p=0; p<k; p++)
                    temp[p].count = temp[p].count + 1;
    //In final temp[]
    //check if elements actual count is greater than n/k
    //print if count grater than n/k
    cout<<"Final Output:"<<endl;
    for (int i=0; i<k-1; i++)
        int correct_count = 0;
        for (int j=0; j<n; j++)
            if (array[j] == temp[i].value)
        if (correct_count > n/k)
           cout << "Element: " << temp[i].value<< " with count: " << correct_count << endl;
/* Driver program to test above function */
int main()
    int input_array[] = {1, 1, 1, 2, 2, 2, 2, 4, 4, 5};
    int size = sizeof(input_array)/sizeof(int);
    int k = 5;
    return 0;
Try It

C++ Online Test - Check Your Rank across the World

Next > < Prev
Scroll to Top