Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 84 of 377

Program to find an element in list whose value is same as its frequency in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 231 Views

Suppose we have a list of numbers called nums, we have to check whether there is an element whose frequency in the list is same as its value or not.So, if the input is like [2, 4, 8, 10, 4, 4, 4], then the output will be TrueTo solve this, we will follow these steps −res := a new map to store value wise frequencyfor each key value pair (k, v) in res, doif k is same as v, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Exampleclass Solution:    def solve(self, nums):       ...

Read More

Program to find all missing numbers from 1 to N in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have a list of numbers called nums of size n where all numbers in the list are present in the interval [1, n] some elements may appear twice while others only once. We have to find all of the numbers from [1, n] such that they are not in the list. We have to return the numbers sorted in ascending order. We have to try to find a solution that takes linear time and constant space.So, if the input is like [4, 4, 2, 2, 6, 6], then the output will be [1, 3, 5].To solve this, we ...

Read More

Program to sort all even and odd numbers in increasing and decreasing order respectively in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 3K+ Views

Suppose we have a list of numbers called nums, we have to sort the array by maintaining following criteriaEven numbers are sorted in ascending orderOdd numbers are sorted in descending orderThe relative positions of the even and odd numbers should not be changed.So, if the input is like [9, 14, 12, 91, -4, 5], then the output will be [91, -4, 12, 9, 14, 5]To solve this, we will follow these steps −evens := list of even terms in nums arrayodds := list of odd terms in nums arraysort the list evenseven_i := 0, odd_i := 0for index in range ...

Read More

Program to check whether given number is Narcissistic number or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 881 Views

Suppose we have a number n; we have to check whether it is equal to the sum of the digits of n to the power of the number of digits.So, if the input is like 9474, then the output will be True as 9^4 + 4^4 + 7^4 + 4^4 = 6561 + 256 + 2401 + 256 = 9474.To solve this, we will follow these steps −s := a list of digits in of nreturn true if n is same as sum of x*(size of s) for all x in s, otherwise falseLet us see the following implementation to ...

Read More

Program to find number of nodes in a range in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 962 Views

Suppose we have a BST, and we also have left and right bounds l and r, we have to find the count of all nodes in root whose values are present in between l and r (inclusive).So, if the input is likel = 7, r = 13, then the output will be 3, as there are three nodes: 8, 10, 12.To solve this, we will follow these steps−stack := a stack and insert root at first, count := 0while stack is not empty, donode := top element of stack, and pop elementif node is not null, thenif l

Read More

Program to find number of ways to arrange n rooks so that they cannot attack each other in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 449 Views

Suppose we have a number n representing a chessboard of size n x n. We have to find the number of ways we can place n rooks, such that they cannot attack each other. Here two ways will be considered different if in one of the ways, some cell of the chessboard is occupied, and another way, the cell is not occupied. (We know that rooks can attack each other if they are either on the same row or on the same column).So, if the input is like 3, then the output will be 6To solve this, we will follow ...

Read More

Program to check minimum number of characters needed to make string palindrome in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 550 Views

Suppose we have a string s, we have to find the minimum number of characters needed to be inserted so that the string becomes a palindrome.So, if the input is like s = "mad", then the output will be 2, as we can insert "am" to get "madam".To solve this, we will follow these steps −Define a function dp(). This will take i, jif i >= j, thenreturn 0if s[i] is same as s[j], thenreturn dp(i + 1, j - 1)otherwise, return minimum of dp(i + 1, j) and dp(i, j - 1) + 1From the main method, do the ...

Read More

Program to find k-length paths on a binary tree in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 338 Views

Suppose we have a binary tree which contains unique values and we also have another value k, we have to find the number of k-length unique paths in the tree. The paths can go either from parent to child or from child to parent. We will consider two paths are different when some node appears in one path but not the other.So, if the input is likek = 3, then the output will be 4, as the paths are [12, 8, 3], [12, 8, 10], [8, 12, 15], [3, 8, 10].To solve this, we will follow these steps−Define a function ...

Read More

Program to find total number of strings, that contains one unique characters in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 246 Views

Suppose we have a string s of lowercase letters, we have to find the total number of substrings that contain one unique character.So, if the input is like "xxyy", then the output will be 6 as substrings are [x, x, xx, y, y, yy]To solve this, we will follow these steps −total := 0previous := blank stringfor each character c in s, doif c is not same as previous, thenprevious := ctemp := 1otherwise, temp := temp + 1total := total + tempreturn totalLet us see the following implementation to get better understanding −Exampleclass Solution:    def solve(self, s):   ...

Read More

Program to find the sum of first n odd numbers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 10K+ Views

Suppose we have one number n, we have to find the sum of the first n positive odd numbers.So, if the input is like 7, then the output will be 49 as [1+3+5+7+9+11+13] = 49To solve this, we will follow these steps −if n is same as 0, thenreturn 0sum := 1, count := 0, temp := 1while count < n-1, dotemp := temp + 2sum := sum + tempcount := count + 1return sumLet us see the following implementation to get better understanding −Exampleclass Solution:    def solve(self, n):       if n == 0:          return 0          sum = 1          count = 0          temp = 1          while(count

Read More
Showing 831–840 of 3,768 articles
« Prev 1 82 83 84 85 86 377 Next »
Advertisements