在 O(n) 时间和 O(1) 额外空间中找到最大重复数
给定一个大小为n的数组,该数组包含范围从 0 到k-1的数字,其中k是一个正整数并且k <= n。查找此数组中的最大重复数。例如,设k为 10,给定数组arr[] = {1, 2, 2, 2, 0, 2, 0, 2, 3, 8, 0, 9, 2, 3},最大重复次数为为 2. 预期时间复杂度为O(n)并且允许的额外空间为O(1) 。允许修改数组。
天真的方法是运行两个循环,外循环一个接一个地选取一个元素,内循环计算所选取元素的出现次数。最后返回具有最大计数的元素。这种方法的时间复杂度是O(n^2) 。
更好的方法是创建一个大小为 k 的 count 数组并将count[]的所有元素初始化为 0。遍历输入数组的所有元素,并为每个元素arr[i]递增count[arr[i]] 。最后,遍历count[]并返回最大值的索引。这种方法需要 O(n) 时间,但需要 O(k) 空间。
以下是O(n)时间和O(1)额外空间的方法。让我们通过一个简单的例子来理解这种方法,其中arr[] = {2, 3, 3, 5, 3, 4, 1, 7}, k = 8, n = 8(arr[] 中的元素数)。
1)遍历输入数组arr[] ,对于每个元素arr[i] ,将arr[arr[i]%k]增加k ( arr[]变为 {2, 11, 11, 29, 11, 12, 1, 15 })
2)在修改后的数组中找到最大值(最大值为29)。最大值的索引是最大重复元素(索引 29 是 3)。
3)如果我们想取回原始数组,我们可以再遍历一次数组并执行arr[i] = arr[i] % k其中i从 0 到n-1变化。
上述算法是如何工作的?由于我们使用arr[i]%k作为索引并在索引arr[i]%k处添加值k ,因此等于最大重复元素的索引最终将具有最大值。请注意,在等于最大重复元素的索引处添加k的最大次数,并且所有数组元素都小于k。
以下是上述算法的 C++ 实现。
C++
// C++ program to find the maximum repeating number
#include
using namespace std;
// Returns maximum repeating element in arr[0..n-1].
// The array elements are in range from 0 to k-1
int maxRepeating(int* arr, int n, int k)
{
// Iterate though input array, for every element
// arr[i], increment arr[arr[i]%k] by k
for (int i = 0; i< n; i++)
arr[arr[i]%k] += k;
// Find index of the maximum repeating element
int max = arr[0], result = 0;
for (int i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
result = i;
}
}
/* Uncomment this code to get the original array back
for (int i = 0; i< n; i++)
arr[i] = arr[i]%k; */
// Return index of the maximum element
return result;
}
// Driver program to test above function
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
// Java program to find the maximum repeating number
import java.io.*;
class MaxRepeating {
// Returns maximum repeating element in arr[0..n-1].
// The array elements are in range from 0 to k-1
static int maxRepeating(int arr[], int n, int k)
{
// Iterate though input array, for every element
// arr[i], increment arr[arr[i]%k] by k
for (int i = 0; i< n; i++)
arr[(arr[i]%k)] += k;
// Find index of the maximum repeating element
int max = arr[0], result = 0;
for (int i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
result = i;
}
}
/* Uncomment this code to get the original array back
for (int i = 0; i< n; i++)
arr[i] = arr[i]%k; */
// Return index of the maximum element
return result;
}
/*Driver function to check for above function*/
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));
}
}
/* This code is contributed by Devesh Agrawal */
Python3
# Python program to find the maximum repeating number
# Returns maximum repeating element in arr[0..n-1].
# The array elements are in range from 0 to k-1
def maxRepeating(arr, n, k):
# Iterate though input array, for every element
# arr[i], increment arr[arr[i]%k] by k
for i in range(0, n):
arr[arr[i]%k] += k
# Find index of the maximum repeating element
max = arr[0]
result = 0
for i in range(1, n):
if arr[i] > max:
max = arr[i]
result = i
# Uncomment this code to get the original array back
#for i in range(0, n):
# arr[i] = arr[i]%k
# Return index of the maximum element
return result
# Driver program to test above function
arr = [2, 3, 3, 5, 3, 4, 1, 7]
n = len(arr)
k = 8
print("The maximum repeating number is",maxRepeating(arr, n, k))
# This code is contributed by
# Smitha Dinesh Semwal
C#
//C# program to find the maximum repeating
// number
using System;
class GFG {
// Returns maximum repeating element
// in arr[0..n-1].
// The array elements are in range
// from 0 to k-1
static int maxRepeating(int []arr,
int n, int k)
{
// Iterate though input array, for
// every element arr[i], increment
// arr[arr[i]%k] by k
for (int i = 0; i< n; i++)
arr[(arr[i]%k)] += k;
// Find index of the maximum
// repeating element
int max = arr[0], result = 0;
for (int i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
result = i;
}
}
// Return index of the
// maximum element
return result;
}
/*Driver function to check for
above function*/
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));
}
}
// This code is contributed by Sam007.
PHP
$max)
{
$max = $arr[$i];
$result = $i;
}
}
/* Uncomment this code to
get the original array back
for (int i = 0; i< n; i++)
arr[i] = arr[i] % k; */
// Return index of the
// maximum element
return $result;
}
// Driver Code
$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);
// This Code is contributed by Ajit
?>
Javascript
输出:
The maximum repeating number is 3