To find incorrect position in array
Oct 04, 2019
array,
2339 Views
Given an array arr[] of n elements and the task is to count the number of elements from this array which are not at 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 correct position.
An element is said to be in an incorrect position if its position changes in the array when the array is sorted
Example: input: arr[]={1,2,6,2,4,5}
output: 4
#include<stdio.h>
#define max 50
void main()
{
int a[max],copy[max],i,j,n,temp,count=0;
printf("Enter the number of elements you want to enter:");
scanf("%d",&n);
printf("Enter the values:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
copy[i]=a[i];
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
if(a[i]!=copy[i])
count++;
}
printf("Number of elements in wrong position:%d",count);
}


