Learn Basic Maths¶
Count digits in a number¶
Example 1:
Input:N = 12345
Output:5
Explanation: The number 12345 has 5 digits.
Example 2:
Input:N = 7789
Output: 4
Explanation: The number 7789 has 4 digits.
Bruite Force Approach¶
def countDigits(num):
count =0
while num > 0:
count +=1
num = num // 10 #remove last digit
return count
print(countDigits(12345))
print(countDigits(7789))
5 4
10.9
Complexity analysis¶
O(N) : N = log10 num
O(1)
Optimize time¶
import math
def countDigits(num):
count = math.log(num,10) + 1
return int(count)
print(countDigits(12345))
print(countDigits(7789))
5 4
Complexity analysis¶
O(1)
O(1)
Reverse digits of a number¶
Note: If a number has trailing zeros, then its reverse will not include them. For e.g., reverse of 10400 will be 401 instead of 00401.
Example 1:
Input:N = 12345
Output:54321
Explanation: The reverse of 12345 is 54321.
Example 2:
Input:N = 7789
Output: 9877
Explanation: The reverse of number 7789 is 9877.
def reverseDigits(num):
result =0
while num > 0 :
ld = num %10
result = result * 10 + ld
num = num // 10
return result
print(reverseDigits(12345))
print(reverseDigits(7789))
print(reverseDigits(10400))
54321 9877 401
complexity analysis¶
O(N) N = log10 num
O(1)
Check Palindrome¶
Check if a number is Palindrome or Not
Problem Statement: Given an integer N, return true if it is a palindrome else return false.
A palindrome is a number that reads the same backward as forward. For example, 121, 1331, and 4554 are palindromes because they remain the same when their digits are reversed.
Example 1:
Input:N = 4554
Output:Palindrome Number
Explanation: The reverse of 4554 is 4554 and therefore it is palindrome number
Example 2:
Input:N = 7789
Output: Not Palindrome
Explanation: The reverse of number 7789 is 9877 and therefore it is not palindrome
Approach¶
- make a original copy for num
- reverse the number
- check if original number is same as reversed number
def checkPalindrome(num):
original_num = num
## reverse the num
reverse =0
while num > 0 :
last_digit = num % 10
reverse = reverse * 10 + last_digit
## remove last digit
num = num // 10
## check if both are same
return reverse == original_num
print(checkPalindrome(4554))
print(checkPalindrome(7789))
True False
Complexity Analysis¶
- Time : O(N) where N = number of digits in num i.e. log(10 num)
- Space : O(1)
4554 == 4554
True
Check palidome num¶
when : $ -2^{31} \leq \text{num} \leq 2^{31} - 1 $
Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left. Example 2:
Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3:
Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
def checkPalindrome(num):
if num < 0:
return False
original_num = num
## reverse the num
reverse =0
while num > 0 :
last_digit = num % 10
reverse = reverse * 10 + last_digit
## remove last digit
num = num // 10
## check if both are same
return reverse == original_num
print(checkPalindrome(121))
print(checkPalindrome(-121))
print(checkPalindrome(10))
True False False
Find GCD of two numbers¶
Example 1:
Input:N1 = 9, N2 = 12
Output:3
Explanation:Factors of 9: 1, 3 and 9
Factors of 12: 1, 2, 3, 4, 6, 12
Common Factors: 1, 3 out of which 3 is the greatest hence it is the GCD.
Example 2:
Input:N1 = 20, N2 = 15
Output: 5
Explanation:Factors of 20: 1, 2, 4, 5
Factors of 15: 1, 3, 5
Common Factors: 1, 5 out of which 5 is the greatest hence it is the GCD.
brute force¶
- min of 2 nums
- loop from max to 1 and return that num which is divisible
def hcf(num1,num2):
min_val = min(num1,num2)
for val in range(min_val,0,-1):
if num1 % val ==0 and num2 % val ==0:
return val
return 1
print(hcf(9,12))
print(hcf(10,15))
3 5
complexity analysis¶
O(N) : N = min(num1,num2)
O(1)
Check if a number is armstrong number or not¶
An Amrstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits.
Example 1:
Input:N = 153
Output:True
Explanation: 13+53+33 = 1 + 125 + 27 = 153
Example 2:
Input:N = 371
Output: True
Explanation: 33+53+13 = 27 + 343 + 1 = 371
from math import log
def isArmstrong(num):
count = int(log(num,10))+1
result =0
original=num
while num > 0:
ld = num % 10
result = result + (ld ** count)
num = num // 10
return result == original
print(isArmstrong(153))
print(isArmstrong(371))
True True
complexity analysis¶
O(N) N = log 10 num
O(1)
Print all divisors of a given num¶
Example 1:
Input:N = 36
Output:[1, 2, 3, 4, 6, 9, 12, 18, 36]
Explanation: The divisors of 36 are 1, 2, 3, 4, 6, 9, 12, 18, 36.
Example 2:
Input:N =12
Output: [1, 2, 3, 4, 6, 12]
Explanation: The divisors of 12 are 1, 2, 3, 4, 6, 12.
bruite force¶
def divisors(num):
result = []
for val in range(1,num +1):
if num % val ==0:
result.append(val)
return result
print(divisors(36))
print(divisors(12))
[1, 2, 3, 4, 6, 9, 12, 18, 36] [1, 2, 3, 4, 6, 12]
complexity analysis¶
O(N) N = num
O(N) N = 2 * sqrt(num)
optimize time¶
- instead of running the loop till num , we can run it till sqrt(num)
- if num % val ==0 , then val is divisor and num // val is also a divisor
import math
def divisors(num):
result = set()
max = math.floor(math.sqrt(num))
for val in range(1,max +1):
if num % val ==0:
result.add(val)
result.add(num//val)
return list(result)
print(divisors(36))
print(divisors(12))
Complexity analysis¶
O(N) N = sqrt(num)
O(N) N = 2 * sqrt(num)
Check if a num is a prime numbe or not¶
Problem Statement: Given an integer N, check whether it is prime or not. A prime number is a number that is only divisible by 1 and itself and the total number of divisors is 2.
Example 1:
Input:N = 2
Output:True
Explanation: 2 is a prime number because it has two divisors: 1 and 2 (the number itself).
Example 2:
Input:N =10
Output: False
Explanation: 10 is not prime, it is a composite number because it has 4 divisors: 1, 2, 5 and 10.
Bruite force¶
def isPrime(num):
for val in range(2,num):
if num % val ==0:
return False
return True
print(isPrime(2))
print(isPrime(10))
True False
complexity analysis¶
O(N) N = num -2
O(1)
Optimise time¶
- No need to check till num ,
- if there are no divisors till sqrt(num) , then also it is a prime
from math import floor, sqrt
def isPrime(num):
max = floor(sqrt(num))
for val in range(2,max):
if num % val ==0:
return False
return True
print(isPrime(2))
print(isPrime(10))
True False
Complexity Analysis¶
O(N) N = sqrt(num)
O(1)