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
Server Side Programming Articles
Page 521 of 2109
Program to find the sum of first n odd numbers in Python
In Python, finding the sum of the first n positive odd numbers is a common mathematical problem. The odd numbers sequence starts from 1, 3, 5, 7, 9, and so on. We can solve this using multiple approaches. So, if the input is like 7, then the output will be 49 as [1+3+5+7+9+11+13] = 49 Method 1: Using While Loop This approach iterates through the first n odd numbers and calculates their sum ? def sum_first_n_odds(n): if n == 0: return 0 ...
Read MoreProgram to find total number of strings, that contains one unique characters in Python
When working with strings, we often need to count substrings that contain only one unique character. This means finding all contiguous sequences where all characters are the same. So, if the input is like "xxyy", then the output will be 6 as the valid substrings are: ["x", "x", "xx", "y", "y", "yy"]. Algorithm To solve this problem, we follow these steps ? Initialize total count to 0 Track the previous character to detect character changes For each character in the string: If character differs from previous, start a new sequence Otherwise, extend the current ...
Read MoreProgram to find k-length paths on a binary tree in Python
Finding k-length paths in a binary tree is a classic dynamic programming problem on trees. We need to count all unique paths of exactly k nodes, where paths can traverse both upward (child to parent) and downward (parent to child). Problem Understanding Given a binary tree with unique values and a number k, we count paths of length k. Two paths are different if they contain different nodes or visit nodes in different order. 12 8 ...
Read MoreProgram 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 More