Minimum array element changes to make its elements 1 to N
Last Updated :
08 Sep, 2022
Suppose you are given an array with N elements with any integer values. You need to find the minimum number of elements of the array which must be changed so that array has all integer values between 1 and N(including 1, N).
Examples:
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
We insert all elements in a hash table. We then iterate from 1 to N and check whether the element is present in the hash table. If it is not present then increment count. The final value of count will be the minimum changes required.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countChanges( int arr[], int n)
{
unordered_set< int > s;
for ( int i = 0; i < n; i++)
s.insert(arr[i]);
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
import java.util.Set;
import java.util.HashSet;
class GfG
{
static int countChanges( int arr[], int n)
{
Set<Integer> s = new HashSet<>();
for ( int i = 0 ; i < n; i++)
s.add(arr[i]);
int count = 0 ;
for ( int i = 1 ; i <= n; i++)
if (!s.contains(i))
count++;
return count;
}
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));
}
}
|
Python 3
def countChanges(arr, n):
s = []
for i in range (n):
s.append(arr[i])
count = 0
for i in range ( 1 , n + 1 ) :
if i not in s:
count + = 1
return count
if __name__ = = "__main__" :
arr = [ 8 , 55 , 22 , 1 , 3 , 22 , 4 , 5 ]
n = len (arr)
print (countChanges(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GfG
{
static int countChanges( int []arr, int n)
{
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < n; i++)
s.Add(arr[i]);
int count = 0;
for ( int i = 1; i <= n; i++)
if (!s.Contains(i))
count++;
return count;
}
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));
}
}
|
PHP
<?php
function countChanges(& $arr , $n )
{
$s = array ();
for ( $i = 0; $i < $n ; $i ++)
array_push ( $s , $arr [ $i ]);
$count = 0;
for ( $i = 1; $i <= $n ; $i ++)
if (!in_array( $i , $s ))
$count ++;
return $count ;
}
$arr = array (8, 55, 22, 1, 3, 22, 4, 5);
$n = sizeof( $arr );
echo countChanges( $arr , $n );
?>
|
Javascript
<script>
function countChanges(arr,n)
{
let s = new Set();
for (let i = 0; i < n; i++)
s.add(arr[i]);
let count = 0;
for (let i = 1; i <= n; i++)
if (!s.has(i))
count++;
return count;
}
let arr=[8, 55, 22, 1, 3, 22, 4, 5];
let n = arr.length;
document.write(countChanges(arr, n));
</script>
|
Complexities Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...