Programming Articles

Page 375 of 2547

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 695 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 430 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 332 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

Program to find number of rectangles that can form the largest square in Python

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

Suppose we have an array called rect where rect[i] has two elements [len_i, wid_i], where len_i and wid_i are representing the length and width of ith rectangle respectively. Now we can cut the ith rectangle to form a square whose side length is of k if both k

Read More

Program to recover decode XORed array in Python

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

Suppose we have a hidden array arr with n non-negative integers. This array is encoded into another array enc of length n-1, where enc[i] = arr[i] XOR arr[i+1]. Given the encoded array and the first element of the original array, we need to recover the original array. So, if the input is like enc = [8, 3, 2, 7], first = 4, then the output will be [4, 12, 15, 13, 10]. Algorithm To solve this, we will follow these steps − arr := an array with only one element first for i in range ...

Read More

Program to find total amount of money we have in bank in Python

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

Suppose you put 1Rs in a bank on the first day (Monday). Each subsequent day of the week, you put in 1Rs more than the previous day. On every new Monday, you start with 1Rs more than the previous Monday. This creates a pattern where the amount increases both daily and weekly. For example, if n = 17 days: Week 1: 1Rs (Mon) + 2Rs (Tue) + 3Rs (Wed) + 4Rs (Thu) + 5Rs (Fri) + 6Rs (Sat) + 7Rs (Sun) = 28Rs Week 2: 2Rs (Mon) + 3Rs (Tue) + 4Rs (Wed) + 5Rs (Thu) + 6Rs ...

Read More

Program to find maximum units that can be put on a truck in Python

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

Suppose we have a set of boxes represented as a 2D array called boxTypes, where boxTypes[i] contains two elements [number of boxes of type i, number of units per box of type i]. Now we also have another value k, which is the maximum number of boxes that can be put on that truck. We can select any boxes to put on the truck as long as the number of boxes does not cross k. We have to find the maximum total number of units that can be put on the truck. Problem Understanding So, if the input ...

Read More
Showing 3741–3750 of 25,466 articles
« Prev 1 373 374 375 376 377 2547 Next »
Advertisements