Programming Articles - Page 1191 of 3366

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

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

319 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

406 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

566 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

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

Arnab Chakraborty
Updated on 18-May-2021 11:47:16

723 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.So, if the input is like boxTypes = [[2, ... Read More

Program to check whether String Halves Are Alike in Python

Arnab Chakraborty
Updated on 18-May-2021 11:45:15

464 Views

Suppose we have a string s whose length is even. We have to split this string into two different halves of same lengths. So consider 'a' is the first half and 'b' is the second half. We say two strings are alike when they have the same number of vowels (uppercase or lowercase). We have to check whether 'a' and 'b' are alike or not.So, if the input is like s = "talent", then the output will be True because two halves are "tal" and "ent", they are alike because they have only one vowel and two consonants.To solve this, ... Read More

Program to find reformatted phone number in Python

Arnab Chakraborty
Updated on 18-May-2021 11:44:35

410 Views

Suppose we have a phone number as string. The phone number number consists of digits, spaces and/or dashes '-'. We want to reformat the phone number in a certain manner. There are few rules −Remove all spaces and dashes at beginningGroup the digits from left side to right side into blocks of length 3 until there are 4 or less digits are left.The final digits are then grouped like −For 2 digits: A single block of length 2.For 3 digits: A single block of length 3.For 4 digits: Two more blocks of length 2 each.These blocks are then clubbed by ... Read More

Program to count number of matches played in tournament in Python

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

2K+ Views

Suppose we have a number n. So there are n number of teams in a tournament that has some rules −If the number of teams is even currently, then each team gets merged with another team. And a total of (n/2) matches are played, from them (n/2) winner teams will move to the next round.If the number of teams is odd, then one team randomly moved in the tournament, and the rest gets merged. So a total of (n-1)/2 matches are played, and (n-1)/2+1 teams are moved to the next round as winner.We have to find total number of matches ... Read More

Program to count the number of consistent strings in Python

Arnab Chakraborty
Updated on 18-May-2021 11:42:50

707 Views

Suppose we have a string s consisting of distinct characters and also have an array of strings called words. A string is consistent when all characters in the string appear in the string s. We have to find the number of consistent strings present in the array words.So, if the input is like s= "px", words = ["ad", "xp", "pppx", "xpp", "apxpa"], then the output will be 3 because there are three strings with only 'p' and 'x', ["xp", "pppx", "xpp"].To solve this, we will follow these steps −count := 0for i in range 0 to size of words - ... Read More

Program to find goal parser interpretation command in Python

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

480 Views

Suppose we have a Goal Parser that can interpret a given string command. A command consists ofAn alphabet "G", Opening and closing parenthesis "()"and/or "(al)" in some order.Our Goal Parser will interpret "G" as the string "G", "()" as "o", and "(al)" as the string "al". Finally interpreted strings are then concatenated in the original order. So if we have string command, we have to find the Goal Parser's interpretation of command.So, if the input is like command = "G()()()(al)(al)", then the output will be Goooalal.To solve this, we will follow these steps −s:= blank stringfor i in range 0 ... Read More

Python Program to find out the number of sets greater than a given value

Arnab Chakraborty
Updated on 18-May-2021 06:16:33

340 Views

Suppose, we have an array containing several integer numbers. We find out all the contiguous subarrays from the given array of numbers and put it into another list. Now, we replace each subarray with the maximum element in that subarray. There is also a number k given to us, and we have to find out how many subarrays are now greater than the given number.So, if the input is like input_array = [5, 6, 7, 8], k = 7, then the output will be 4The contiguous subarrays from the given input array are: {5}, {6}, {7}, {8}, {5, 6}, {6, ... Read More

Advertisements