Programming Articles

Page 524 of 2547

Program to find duplicate element from n+1 numbers ranging from 1 to n in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 339 Views

Suppose we have a list of numbers called nums of length n + 1. These numbers are picked from range 1, 2, ..., n. As we know, using the pigeonhole principle, there must be a duplicate. We have to find that and return it. So, if the input is like [2, 1, 4, 3, 3], then the output will be 3. Approach To solve this, we will follow these steps − l := size of nums temp := l*(l-1) /2 temp_sum := sum ...

Read More

Program to check programmers convention arrangements are correct or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 170 Views

Suppose we have a number n representing programmers looking to enter a convention, and we also have a list of numbers where 1 represents a programmer and 0 represents empty space. The condition is that no two programmers can sit next to each other. We need to check whether all n programmers can enter the convention or not. So, if the input is like n = 2, convention = [0, 0, 1, 0, 0, 0, 1], then the output will be True because we can place 2 more programmers without violating the adjacency rule. Algorithm To solve ...

Read More

Program to find the formatted amount of cents of given amount in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 467 Views

When dealing with financial calculations in Python, we often need to format amounts from cents to currency format. This involves converting a numeric value representing cents into a properly formatted string with dollars and cents separated by a decimal point, and thousands separated by commas. So, if the input is like n = 123456, then the output will be "1, 234.56". Algorithm To solve this, we will follow these steps − Convert the number to a string to work with digits Handle special cases where the amount is less than $1.00 Separate the dollars and ...

Read More

Program to find the nth row of Pascal's Triangle in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Pascal's Triangle is a triangular array of numbers where each row represents the binomial coefficients. We need to find the nth row (0-indexed) of this mathematical structure. Understanding Pascal's Triangle Pascal's Triangle follows these rules: The top row contains only [1] Each subsequent row starts and ends with 1 Each interior number is the sum of the two numbers above it 1 1 1 1 2 1 1 3 3 1 ...

Read More

Program to count number of valid pairs from a list of numbers, where pair sum is odd in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 414 Views

Suppose we have a list of positive numbers, we have to find the number of valid pairs of indices (i, j), where i < j, and nums[i] + nums[j] is an odd number. An important mathematical property: the sum of two numbers is odd only when one number is even and the other is odd. This means we need to count pairs where one element is even and another is odd. So, if the input is like [5, 4, 6], then the output will be 2, as two pairs are [5, 4] and [5, 6], whose sums are ...

Read More

Program to check old and new version numbering are correct or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 232 Views

Version comparison is a common task in software development. We need to compare two version strings in the format "major.minor.patch" to determine if the newer version is actually newer than the older one. So, if the input is like older = "7.2.2", newer = "7.3.1", then the output will be True because 7.3.1 is newer than 7.2.2. Algorithm To solve this problem, we will follow these steps − Split both version strings into lists of major, minor, patch numbers Compare each component (major, minor, patch) from left to right If newer component is greater, return ...

Read More

Program to count number of elements in a list that contains odd number of digits in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 608 Views

Given a list of positive numbers, we need to count how many elements have an odd number of digits. For example, the number 123 has 3 digits (odd), while 1234 has 4 digits (even). So, if the input is like [1, 300, 12, 10, 3, 51236, 1245], then the output will be 4 because numbers 1, 3, 300, and 1245 have odd digit counts (1, 1, 3, and 4 digits respectively). Algorithm To solve this, we will follow these steps: Initialize counter c = 0 For each ...

Read More

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

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

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 More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 263 Views

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 More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 360 Views

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 More
Showing 5231–5240 of 25,466 articles
« Prev 1 522 523 524 525 526 2547 Next »
Advertisements