假设您有一个包含 N 个元素且具有任意整数值的数组。您需要找到必须更改的数组元素的最小数量,以便数组具有 1 和 N 之间的所有整数值(包括 1、N)。
例子:
Input : arr[] = {1 4 5 3 7}
Output : 1
We need to replace 7 with 2 to satisfy
condition hence minimum changes is 1.
Input : arr[] = {8 55 22 1 3 22 4 5}
Output :3
我们将所有元素插入到一个哈希表中。然后我们从 1 到 N 迭代并检查该元素是否存在于哈希表中。如果它不存在,则增加计数。 count 的最终值将是所需的最小更改。
C++
// Count minimum changes to make array
// from 1 to n
#include
using namespace std;
int countChanges(int arr[], int n)
{
// it will contain all initial elements
// of array for log(n) complexity searching
unordered_set s;
// Inserting all elements in a hash table
for (int i = 0; i < n; i++)
s.insert(arr[i]);
// Finding elements to be changed
int count = 0;
for (int i = 1; i <= n; i++)
if (s.find(i) == s.end())
count++;
return count;
}
int main()
{
int arr[] = {8, 55, 22, 1, 3, 22, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
cout << countChanges(arr, n);
return 0;
}
Java
// Count minimum changes to
// make array from 1 to n
import java.util.Set;
import java.util.HashSet;
class GfG
{
static int countChanges(int arr[], int n)
{
// It will contain all initial elements
// of array for log(n) complexity searching
Set s = new HashSet<>();
// Inserting all elements in a hash table
for (int i = 0; i < n; i++)
s.add(arr[i]);
// Finding elements to be changed
int count = 0;
for (int i = 1; i <= n; i++)
if (!s.contains(i))
count++;
return count;
}
// Driver code
public static void main(String []args)
{
int arr[] = {8, 55, 22, 1, 3, 22, 4, 5};
int n = arr.length;
System.out.println(countChanges(arr, n));
}
}
// This code is contributed by Rituraj Jain
Python 3
# Count minimum changes to
# make array from 1 to n
def countChanges(arr, n):
# it will contain all initial
# elements of array for log(n)
# complexity searching
s = []
# Inserting all elements in a list
for i in range(n):
s.append(arr[i])
# Finding elements to be changed
count = 0
for i in range(1, n + 1) :
if i not in s:
count += 1
return count
# Driver Code
if __name__ == "__main__":
arr = [8, 55, 22, 1, 3, 22, 4, 5]
n = len(arr)
print(countChanges(arr, n))
# This code is contributed
# by ChitraNayal
C#
// C# program to Count minimum changes to
// make array from 1 to n
using System;
using System.Collections.Generic;
class GfG
{
static int countChanges(int []arr, int n)
{
// It will contain all initial elements
// of array for log(n) complexity searching
HashSet s = new HashSet();
// Inserting all elements in a hash table
for (int i = 0; i < n; i++)
s.Add(arr[i]);
// Finding elements to be changed
int count = 0;
for (int i = 1; i <= n; i++)
if (!s.Contains(i))
count++;
return count;
}
// Driver code
public static void Main(String []args)
{
int []arr = {8, 55, 22, 1, 3, 22, 4, 5};
int n = arr.Length;
Console.WriteLine(countChanges(arr, n));
}
}
// This code is contributed by 29AjayKumar
PHP
Javascript
输出:
3
时间复杂度: O(n)
辅助空间: O(n)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。