Related Articles
Count of elements which are not at the correct position
• Last Updated : 04 Jun, 2021

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:
Array in the sorted form will be {1, 2, 2, 4, 5, 6}
Input: arr[] = {1, 2, 3, 4}
Output:
All the elements are already sorted.

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[0]);` `    ``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`

## Javascript

 ``
Output:
`4`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up