Find the maximum repeating number in O(n) time and O(1) extra space
Last Updated :
20 Jun, 2022
Given an array of size n, the array contains numbers in the range from 0 to k-1 where k is a positive integer and k <= n. Find the maximum repeating number in this array. For example, let k be 10 the given array be arr[] = {1, 2, 2, 2, 0, 2, 0, 2, 3, 8, 0, 9, 2, 3}, the maximum repeating number would be 2. The expected time complexity is O(n) and extra space allowed is O(1). Modifications to array are allowed.
The naive approach is to run two loops, the outer loop picks an element one by one, and the inner loop counts a number of occurrences of the picked element. Finally, return the element with a maximum count. The time complexity of this approach is O(n^2).
A better approach is to create a count array of size k and initialize all elements of count[] as 0. Iterate through all elements of input array, and for every element arr[i], increment count[arr[i]]. Finally, iterate through count[] and return the index with maximum value. This approach takes O(n) time, but requires O(k) space.
Following is the O(n) time and O(1) extra space approach.
Let us understand the approach with a simple example where arr[] = {2, 3, 3, 5, 3, 4, 1, 7}, k = 8, n = 8 (number of elements in arr[]).
- Iterate though input array arr[], for every element arr[i], increment arr[arr[i]%k] by k (arr[] becomes {2, 11, 11, 29, 11, 12, 1, 15 })
- Find the maximum value in the modified array (maximum value is 29). Index of the maximum value is the maximum repeating element (index of 29 is 3).
- If we want to get the original array back, we can iterate through the array one more time and do arr[i] = arr[i] % k where i varies from 0 to n-1.
How does the above algorithm work? Since we use arr[i]%k as index and add value k at the index arr[i]%k, the index which is equal to maximum repeating element will have the maximum value in the end. Note that k is added maximum number of times at the index equal to maximum repeating element and all array elements are smaller than k.
Following is C++ implementation of the above algorithm.
C++
#include<iostream>
using namespace std;
int maxRepeating( int * arr, int n, int k)
{
for ( int i = 0; i< n; i++)
arr[arr[i]%k] += k;
int max = arr[0], result = 0;
for ( int i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
result = i;
}
}
return result;
}
int main()
{
int arr[] = {2, 3, 3, 5, 3, 4, 1, 7};
int n = sizeof (arr)/ sizeof (arr[0]);
int k = 8;
cout << "The maximum repeating number is " <<
maxRepeating(arr, n, k) << endl;
return 0;
}
|
Java
import java.io.*;
class MaxRepeating {
static int maxRepeating( int arr[], int n, int k)
{
for ( int i = 0 ; i< n; i++)
arr[(arr[i]%k)] += k;
int max = arr[ 0 ], result = 0 ;
for ( int i = 1 ; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
result = i;
}
}
return result;
}
public static void main (String[] args)
{
int arr[] = { 2 , 3 , 3 , 5 , 3 , 4 , 1 , 7 };
int n = arr.length;
int k= 8 ;
System.out.println( "Maximum repeating element is: " +
maxRepeating(arr,n,k));
}
}
|
Python3
def maxRepeating(arr, n, k):
for i in range ( 0 , n):
arr[arr[i] % k] + = k
max = arr[ 0 ]
result = 0
for i in range ( 1 , n):
if arr[i] > max :
max = arr[i]
result = i
return result
arr = [ 2 , 3 , 3 , 5 , 3 , 4 , 1 , 7 ]
n = len (arr)
k = 8
print ( "The maximum repeating number is" ,maxRepeating(arr, n, k))
|
C#
using System;
class GFG {
static int maxRepeating( int []arr,
int n, int k)
{
for ( int i = 0; i< n; i++)
arr[(arr[i]%k)] += k;
int max = arr[0], result = 0;
for ( int i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
result = i;
}
}
return result;
}
public static void Main ()
{
int []arr = {2, 3, 3, 5, 3, 4, 1, 7};
int n = arr.Length;
int k=8;
Console.Write( "Maximum repeating "
+ "element is: "
+ maxRepeating(arr,n,k));
}
}
|
PHP
<?php
function maxRepeating( $arr , $n , $k )
{
for ( $i = 0; $i < $n ; $i ++)
$arr [ $arr [ $i ] % $k ] += $k ;
$max = $arr [0];
$result = 0;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $arr [ $i ] > $max )
{
$max = $arr [ $i ];
$result = $i ;
}
}
return $result ;
}
$arr = array (2, 3, 3, 5, 3, 4, 1, 7);
$n = sizeof( $arr );
$k = 8;
echo "The maximum repeating number is " ,
maxRepeating( $arr , $n , $k );
?>
|
Javascript
<script>
function maxRepeating(arr, n, k)
{
for (let i = 0; i< n; i++)
arr[arr[i]%k] += k;
let max = arr[0], result = 0;
for (let i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
result = i;
}
}
return result;
}
let arr = [2, 3, 3, 5, 3, 4, 1, 7];
let n = arr.length;
let k = 8;
document.write( "The maximum repeating number is " +
maxRepeating(arr, n, k) + "<br>" );
</script>
|
Output
The maximum repeating number is 3
Time Complexity : O(n)
Auxiliary Space : O(1)
Exercise: The above solution prints only one repeating element and doesn’t work if we want to print all maximum repeating elements. For example, if the input array is {2, 3, 2, 3}, the above solution will print only 3. What if we need to print both of 2 and 3 as both of them occur maximum number of times. Write a O(n) time and O(1) extra space function that prints all maximum repeating elements. (Hint: We can use maximum quotient arr[i]/n instead of maximum value in step 2).
Note that the above solutions may cause overflow if adding k repeatedly makes the value more than INT_MAX.
Share your thoughts in the comments
Please Login to comment...