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++ 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 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 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 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# 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 |
<script> // Javascript implementation of the approach // Function to return the count of // elements which are not in // the correct position when sorted function 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)