Python Articles

Page 115 of 855

Program to find out if the strings supplied differ by a character in the same position in Python

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

Suppose, we are provided an array that contains several strings of the same length. We have to find out if any two of the supplied strings differ by a single character at the same position. If this difference is present, we return True, otherwise we return False. So, if the input is like words = ['pqrs', 'prqs', 'paqs'], then the output will be True. The output is True because all strings differ at index 1 ('q', 'r', 'a'), so any two pairs have a difference in the same position. Algorithm Steps To solve this, we will follow ...

Read More

Program to find out the index of the most frequent element in a concealed array in Python

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

Suppose we are given a class called TestArray that contains a private array which can only contain values 0 or 1, and two public member functions length() and query(). The function length() returns the length of the array and the function query() returns three different values comparing various values in the array. The function takes four indices p, q, r, s as input and works like this: If all four values at the given indices are the same (either all 0s or all 1s), it returns 4 If three values ...

Read More

Program to find out the index in an array where the largest element is situated in Python

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

Finding the index of the largest element in an array is a common problem. This tutorial demonstrates a unique approach where the array is encapsulated in a class and can only be accessed through specific member functions. Problem Statement We have a class called TestArray that contains an array not accessible by other classes. It provides two public member functions: length() − returns the length of the array compare(l, r, x, y) − compares sums of two subarrays The compare() function works as follows: ...

Read More

Program to find the diameter of a n-ary tree in Python

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

An n-ary tree is a tree where each node can have any number of children. The diameter of an n-ary tree is the longest path between any two nodes in the tree. This path doesn't necessarily have to pass through the root node. So, if the input is like ? 14 27 ...

Read More

Program to find the root of a n-ary tree in Python

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

In an n-ary tree, each node can have multiple children. When given nodes of an n-ary tree in an array, we need to find the root node and reconstruct the tree. The root node is the only node that has no parent (in-degree = 0). Algorithm To find the root of an n-ary tree, we use the concept of in-degree: Create an in-degree counter for all nodes For each node, increment the in-degree of its children The node with in-degree 0 is the root Return the root node for tree reconstruction Implementation ...

Read More

Program to find minimum changes required for alternating binary string in Python

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

Suppose we have a binary string s. Let us consider an operation where we can flip one bit. The string s is called alternating string if no two adjacent characters are same. We have to find the minimum number of operations needed to make s alternating. So, if the input is like s = "11100011", then the output will be 3 because if we flip bits at position 1, 4 and 7, then, it will be "10101010", then all are alternating. Approach The key insight is that there are only two possible alternating patterns: Pattern ...

Read More

Program to find sum of unique elements in Python

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

Suppose we have an array nums with few duplicate elements and some unique elements. We have to find the sum of all the unique elements present in nums. So, if the input is like nums = [5, 2, 1, 5, 3, 1, 3, 8], then the output will be 10 because only unique elements are 8 and 2, so their sum is 10. Approach To solve this, we will follow these steps − count := a dictionary holding all unique elements and their frequency ans := 0 ...

Read More

Program to find maximum number of balls in a box using Python

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

Suppose we have a ball factory where we have n balls numbered from l to r (both inclusive) and have an infinite number of boxes numbered from 1 to infinity. We put each ball in the box with a number equal to the sum of digits of the ball's number. For example, ball number 123 will be put in box number 1 + 2 + 3 = 6. Given two values l and r, we need to find the maximum number of balls in any single box. Example If the input is l = 15 and r = ...

Read More

Program to find latest valid time by replacing hidden digits in Python

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

Suppose we have a string representing time in the format hh:mm. Some digits are hidden (represented by ?). Considering a 24-hour clock, valid times are between 00:00 and 23:59. We need to find the latest valid time by replacing the hidden digits. For example, if the input is "1?:?5", the output will be "19:55" as we want the latest possible time. Algorithm To solve this problem, we need to consider the constraints of a 24-hour time format ? First digit of hour: can be 0, 1, or 2 Second digit of hour: depends on the ...

Read More

Program to find the highest altitude of a point in Python

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

Suppose there is a biker going on a road trip through n different points at various altitudes. The biker starts from point 0 with altitude 0. Given a sequence called gain with n elements, gain[i] represents the net gain in altitude between points i and i + 1. We need to find the highest altitude reached during the trip. For example, if gain = [-4, 2, 6, 1, -6], the altitudes at each point would be [0, -4, -2, 4, 5, -1], making the highest altitude 5. Algorithm To solve this problem, we follow these steps ? ...

Read More
Showing 1141–1150 of 8,549 articles
« Prev 1 113 114 115 116 117 855 Next »
Advertisements