Largest element in the array that is repeated exactly k times
Given an array of integers and an integer ‘k’, the task is to find the largest element from the array that is repeated exactly ‘k’ times.
Examples:
Input: arr = {1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6}, k = 2
Output: 5
The elements that exactly occur 2 times are 1, 3 and 5
And, the largest element among them is 5.
Input: arr = {1, 2, 3, 4, 5, 6}, k = 2
Output: No such element
There isn't any element in the array
that occurs exactly 2 times.
A simple approach:
- Sort the array.
- Start traversing the array from the end (as we’re interested in the largest element that satisfies the condition) and count the frequencies of each element by comparing the element with its neighbour elements.
- The first element from the right that occurs exactly ‘k’ times is the answer.
- If no element is found that occurs exactly ‘k’ times then print ‘No such element’.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int arr[], int n, int k)
{
sort(arr, arr + n);
if (k == 1 && arr[n - 2] != arr[n - 1]) {
cout << arr[n - 1] << endl;
return ;
}
int count = 1;
for ( int i = n - 2; i >= 0; i--) {
if (arr[i] == arr[i + 1])
count++;
else
count = 1;
if (count == k && (i == 0 || (arr[i - 1] != arr[i]))) {
cout << arr[i] << endl;
return ;
}
}
cout << "No such element" << endl;
}
int main()
{
int arr[] = { 1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6 };
int k = 2;
int n = sizeof (arr) / sizeof ( int );
solve(arr, n, k);
return 0;
}
|
Java
import java.util.Arrays ;
public class GFG {
static void solve( int arr[], int n, int k)
{
Arrays.sort(arr);
if (k == 1 && arr[n - 2 ] != arr[n - 1 ]) {
System.out.println(arr[n - 1 ]);
return ;
}
int count = 1 ;
for ( int i = n - 2 ; i >= 0 ; i--) {
if (arr[i] == arr[i + 1 ])
count++;
else
count = 1 ;
if (count == k && (i == 0 || (arr[i - 1 ] != arr[i]))) {
System.out.println(arr[i]);
return ;
}
}
System.out.println( "No such element" );
}
public static void main(String args[])
{
int arr[] = { 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 };
int k = 2 ;
int n = arr.length;
solve(arr, n, k);
}
}
|
Python 3
def solve(arr, n, k):
arr.sort()
if (k = = 1 and arr[n - 2 ] ! = arr[n - 1 ]):
print ( arr[n - 1 ] )
return
count = 1
for i in range (n - 2 , - 1 , - 1 ) :
if (arr[i] = = arr[i + 1 ]):
count + = 1
else :
count = 1
if (count = = k and (i = = 0 or
(arr[i - 1 ] ! = arr[i]))):
print (arr[i])
return
print ( "No such element" )
if __name__ = = "__main__" :
arr = [ 1 , 1 , 2 , 3 , 3 , 4 ,
5 , 5 , 6 , 6 , 6 ]
k = 2
n = len (arr)
solve(arr, n, k)
|
C#
using System;
class GFG
{
static void solve( int []arr, int n, int k)
{
Array.Sort(arr);
if (k == 1 && arr[n - 2] != arr[n - 1])
{
Console.WriteLine(arr[n - 1]);
return ;
}
int count = 1;
for ( int i = n - 2; i >= 0; i--)
{
if (arr[i] == arr[i + 1])
count++;
else
count = 1;
if (count == k && (i == 0 ||
(arr[i - 1] != arr[i])))
{
Console.WriteLine(arr[i]);
return ;
}
}
Console.WriteLine( "No such element" );
}
static public void Main ()
{
int []arr = { 1, 1, 2, 3, 3, 4,
5, 5, 6, 6, 6 };
int k = 2;
int n = arr.Length;
solve(arr, n, k);
}
}
|
PHP
<?php
function solve(& $arr , $n , $k )
{
sort( $arr );
if ( $k == 1 && $arr [ $n - 2] != $arr [ $n - 1])
{
echo $arr [ $n - 1] ;
echo ( "\n" );
return ;
}
$count = 1;
for ( $i = $n - 2; $i >= 0; $i --)
{
if ( $arr [ $i ] == $arr [ $i + 1])
$count ++;
else
$count = 1;
if ( $count == $k && ( $i == 0 ||
( $arr [ $i - 1] != $arr [ $i ])))
{
echo ( $arr [ $i ]);
echo ( "\n" );
return ;
}
}
echo ( "No such element" );
echo ( "\n" );
}
$arr = array (1, 1, 2, 3, 3, 4,
5, 5, 6, 6, 6 );
$k = 2;
$n = sizeof( $arr );
solve( $arr , $n , $k );
?>
|
Javascript
<script>
function solve(arr, n, k)
{
arr.sort((a,b)=> a-b)
if (k == 1 && arr[n - 2] != arr[n - 1]) {
cout << arr[n - 1] << endl;
return ;
}
var count = 1;
for ( var i = n - 2; i >= 0; i--) {
if (arr[i] == arr[i + 1])
count++;
else
count = 1;
if (count == k && (i == 0 || (arr[i - 1] != arr[i]))) {
document.write( arr[i] + "<br>" );
return ;
}
}
document.write( "No such element" );
}
var arr = [ 1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6 ];
var k = 2;
var n = arr.length;
solve(arr, n, k);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Efficient approach:
- Create a map and store the frequency of each element in the map.
- Then, traverse the array and find out the largest element that has frequency equal to ‘k’.
- If found, print the number
- Else, print ‘No such element’.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int arr[], int n, int k)
{
unordered_map< int , int > m;
for ( int i = 0; i < n; i++) {
m[arr[i]]++;
}
int max = INT_MIN;
for ( int i = 0; i < n; i++) {
if (m[arr[i]] == k && max == INT_MIN) {
max = arr[i];
}
else if (m[arr[i]] == k && max < arr[i]) {
max = arr[i];
}
}
if (max == INT_MIN)
cout << "No such element" << endl;
else
cout << max << endl;
}
int main()
{
int arr[] = { 1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6 };
int k = 4;
int n = sizeof (arr) / sizeof ( int );
solve(arr, n, k);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
class GfG
{
static void solve( int arr[], int n, int k)
{
HashMap<Integer, Integer> m = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (!m.containsKey(arr[i]))
m.put(arr[i], 0 );
m.put(arr[i], m.get(arr[i]) + 1 );
}
int max = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++)
{
if (m.get(arr[i]) == k && max == Integer.MIN_VALUE)
{
max = arr[i];
}
else if (m.get(arr[i]) == k && max < arr[i])
{
max = arr[i];
}
}
if (max == Integer.MIN_VALUE)
System.out.println( "No such element" );
else
System.out.println(max);
}
public static void main(String []args)
{
int arr[] = { 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 };
int k = 4 ;
int n = arr.length;
solve(arr, n, k);
}
}
|
Python3
import sys
def solve(arr, n, k):
m = {};
for i in range ( 0 , n - 1 ):
if (arr[i] in m.keys()):
m[arr[i]] + = 1 ;
else :
m[arr[i]] = 1 ;
i + = 1 ;
max = sys.maxsize;
for i in range ( 0 , n - 1 ):
if (m[arr[i]] = = k and
max = = sys.maxsize):
max = arr[i];
elif (m[arr[i]] = = k and max < arr[i]):
max = arr[i];
i + = 1
if ( max = = sys.maxsize):
print ( "No such element" );
else :
print ( max );
arr = [ 1 , 1 , 2 , 3 , 3 , 4 ,
5 , 5 , 6 , 6 , 6 ]
k = 4 ;
n = len (arr)
solve(arr, n, k)
|
C#
using System;
using System.Collections.Generic;
class GfG
{
static void solve( int []arr, int n, int k)
{
Dictionary< int , int > m = new Dictionary< int , int >();
for ( int i = 0 ; i < n; i++)
{
if (m.ContainsKey(arr[i]))
{
var val = m[arr[i]];
m.Remove(arr[i]);
m.Add(arr[i], val + 1);
}
else
{
m.Add(arr[i], 1);
}
}
int max = int .MinValue;
for ( int i = 0; i < n; i++)
{
if (m[arr[i]] == k && max == int .MinValue)
{
max = arr[i];
}
else if (m[arr[i]] == k && max < arr[i])
{
max = arr[i];
}
}
if (max == int .MinValue)
Console.WriteLine( "No such element" );
else
Console.WriteLine(max);
}
public static void Main(String []args)
{
int []arr = { 1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6 };
int k = 4;
int n = arr.Length;
solve(arr, n, k);
}
}
|
Javascript
<script>
function solve(arr, n, k)
{
var m = new Map();
for ( var i = 0; i < n; i++) {
m.set(arr[i], m.get(arr[i])+1);
}
var max = -1000000000;
for ( var i = 0; i < n; i++) {
if (m.get(arr[i]) == k && max == -1000000000) {
max = arr[i];
}
else if (m.get(arr[i]) == k && max < arr[i]) {
max = arr[i];
}
}
if (max == -1000000000)
document.write( "No such element" );
else
document.write( max);
}
var arr = [ 1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6 ];
var k = 4;
var n = arr.length;
solve(arr, n, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
01 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...