String Searching , Mod Function
We have to check whether a number as an input is divisible by 21 or not or whether it contains 21 as a part of it.
Iterate over the digits of the input number to check whether there is an occurrence of 21 . Then check whether the modulus operator of the input digit with 21 is zero and if any one of the condition satisfies then the number is not acceptable otherwise it is acceptable.
We can take the input 'N' and check it for two conditions : 1) We can iterate through every digit of the number to find the occurrence of 21. This can be done in O(Len(N)): Pseudo Code : for i = 0 to len(N)-1 : if N[i]==2 and N[i+1]==1 : Pattern 21 Found. Condition False break; end if end for if i==len(N): Pattern not found . Condition Accepted 2) We can check whether the input number is divisible by 21 or not. This can be done in O(1) approach. Pseudo Code : if(N%21==0): Condition False else: Condition Accepted Time Complexity for our code : O(N);
Can be found here.