Programming Articles - Page 1037 of 3363

Program to find ways to make a fair array in Python

Arnab Chakraborty
Updated on 05-Oct-2021 12:45:38

366 Views

Suppose we have an array called nums. We can select exactly one index and remove the element from that index. (The index of the elements may change after the removal). We can say an array is fair when the sum of the odd-indexed values equals the sum of the even-indexed values. We have to find the number of indices that we could select such that after the removal, nums is fair.So, if the input is like nums = [5, 3, 7, 2], then the output will beRemove from index 0, array is [3, 7, 2], even position sum: 3+2 = ... Read More

Program to find smallest string with a given numeric value in Python

Arnab Chakraborty
Updated on 05-Oct-2021 12:39:59

525 Views

Suppose we have two values n and k. We have to find the lexicographically smallest string whose length is n and numeric value equal to k. The numeric value of a lowercase character is its position (starting from 1) in the alphabet, so the numeric value of character 'a' is 1, the numeric value of character 'b' is 2 and so on. And the numeric value of a string consisting of lowercase characters is the sum of its characters' numeric values.So, if the input is like n = 4 k = 16, then the output will be "aaam" because here ... Read More

Program to find minimum operations to reduce X to zero in Python

Arnab Chakraborty
Updated on 05-Oct-2021 12:34:15

390 Views

Suppose we have an array called nums and another value x. In one operation, we can either delete the leftmost or the rightmost element from the array and subtract the value from x. We have to find the minimum number of operations required to reduce x to exactly 0. If it is not possible then return -1.So, if the input is like nums = [4, 2, 9, 1, 4, 2, 3] x = 9, then the output will be 3 because at first we have to delete left most element 4, so array will be [2, 9, 1, 4, 2, ... Read More

Program to count minimum deletions needed to make character frequencies unique in Python

Arnab Chakraborty
Updated on 05-Oct-2021 12:26:07

297 Views

Suppose we have a string s, s is said to be good if there are no two different characters in s that have the same frequency. We have to find the minimum number of characters we need to delete to make s a good string.So, if the input is like s = "ssstttuu", then the output will be 2 because if we delete one 't', then there will be three 's', two 't' and two 'u', then again delete one, either 't' or 'u', to make them good.To solve this, we will follow these steps −val := a new map ... Read More

Program to count sorted vowel strings in Python

Arnab Chakraborty
Updated on 05-Oct-2021 12:22:05

738 Views

Suppose we have a number n, we have to find the number of strings of size n that consist only of vowels (a, e, i, o, u) and they are lexicographically sorted. We can say that a string s is lexicographically sorted when for all valid index i, s[i] is the same as or comes before s[i+1] in the alphabet.So, if the input is like n = 2, then the output will be 15 because there are many strings like ["aa", "ae", "ai", "ao", "au", "ee", "ei", "eo", "eu", "ii", "io", "iu", "oo", "ou", "uu"].To solve this, we will follow ... Read More

Program to count substrings that differ by one character in Python

Arnab Chakraborty
Updated on 05-Oct-2021 12:02:37

281 Views

Suppose we have two strings s and t, we have to find the number of ways we can select a nonempty substring of s and replace one single character by another different character such that the resulting substring is one of the substring of t. We have to find the number of substrings that satisfy the condition above.So, if the input is like s = "sts" t = "tsts", then the output will be 6 because the following are the pairs of substrings from s and t that differ by 1 character −("sts", "tsts"), ("sts", "tsts"), ("sts", "tsts"), ("sts", "tsts"), ... Read More

Program to find path with minimum effort in Python

Arnab Chakraborty
Updated on 05-Oct-2021 11:51:58

448 Views

Suppose we have 2D matrix of order m x n called height. The heights[i][j] represents the height of cell (i, j). If we are at (0, 0) cell we want to travel to the bottom-right cell, (m-1, n-1). We can move up, down, left, or right, and we wish to find a route that requires the minimum effort. In this problem the roots effort is the maximum absolute difference in heights between two consecutive cells of the route. So finally, we need to find minimum efforts needed to travel to the destination.So, if the input is like234495646then the output will ... Read More

Program to find best team with no conflicts in Python

Arnab Chakraborty
Updated on 05-Oct-2021 11:40:57

552 Views

Suppose we have two lists called scores and ages, where scores[i] and ages[i] represents the score and age of the ith player in a basketball game. We want to select the team with the highest overall score. Here the score of the team is the total sum of scores of all the players in the team. But we do not allow conflicts in the game. Here a conflict exists if a younger player has a strictly higher score than an older player.So, if the input is like scores = [5, 7, 9, 14, 19], ages = [5, 6, 7, 8, ... Read More

Program to find lexicographically smallest string after applying operations in Python

Arnab Chakraborty
Updated on 05-Oct-2021 11:25:54

326 Views

Suppose we have a string s with only numeric digits and also have two values a and b. We can apply any one of the following two operations any number of times and in any order on s −Add 'a' to all odd positioned items of s(0-indexed). If digit is 9, then by adding something with it will be cycled back to 0.Rotate 's' to the right by b positions.So we have to find the lexicographically smallest string we can get by applying the above operations any number of times on s.So, if the input is like s = "5323" ... Read More

Program to find number of sets of k-non-overlapping line segments in Python

Arnab Chakraborty
Updated on 05-Oct-2021 11:16:36

361 Views

Suppose we have n points on a line, where the ith point (from 0 to n-1) is at position x = i, we have to find the number of ways we can draw exactly k different non-overlapping line segments such that each segment covers two or more points. The endpoints of each line segment must have integral coordinates. The k line segments do not have to cover all given n points, and they can share endpoints. If the answer is too large, then return result mod 10^9+7.So, if the input is like n = 4 k = 2, then the ... Read More

Advertisements