给定一个由N个元素组成的数组arr [] ,任务是计算该数组中位置不正确的元素的数量。如果元素在排序数组时在数组中的位置发生变化,则称该元素的位置不正确。
例子:
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.
方法:首先将数组元素复制到另一个数组中,例如B [],然后对给定数组进行排序。开始遍历数组,并且对于每个元素,如果arr [i]!= B [i] ,则该元素不在给定数组中的正确位置。
下面是上述方法的实现:
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
输出:
4