# Minimum array element changes to make its elements 1 to N

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 than increment count. The final value of count will be the minimum changes required.

## 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<``int``> 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); ` `    ``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<``int``> s = ``new` `HashSet<``int``>();  ` `     `  `        ``// 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

 ` `

Output:

```3
```

Time Complexity: O(n)
Auxiliary Space: O(n)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.