Given an integer n, write a function that returns count of trailing zeroes in n!. Examples: Input: n = 5 Output: 1 Factorial of 5 is 20 which has one trailing 0. Input: n = 20 Output: 4 Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes. Input: n = 100 Output: 24
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.