Programming Articles - Page 1021 of 3363

Program to find frequency of the most frequent element in Python

Arnab Chakraborty
Updated on 07-Oct-2021 13:13:52

672 Views

Suppose we have an array nums and another value k. In one operation, we can select an index of nums and increase the element at that index by 1. We have to find the maximum possible frequency of an element after performing at most k number of operations.So, if the input is like nums = [8, 3, 6], k = 9, then the output will be 3 because we can update 3 by 5, 6 by 2, to make it [8, 8, 8] so after 7 operations we have maximum frequency 3.To solve this, we will follow these steps −sort ... Read More

Program to find maximum ice cream bars in Python

Arnab Chakraborty
Updated on 07-Oct-2021 13:08:09

711 Views

Suppose we have an array costs with n elements, where costs[i] is the price of the ith ice cream bar in coins. We initially have c number coins to spend, and we want to buy as many ice cream bars as possible. We have to find the maximum number of ice cream bars we can buy with c coins.So, if the input is like costs = [3, 1, 4, 5, 2], c = 10, then the output will be 4 because we can buy ice cream bars at indices 0, 1, 2, 4 for a total price of 3 + ... Read More

Program to find maximum XOR for each query in Python

Arnab Chakraborty
Updated on 07-Oct-2021 13:04:49

310 Views

Suppose we have an array which is presorted called nums of size n and also have one value b. We want to perform the following query n times −Search for a non-negative value k < 2^m such that XOR of all elements in nums and k is maximized. So k is the answer to the ith query.Remove the last element from the current array nums.We have to find an array answer, where answer[i] is the answer to the ith query.So, if the input is like nums = [0, 1, 1, 3], m = 2, then the output will be [0, ... Read More

Program to find length of longest fibonacci subsequence in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:59:42

242 Views

Suppose we have one sequence like X_1, X_2, ..., X_n is fibonacci-like if −n >= 3X_i + X_i+1 = X_i+2 for all i + 2 last, thencome out from loopreturn bestExampleLet us see the following implementation to get better understanding −from collections import Counter def solve(A):    sA = set(A)    last = A[-1]    B = Counter()    best = 0    for i in reversed(range(len(A))):       a = A[i]       for b in A[i+1:]:          c = a+b          if c in sA:         ... Read More

Program to count number of nice subarrays in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:55:07

848 Views

Suppose we have an array called nums and another value k. We have to find the number of nice sub-arrays. A subarray is said to be nice subarray if there are k odd numbers on it.So, if the input is like nums = [1, 1, 2, 1, 1], k = 3, then the output will be 2 because there are two subarrays [1, 1, 2, 1] and [1, 2, 1, 1].To solve this, we will follow these steps −odd_i := a new listfor i in range 0 to size of nums - 1, doif nums[i] mod 2 is same as ... Read More

Program to find minimum absolute sum difference in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:51:40

479 Views

Suppose we have two positive valued arrays nums1 and nums2, of same size. The absolute sum difference of these two arrays is the sum of |nums1[i] - nums2[i]| for each 0 [2, 2, 6], orReplace the element at index 1 with the element at index 2: [2, 8, 6] => [2, 6, 6].Both of them get a sum difference of |2-3| + (|2-4| or |6-4|) + |6-6| = 3.To solve this, we will follow these steps −if nums1 is same as nums2, thenreturn(0)minn_diff := -infinityind := -1for i in range 0 to size of nums1 - 1, doif |nums1[i]-nums2[i]| ... Read More

Program to get maximum length merge of two given strings in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:48:18

235 Views

Suppose we have two strings s and t. We have to make a string called merge in the following way: while either s or t are non-empty, select one of the following options −If s is non-empty, then append the first character in s to merge and delete it from s.If t is non-empty, then append the first character in t to merge and delete it from t.So we have to find the lexicographically largest merge we can make.So, if the input is like s = "zxyxx" t = "yzxxx", then the output will be zyzxyxxxxx, becausePick from s: merge ... Read More

Program to find total similarities of a string and its substrings in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:55:37

383 Views

Suppose we have a string s. We have to find the sum of similarities of string s with each of it's suffixes. Here the similarity between two strings are the length of the longest prefix common to both strings.So, if the input is like s = "pqpqpp", then the output will be 11 because the suffixes of the string are "pqpqpp", "qpqpp", "pqpp", "qpp", "pp" and "p". The similarities of these substrings with the string "pqpqpp" are 6, 0, 3, 0, 1, and 1. So the summation is 6 + 0 + 3 + 0 + 1 + 1 = ... Read More

Program to count nice pairs in an array in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:44:31

411 Views

Suppose we have an array called nums with non-negative values. We have to find number nice pairs of indices present in the array, if the answer is too large, then return answer mod 10^9+7. Here a pair of indices (i, j) is said to be nice when they satisfy all of these conditions: 1. 0

Program to convert hour minutes’ time to text format in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:42:35

677 Views

Suppose we have two inputs hour and minutes. We have to show the time in text format. This is like −8:00 : 8'o clock8:01 : one minute past eight8:10 : ten minutes past eight8:15 : quarter past eight8:30 : half past eight8:40 : twenty minutes to nine8:45 : quarter to nine8:47 : thirteen minutes to nine8:28 : twenty eight minutes past eightSo, if the input is like h = 9, m = 42, then the output will be eighteen minutes to tenTo solve this, we will follow these steps −text:= a list containing texts for 30 different numeric values as ... Read More

Advertisements