Count of elements which are not at the correct position

28 Jun, 2022
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.  
 
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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the count of// elements which are not in// the correct position when sortedint 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 codeint main(){    int arr[] = { 1, 2, 6, 2, 4, 5 };    int n = sizeof(arr) / sizeof(arr[0]);     cout << cntElements(arr, n);     return 0;}
// Java implementation of the approachimport 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 implementation of the approach # Function to return the count of# elements which are not in# the correct position when sorteddef 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 codearr = [ 1, 2, 6, 2, 4, 5 ]n = len(arr) print(cntElements(arr, n)) # This code is contributed by# divyamohan123
// C# implementation of the approachusing 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
<script>// Javascript implementation of the approach  // Function to return the count of// elements which are not in// the correct position when sortedfunction cntElements(arr, n) {     // To store a copy of the    // original array    let copy_arr = new Array(n);     // Copy the elements of the given    // array to the new array    for (let i = 0; i < n; i++)        copy_arr[i] = arr[i];     // To store the required count    let count = 0;     // Sort the original array    arr.sort((a, b) => a - b);    for (let 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 let arr = [1, 2, 6, 2, 4, 5];let n = arr.length; document.write(cntElements(arr, n)); // This code is contributed by gfgking.</script>
4
 
Time Complexity: O(n*log(n))Auxiliary Space: O(n)
divyamohan123
ankthon
mohit kumar 29
gfgking
pushpeshrajdx01
Arrays
Sorting
Arrays
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
Chocolate Distribution Problem
Find duplicates in O(n) time and O(1) extra space | Set 1
Merge Sort
Bubble Sort Algorithm
QuickSort
Insertion Sort
Selection Sort Algorithm