FOR FREE YEAR SOLVED

Duck Number

Back to Programming

Description

A number is called a duck number if the number contains at least one zero, but the number should not start with 0

 

For example, if we consider a number 20150, here two zeroes are present in the number, but it does not start with zero. Therefore, the number is a duck number.

But if we consider a number 02509, the number contains two zeroes but it starts at 0, therefore, it is not a duck number.

If the number is 1234, it does not contain any zero; therefore, it is not a duck number.

 

The number is taken here as the input, then the number of zeroes is counted. If the count is greater than 0 and it does not start with 0 then the number is a duck number otherwise not.

Algorithm

INPUT: A number
OUTPUT: Whether the number is a duck number or not.
PROCESS:
Step 1: [Taking the input]
	Read n [The number to be checked]
Step 2: [Checking for duck number]
	Set c<-0
	[Finding the length of the number]
	Set n<-length of the string stored in variable ‘s’
	For i=0 to n-1 repeat
		[Counting the number of zeroes present in the number]
		If s[i]='0' then
			Set c<-c+1
		[End of ‘if’]
	[End of ‘for’ loop]
	[Checking for the duck number]
	If s[0]≠'0' and c>0 then
		Print "The given number is a duck number"
	Else
		Print "The given number is not a duck number"
	[End of ‘if-else’]
Step 3: Stop.

Code

Time Complexity:

for(i=0;i<n;i++)------------------------------------O(n)

                {        //counting the number of zeroes present

                                //in the number

                                if(s[i]=='0')------------------------------O(1)

                                                c++; 

                }

                //checking for the duck number

                if(s[0]!='0'&&c>0)-------------------------------O(1)

                                printf("The given number is a duck number");

                else

                                printf("The given number is not a duck number");

 

The time complexity of this program is O(n) where ‘n’ is the number of digits of the input number to be checked as the duck number.

 

Space Complexity:

The space complexity of this program is O(1) as it requires a fixed number of memory space to execute the program for any given number.