Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 117 of 377
Program to find number of ways to arrange n rooks so that they cannot attack each other in Python
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 6. ...
Read MoreProgram to find number of nodes in a range in Python
Given a Binary Search Tree (BST) and two bounds l and r, we need to count all nodes whose values fall within the range [l, r] (inclusive). For example, if we have a BST with nodes and l = 7, r = 13, we count nodes with values 8, 10, and 12, giving us a result of 3. 12 8 15 3 ...
Read MoreProgram to check whether given number is Narcissistic number or not in Python
A Narcissistic number (also called an Armstrong number) is a number that equals the sum of its digits raised to the power of the number of digits. For example, 153 is narcissistic because 1³ + 5³ + 3³ = 153. So, if the input is like 9474, then the output will be True as 9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256 = 9474. Algorithm To solve this, we will follow these steps − Convert the number to string to get individual digits Calculate the sum of each ...
Read MoreProgram to sort all even and odd numbers in increasing and decreasing order respectively in Python
Sometimes we need to sort a list where even and odd numbers follow different sorting rules while maintaining their relative positions. This problem requires sorting even numbers in ascending order and odd numbers in descending order. Problem Statement Given a list of numbers, we need to sort the array by maintaining the following criteria ? Even numbers are sorted in ascending order Odd numbers are sorted in descending order The relative positions of even and odd numbers should not be changed For example, if the input is [9, 14, 12, 91, -4, 5], the ...
Read MoreProgram to find all missing numbers from 1 to N in Python
Finding missing numbers from a range is a common programming problem. Given a list of numbers where each number should be in the range [1, N], we need to identify which numbers are missing. Some numbers may appear multiple times while others are missing entirely. So, if the input is like [4, 4, 2, 2, 6, 6], then the output will be [1, 3, 5] since these numbers are missing from the range [1, 6]. Using Array Counting Method This approach uses an auxiliary array to count occurrences of each number ? def find_missing_numbers(nums): ...
Read MoreProgram to find an element in list whose value is same as its frequency in Python
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 True because the number 4 appears 4 times in the list. Algorithm To solve this, we will follow these steps − Create a frequency map to store each element's count For each key-value pair (element, frequency) in the map, ...
Read MoreProgram to find the minimum cost to arrange the numbers in ascending or descending order in Python
Suppose we have a list of numbers called nums, we have to find the minimum cost to sort the list in any order (ascending or descending). Here the cost is the sum of differences between any element's old and new value. So, if the input is like [2, 5, 4], then the output will be 2. Algorithm To solve this, we will follow these steps − Create a copy of the array nums and sort it Calculate cost for ascending order: sum of absolute differences between original and sorted positions Calculate cost for descending order: ...
Read MoreProgram to merge two sorted list to form larger sorted list in Python
Suppose we have two sorted lists A and B. We have to merge them and form only one sorted list C. The size of lists may be different. For example, suppose A = [1, 2, 4, 7] and B = [1, 3, 4, 5, 6, 8], then merged list C will be [1, 1, 2, 3, 4, 4, 5, 6, 7, 8]. Algorithm We will solve this using the two-pointer technique. The algorithm works as follows ? Initialize an empty result list and two pointers i=0, j=0 While both pointers are within bounds: If ...
Read MoreProgram to split a list of numbers such that the absolute difference of median values are smallest in Python
Suppose we have a list of numbers called nums, we have to divide it into two parts of same size where the absolute difference between each list's median is as small as possible and we have to find this difference. We have to keep in mind that here length of nums / 2 will be odd. So, if the input is like [2, 10, 8, 5, 4, 7], then the output will be 2, as we can make two lists like [2, 5, 10] and [4, 7, 8], then the medians are 5 and 7, their difference is 2. ...
Read MoreProgram to count how many times we can find "pizza" with given string characters in Python
When we need to count how many times we can form the word "pizza" from available characters in a string, we need to check the frequency of each required character. Since "pizza" contains 'p', 'i', 'z', 'z', 'a', we need at least 1 'p', 1 'i', 2 'z', and 1 'a' to form one complete word. So, if the input is like "ihzapezlzzilaop", then the output will be 2. Algorithm To solve this problem, we follow these steps ? Count frequency of 'p' in the string Count ...
Read More