Programming Articles - Page 1190 of 3363

Program to make a copy of a n-ary tree in Python

Arnab Chakraborty
Updated on 18-May-2021 12:01:45

2K+ Views

Suppose, we have been provided a n-ary tree whose root is given to us 'root'. We have to make a copy of the full n-ary binary tree and perform a preorder traversal of both trees. The copied tree has to be stored using another root node. The node structure of the tree is given below −Node:    value :    children : So, if the input is like, then the output will be[14, 27, 32, 42, 56, 65]The preorder representation of the input tree and the output tree will be the same as an exact copy of the tree ... Read More

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

Arnab Chakraborty
Updated on 18-May-2021 11:59:02

371 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.To solve this, we will follow these steps −change := 0even_1 := 0, even_0 := 0odd_1 := 0, odd_0 := 0for ... Read More

Program to check whether an array Is sorted and rotated in Python

Arnab Chakraborty
Updated on 18-May-2021 11:57:26

587 Views

Suppose we have an array called nums, we have to check whether the array was originally sorted in non-decreasing order, and then rotated some number of positions (may be zero) or not. Duplicates may also present in the array.So, if the input is like nums = [12, 15, 2, 5, 6, 9], then the output will be True, because it is rotated to the right for two placesTo solve this, we will follow these steps −j := 0while j < size of nums - 1 and nums[j] res[i + 1], thenreturn Falsereturn TrueExample (Python)Let us see the following implementation ... Read More

Program to find sum of unique elements in Python

Arnab Chakraborty
Updated on 18-May-2021 11:56:41

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.To solve this, we will follow these steps −count := a dictionary holding all unique elements and their frequencyans := 0for each index i and value v in nums, doif count[v] is same as 1, thenans := ans + ... Read More

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

Arnab Chakraborty
Updated on 18-May-2021 11:56:18

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. So if we put each ball in the box with a number same to the sum of digits of the ball's number. (As an example, the ball number 123 will be put in the box number 1 + 2 + 3 = 6). So if we have two values l and r, we have to find the number of balls in the box with the most balls.So, if the input ... Read More

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

Arnab Chakraborty
Updated on 18-May-2021 11:55:52

293 Views

Suppose we have a string s represents time in the form of hh:mm. Some of the digits in s are hidden (represented by ?). Considering 24hr clock, the valid times are between 00:00 and 23:59. We have to find the latest valid time that we can get from time by replacing the hidden digits.So, if the input is like s= "1?:?5", then the output will be 13:55 as the latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50.To solve this, we will follow these steps −ans := a new ... Read More

Program to find the highest altitude of a point in Python

Arnab Chakraborty
Updated on 18-May-2021 11:49:57

1K+ Views

Suppose there is a biker who is going on a road trip. There are n different points in his road trip at different altitudes. The biker starts his trip from point 0 with altitude 0. If we have a sequence called gain with n elements, gain[i] is the net gain in altitude between points i and i + 1 for all (0

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

Arnab Chakraborty
Updated on 18-May-2021 11:49:36

338 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

Program to recover decode XORed array in Python

Arnab Chakraborty
Updated on 18-May-2021 11:49:07

415 Views

Suppose we have a hidden array arr with n non-negative integers. Now this array is encoded into another array enc of length n-1. So here enc[i] = arr[i] XOR arr[i+1]. If we have the encoded enc array and an integer first, that is the first element of actual array, we have to find 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].To solve this, we will follow these steps −arr := an array with only one element firstfor i in range 0 ... Read More

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

Arnab Chakraborty
Updated on 18-May-2021 11:48:43

574 Views

Suppose you put 1Rs in a bank on first day say Monday. And every day from next day, Tuesday to Sunday, you put in 1Rs more than the day before. And on every subsequent Monday, you will put in 1Rs more than the previous Monday. If we have a number n, we have to find the total amount of money you will have in the bank at the end of the nth day.So, if the input is like n = 17, then the output will be 75 because, put 1Rs on Monday, 2Rs on Tuesday and so on, so 7Rs ... Read More

Advertisements