# Count of elements which are not at the correct position

Given an array arr[] of N elements and the task is to count the number of elements from this array which are not at the correct position. An element is said to be in an incorrect position if its position changes in the array when the array is sorted.

Examples:

Input: arr[] = {1, 2, 6, 2, 4, 5}
Output: 4
Array in the sorted form will be {1, 2, 2, 4, 5, 6}

Input: arr[] = {1, 2, 3, 4}
Output: 0
All the elements are already sorted.

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

Approach: First copy the array elements in another array say B[] then sort the given array. Start traversing the array and for every element if arr[i] != B[i] then it is the element which was not at the right position in the given array.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the count of ` `// elements which are not in ` `// the correct position when sorted ` `int` `cntElements(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``// To store a copy of the ` `    ``// original array ` `    ``int` `copy_arr[n]; ` ` `  `    ``// Copy the elements of the given ` `    ``// array to the new array ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``copy_arr[i] = arr[i]; ` ` `  `    ``// To store the required count ` `    ``int` `count = 0; ` ` `  `    ``// Sort the original array ` `    ``sort(arr, arr + n); ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// If current element was not ` `        ``// at the right position ` `        ``if` `(arr[i] != copy_arr[i]) { ` `            ``count++; ` `        ``} ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 2, 6, 2, 4, 5 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << cntElements(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to return the count of  ` `    ``// elements which are not in  ` `    ``// the correct position when sorted  ` `    ``static` `int` `cntElements(``int` `arr[], ``int` `n)  ` `    ``{  ` `     `  `        ``// To store a copy of the  ` `        ``// original array  ` `        ``int` `copy_arr[] = ``new` `int``[n];  ` `     `  `        ``// Copy the elements of the given  ` `        ``// array to the new array  ` `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `            ``copy_arr[i] = arr[i];  ` `     `  `        ``// To store the required count  ` `        ``int` `count = ``0``;  ` `     `  `        ``// Sort the original array  ` `        ``Arrays.sort(arr);  ` `         `  `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{  ` `     `  `            ``// If current element was not  ` `            ``// at the right position  ` `            ``if` `(arr[i] != copy_arr[i]) ` `            ``{  ` `                ``count++;  ` `            ``}  ` `        ``}  ` `        ``return` `count;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args) ` `    ``{  ` `        ``int` `arr[] = { ``1``, ``2``, ``6``, ``2``, ``4``, ``5` `};  ` `        ``int` `n = arr.length;  ` `     `  `        ``System.out.println(cntElements(arr, n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to return the count of  ` `# elements which are not in  ` `# the correct position when sorted  ` `def` `cntElements(arr, n) : ` ` `  `    ``# To store a copy of the  ` `    ``# original array  ` `    ``copy_arr ``=` `[``0``] ``*` `n  ` ` `  `    ``# Copy the elements of the given  ` `    ``# array to the new array  ` `    ``for` `i ``in` `range``(n):  ` `        ``copy_arr[i] ``=` `arr[i]  ` ` `  `    ``# To store the required count  ` `    ``count ``=` `0` ` `  `    ``# Sort the original array  ` `    ``arr.sort()  ` `    ``for` `i ``in` `range``(n):  ` ` `  `        ``# If current element was not  ` `        ``# at the right position  ` `        ``if` `(arr[i] !``=` `copy_arr[i]) :  ` `            ``count ``+``=` `1` `             `  `    ``return` `count ` `     `  `# Driver code  ` `arr ``=` `[ ``1``, ``2``, ``6``, ``2``, ``4``, ``5` `]  ` `n ``=` `len``(arr)  ` ` `  `print``(cntElements(arr, n))  ` ` `  `# This code is contributed by ` `# divyamohan123 `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to return the count of  ` `    ``// elements which are not in  ` `    ``// the correct position when sorted  ` `    ``static` `int` `cntElements(``int` `[] arr, ``int` `n)  ` `    ``{  ` `     `  `        ``// To store a copy of the  ` `        ``// original array  ` `        ``int` `[] copy_arr = ``new` `int``[n];  ` `     `  `        ``// Copy the elements of the given  ` `        ``// array to the new array  ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `            ``copy_arr[i] = arr[i];  ` `     `  `        ``// To store the required count  ` `        ``int` `count = 0;  ` `     `  `        ``// Sort the original array  ` `        ``Array.Sort(arr);  ` `         `  `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{  ` `     `  `            ``// If current element was not  ` `            ``// at the right position  ` `            ``if` `(arr[i] != copy_arr[i]) ` `            ``{  ` `                ``count++;  ` `            ``}  ` `        ``}  ` `        ``return` `count;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main (String[] args) ` `    ``{  ` `        ``int` `[] arr = { 1, 2, 6, 2, 4, 5 };  ` `        ``int` `n = arr.Length;  ` `     `  `        ``Console.WriteLine(cntElements(arr, n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by Mohit kumar `

Output:

```4
```

My Personal Notes arrow_drop_up 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 :

Be the First to upvote.

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