You are given n real numbers in an array. A number in the array is called a decimal dominant if it occurs more than n/10 times in the array. Give an O(n) time algorithm to determine if the given array has a decimal dominant.
Implement a function getbits, that returns the(right adjusted) n bits that begin at position p of an integer. Assume bit position 0 is at the right end and that n and p are sensible positive values.
You can do this in linear time by using a reverse() helper.
ReplyDelete// rotate array of size=size, by n positions
void rotate(int array[], int size, int n)
{
// reverse array[0...size-1]
reverse(array, 0, size-1);
// reverse A[0...n-1]
reverse(array, 0, n-1);
// reverse A[n...size-1]
reverse(array, n, size-1);
}
code :
void rev(int arr[],int start,int last)
{
int i,temp,j;
for(i=start,j=last;i<j;i++,j--)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int main()
{
int arr[]={1,2,3,4,5},i;
rev(arr,0,4);
rev(arr,0,1);
rev(arr,2,4);
for(i=0;i<5;i++)
printf("%d \t ",arr[i]);
return 0;
}
this runs in linear time.