That’s quite a comprehensive list of programming problems! Here’s a structured breakdown for clarity, grouped by category:
Problems on Arrays
Basic Operations
- Find the smallest number in an array
- Find the largest number in an array
- Second smallest and second largest elements in an array
- Reverse a given array
- Count frequency of each element in an array
- Calculate the sum of the elements of the array
- Average of all elements in an array
- Find the median of the given array
Rearrangement and Rotation
- Rearrange array in increasing-decreasing order
- Rotate the array by K elements (Block Swap Algorithm)
- Remove duplicates from a sorted array
- Remove duplicates from an unsorted array
- Rotation of elements of array (left and right)
- Finding circular rotation of an array by K positions
Search and Rank
- Search an element in an array
- Replace each element of the array by its rank
- Sort elements of an array by frequency
- Sort an array according to the order defined by another array
- Check if an array is a subset of another array
Special Problems
- Find all repeating elements in an array
- Find all non-repeating elements in an array
- Find all symmetric pairs in an array
- Maximum product subarray in an array
- Finding equilibrium index of an array
Problems on Numbers
Number Properties
- Check if a number is palindrome
- Find all palindrome numbers in a given range
- Check if a number is prime
- Prime numbers in a given range
- Check if a number is Armstrong
- Check if a number is a perfect number
- Check if a number is even or odd
- Check if a number is positive or negative
Series and Sums
- Sum of first N natural numbers
- Sum of an AP series
- Sum of a GP series
- Sum of numbers in a given range
Comparisons and Operations
- Greatest of two numbers
- Greatest of three numbers
- Leap year or not
- Reverse digits of a number
- Maximum and minimum digit in a number
- Factorial of a number
- Power of a number
- Factors of a given number
- Print all prime factors of a given number
- GCD and LCM of two numbers
Special Numbers
- Check if a number is a strong number
- Check if a number is automorphic
- Check if a number is Harshad
- Check if a number is abundant
Other
- Permutations of N people occupying R seats
- Program to add two fractions
- Replace all 0s with 1s in a number
- Check if a number can be expressed as a sum of two primes
- Calculate the area of a circle
- Find roots of a quadratic equation
Problems on Number Systems
Conversions
- Binary to Decimal
- Binary to Octal
- Decimal to Binary
- Decimal to Octal
- Octal to Binary
- Octal to Decimal
Extras
- Convert digits/numbers to words
Problems on Sorting
- Algorithms
- Bubble Sort
- Selection Sort
- Insertion Sort
- Quick Sort
- Merge Sort
Problems on Strings
Basic Operations
- Check if a string is a palindrome
- Reverse a string
- Count number of vowels, consonants, spaces
- Remove all vowels from a string
- Remove spaces from a string
- Remove characters except alphabets
Frequency and Characters
- Frequency of characters
- Find non-repeating characters
- Maximum occurring character
- Remove duplicates
- Print all duplicates
Manipulations
- Capitalize first and last character of each word
- Change every letter to the next lexicographic alphabet
- Change case of each character
- Concatenate two strings
Search and Substrings
- Find a substring and its starting position
- Find a word with the highest number of repeated letters
Miscellaneous
- Reverse words in a string
- Sort characters in a string
- Count words in a string
- Check if two strings are anagrams
- Handle wildcards in string matches
- Remove characters of one string from another
01. Problems on Arrays - Basic Operations
# 1. Find the smallest number in an array
def find_smallest(arr):
return min(arr)
# 2. Find the largest number in an array
def find_largest(arr):
return max(arr)
# 3. Second smallest and second largest elements in an array
def find_second_smallest_largest(arr):
unique_sorted = sorted(set(arr))
if len(unique_sorted) < 2:
return None, None # Not enough elements
return unique_sorted[1], unique_sorted[-2]
# 4. Reverse a given array
def reverse_array(arr):
return arr[::-1]
# 5. Count frequency of each element in an array
from collections import Counter
def count_frequency(arr):
return dict(Counter(arr))
# 6. Calculate the sum of the elements of the array
def calculate_sum(arr):
return sum(arr)
# 7. Average of all elements in an array
def calculate_average(arr):
return sum(arr) / len(arr) if arr else 0
# 8. Find the median of the given array
def find_median(arr):
arr_sorted = sorted(arr)
n = len(arr_sorted)
mid = n // 2
if n % 2 == 0:
return (arr_sorted[mid - 1] + arr_sorted[mid]) / 2
else:
return arr_sorted[mid]
# Example usage
array = [7, 2, 9, 1, 4, 6, 2]
print("Smallest:", find_smallest(array))
print("Largest:", find_largest(array))
second_smallest, second_largest = find_second_smallest_largest(array)
print("Second Smallest:", second_smallest)
print("Second Largest:", second_largest)
print("Reversed Array:", reverse_array(array))
print("Frequency Count:", count_frequency(array))
print("Sum of Elements:", calculate_sum(array))
print("Average of Elements:", calculate_average(array))
print("Median:", find_median(array))
02. Rearrangement and Rotation
# 1. Rearrange array in increasing-decreasing order
def rearrange_increasing_decreasing(arr):
arr.sort()
mid = len(arr) // 2
return arr[:mid] + arr[mid:][::-1]
# 2. Rotate the array by K elements (Block Swap Algorithm)
def rotate_array(arr, k):
def swap_blocks(arr, start, end, d):
for i in range(d):
arr[start + i], arr[end + i] = arr[end + i], arr[start + i]
n = len(arr)
k %= n
if k == 0:
return arr
i, j = k, n - k
while i != j:
if i < j:
swap_blocks(arr, k - i, k + j - i, i)
j -= i
else:
swap_blocks(arr, k - i, k, j)
i -= j
swap_blocks(arr, k - i, k, i)
return arr
# 3. Remove duplicates from a sorted array
def remove_duplicates_sorted(arr):
return list(dict.fromkeys(arr))
# 4. Remove duplicates from an unsorted array
def remove_duplicates_unsorted(arr):
return list(dict.fromkeys(arr))
# 5. Rotation of elements of array (left and right)
def rotate_left(arr, k):
k %= len(arr)
return arr[k:] + arr[:k]
def rotate_right(arr, k):
k %= len(arr)
return arr[-k:] + arr[:-k]
# 6. Finding circular rotation of an array by K positions
def circular_rotation(arr, k):
return rotate_left(arr, k)
# Example usage
if __name__ == "__main__":
array = [7, 2, 9, 1, 4, 6, 2]
print("Original Array:", array)
# 1. Rearrange in increasing-decreasing order
print("Rearranged (Increasing-Decreasing):", rearrange_increasing_decreasing(array))
# 2. Rotate array by K elements
print("Rotated Array by 2 (Block Swap):", rotate_array(array.copy(), 2))
# 3. Remove duplicates from sorted array
print("Remove Duplicates (Sorted):", remove_duplicates_sorted(sorted(array)))
# 4. Remove duplicates from unsorted array
print("Remove Duplicates (Unsorted):", remove_duplicates_unsorted(array))
# 5. Rotation of elements (left and right)
print("Left Rotation by 2:", rotate_left(array, 2))
print("Right Rotation by 2:", rotate_right(array, 2))
# 6. Circular rotation by K positions
print("Circular Rotation by 3:", circular_rotation(array, 3))
Coming soon....
DSA Practice Sheet
*Company wise details for Questions coming soon.
Note – This Sheet is only meant for Prime & Super Students.
Coding Practice Sheet
*Company wise details for Questions coming soon.
Note – This Sheet is only meant for Prime & Super Students.
Mathematics Basics Python
- Sum of Natural Numbers
- Count Digits
- Palindrome Number
- Factorial of a Number
- Trailing Zeros in Factorial
- GCD or HCF of Two Numbers
- LCM of Two Numbers
- Check for Prime
- Prime Factorization
- All Divisors of a Number
- Sieve of Eratosthenes
- Computing Power
Iterative Power (Binary Exponentiation)
List Based Problems
- List (Dynamic Sized Array) Introduction
- Working of List in Python
- Average or Mean of a List
- Separate Even and Odd
- Get Smaller Elements
- Slicing (List, Tuple, and String)
- Comprehensions in Python
- Largest Element in a List
- Second Largest Element in a List
- Check if a List is Sorted
- Reverse a List in Python
- Remove Duplicates
- Left Rotate a List by One
Day 05 : Arrays V
20 Basic Numerical Problems and It’s Solution — Python Basics
1. Find the Sum of the First N Natural Numbers
Given a Natural Number of N, we need to find the sum of all the natural numbers until the value of N. We can use different methods to write the Python code and one of the most effective method is using For Loop.
Code:
range = int(input("Enter the ranges: "))
add = 0
for i in range(range+1):
add = add + i
print(f”The sum of the first {ranges} numbers is {add}“)
2. Check Whether a Number is Abundant Number or not
A number in which the sum of it’s divisors is greater than the number is known as Abundant Number
Let’s take number 12 as an example
Sum of it’s divisors = 1+2+3+4+6 = 16 which is greater than 12. Hence, It is an abundant number
Code:
num = int(input("Enter the number : "))
add = 0
for i in range(1, num):
if (num%i==0):
add = add + i
if (num<add):
print(f’{num} is an abundant number’)
else:
print(“It is not an abundant number”)
3. Check Whether a Number is Automorphic Number or Not
A Number is known as an Automorphic Number if the last digits of it’s square is same as the number itself
Let’s take number 76 as an example
76² = 5776
The last two digit of the square , 76 is equal to the number itself
Code:
num = int(input("Enter a number : "))
length = len(str(num))
sqr = str(num ** 2)
print(sqr)
new = int(sqr[(length):])
print(new)
if (new == num):
print(“It is an Automorphic Number”)
else:
print(“It is not an Automorphic Number”)
4. Print N number of Automorphic numbers in a given range
A Range N will be given and we need to print all the automorphic numbers up to that range. To Know more about Automorphic Number, Refer To Question 4.
Code :
range = int(input('Enter the given range : '))
for i in range(1, range):
sqr = i**2
mod = 10 ** len(str(i))
if (sqr%mod == i):
print(i)
5. Print Harshad Numbers in nth range
A number that is divisible by sum of it’s own digits is know an Harshad Number.
Example: 12 , 1+ 2 = 3 , 12 is divisible by 3
Code:
range = int(input("Enter the range : "))
for i in range(1, range):
add = 0 #Use Flag Equations in Outer Loop
for j in str(i):
add = add + int(j)
if (i %add == 0):
print(i)
6. Print the Armstrong Numbers in Nth Range
An number is an Armstrong number if the sum of its own digits, each raised to the power of n, is equal to the number itself.
Example:
153 = 1³ + 5³ + 3³ = 153
1634 = 1⁴ + 6⁴ + 3⁴ + 4⁴ = 1634
Code :
range = int(input("Enter the range : "))
for i in range(1, range):
add = 0
length = len(str(i))
temp = i
while (temp != 0):
reminder = temp % 10
add = add + (reminder**length)
temp = temp // 10
if (i==add):
print(i)
7. Check Whether a Number is Palindrome or Not
A number when reversed, equates to the same number is known as Palindrome Number
121 reversed is 121 itself
Code:
num = int(input("Enter a number: "))
temp = num
new = 0
while (temp!=0):
reminder = temp % 10
new = (new * 10) + reminder
temp = temp // 10
print(new)
if (num == new):
print(“The Number is Palindrome”)
else:
print(“The Number is not a palindrome”)
8. Print the Reverse of a Number
Code:
num = int(input("Enter a number: "))
temp = num
new = 0
while (temp!=0):
reminder = temp % 10
new = (new * 10) + reminder
temp = temp // 10
print(new)
9. Find the Sum of the digits of a number
Each digit of a number is taken and added to get the required result
Code:
num = int(input("Enter a number : "))
# Let us Do This By using modulus
add = 0
while (num!=0):
reminder = num % 10
add = add + reminder
num = num // 10
print(add)
10. Greatest Of Three Numbers
Code:
n1, n2, n3 = map(int, input("Enter Three Numbers : ").split())
if (n1>=n2) and (n1>=n3):
print(f’{n1} is the greatest’)
elif (n2>=n1) and (n2>=n3):
print(f’{n2} is the greatest’)
elif (n3>=n1) and (n3>=n2):
print(f’{n3} is the greatest’)
else:
print(“They are all Equal”)
11. Print Abundant Numbers in The range N
Numbers upto the range N is checked whether it is an abundant number or not. For more reference , Check Problem Number 3
Code:
# A number that is smaller than the sum of all its factors except the number itself is known as an abundant number
num = int(input(“Enter the number : “))
add = 0
for i in range(1, num):
if (num%i==0):
add = add + i
if (num<add):
print(f’{num} is an abundant number’)
else:
print(“It is not an abundant number”)
11. Check whether a Year is Leap Year or Not.
Conditions for a Leap Year :
- The year should be perfectly divisible by 400.
- The year divisible by 4 but not by 100.
Code
year = int(input("Enter a Year :"))
if (year%4==0 and year%100 !=0) or (year%400 == 0):
print(“Leap Year”)
else:
print(“No Leap Year”)
12. Checking Whether a number is prime or not
Prime Number is a number which is divisible by 1 and the number itself
Code
num = int(input("Enter a number : "))
flag = 0
if (num>1):
for i in range(2, num):
if (num%i==0):
flag = 1
break;
if flag == 1:
print(“Composite Number”)
else:
print(“Prime Number”)
else:
print(“Neither Prime Nor Composite”)
13.Print the Prime Numbers in a Given Range
Code
range = int(input("Enter the range you want: "))
for i in range(2, range):
flag = 0 #Used To Assume a number is prime , on every new outerloop , the value is resetted to 0
for j in range(2, i):
if (i%j==0):
flag = 1
break;
if (flag ==0):
print(i)
14. Find the Factorial of a Number
Example:
Factorial of 5 = 5*4*3*2*1 = 120
Code:
num = int(input("Enter a number : "))
product = 1
for i in range(1,num+1):
product = product * i
print(f”The Factorial of the number is {product}“)
15. Find the Power Of a Number using Iteration (Most Basic)
Code:
num, power = map(int, input("Enter the num and the power : ").split())
#print(num ** power)
# Using Simple Iteration
output = 1
for i in range(power):
output = output * num
print(output)
16. Check whether a number is Perfect Number or Not
A perfect number is a number whose sum of it’s divisors is equal to the number itself.
For example, 28
Sum of it’s divisors = 1 + 14 + 2 + 4 + 7 = 28 which is equal to the number itself
Code:
num = int(input("Enter a number : "))
add = 0
for i in range(1, num):
if num % i == 0:
add = add + i
if (num == add):
print(“Perfect Number”)
else:
print(“Not Perfect Number”)
Visualization
17. Find the Prime Factors of a Given Number:
Factors of a Number which is only divisible by 1 and the number itself is known as Prime Factors
Code:
n = int(input("Enter the number : "))
arr = []
for i in range(2, n):
if n % i == 0:
arr.append(i)
print(arr)
sol = []
for j in arr:
flag = 0
for k in range(2, j):
if j % k == 0:
flag = 1
break
if flag == 0:
sol.append(j)
print(“The Prime Factors of the number are: “)
for m in sol:
print(m, end=‘ ‘)
18. Find whether a number is perfect square or not
Examples of Perfect Number : 4, 9, 16, 25 etc
Code
import math
num = int(input("Enter the number : "))
result1 = int(math.sqrt(num))
if float(result1) == math.sqrt(num):
print("Yes")
else:
print("No")
19. Check Whether Two Numbers forms a friendly pair or not
- Read Two Numbers
- Find The Factors of Both The Numbers
- Divide the sum of the factors with the number for each of these numbers
- If the resultant of the above step for each numbers are same, then both of these numbers are said to be friendly pairs.
Code
num1, num2 = map(int, input("Enter the two numbers : ").split())
sum1 = 0
sum2 = 0
for i in range(1, num1+1):
if num1 % i == 0:
sum1 = sum1 + i
for j in range(1, num2+1):
if num2 % j == 0:
sum2 = sum2 + j
result1 = sum1 / num1
if (result1==result2):
print(f”{num1} and {num2} are friendly pairs”)
else:
print(“They are not friendly pairs”)
20. Check Whether or Not the Number is a Strong Number
A Number whose sum of the factorials of it’s digits is equal to the number itself is known as Strong Number
For Example :
145;
1!+4!+5! = 145; Hence it is a strong number.
Code
n = int(input("Enter a number : "))
digits = []
for i in str(n):
digits.append(int(i))
add = 0
for i in digits:
product = 1
for j in range(1, i+1):
product = product * j
add = add + product
print(add)
if (n==add):
print(“It is a strong number”)
else:
print(“It is not a strong number”)
Conclusion
These are some of the most important and basic numerical questions which are being repeatedly asked in various tests.
There are many other methods to solve the same question, explore them too. Try to solve the questions without looking at the given code and check whether you are getting the desired output.