Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 80 of 377

Program to find maximum distance between a pair of values in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 08-Oct-2021 729 Views

Suppose we have two arrays (non-growing) nums1 and nums2. The index pair (i, j) with 0

Read More

Program to find maximum element after decreasing and rearranging in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 08-Oct-2021 238 Views

Suppose we have an array called arr. We have to perform some operations on arr so that it satisfies these conditions −The first element in arr must be 1.The absolute difference between any 2 adjacent elements must be at most 1.And there are two operations. We can perform these two types of operations any number of times −Decrease any value of arr to a smaller positive number.Rearrange the elements of arr to be in any order.We have to find the maximum possible value in arr after performing the operations to satisfy the given conditions.So, if the input is like arr ...

Read More

Program to implement seat reservation manager in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 08-Oct-2021 2K+ Views

Suppose we have to design a system that manages the reservation state of n seats. The seats are numbered from 1 to n. So we have to implement the SeatReserveManager class, with these functions −Constructor that takes n as input and initializes the object that will manage n seats numbered from 1 to n. Initially all seats are available.reserve(), this will fetch the smallest-numbered unreserved seat, then reserves it, and returns its number.unreserve(seatNumber), this will unreserve one reserved seat with the given seatNumber.So, if the input is likeobj = SeatReserveManager(7)obj.reserve()obj.reserve()obj.reserve()obj.unreserve(2)obj.unreserve(5)obj.reserve()obj.reserve()then the output will be 1, 2, 3, 2, 5, initially ...

Read More

Program to find closest subsequence sum in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 08-Oct-2021 533 Views

Suppose we have an array nums and another value goal. We want to select a subsequence of nums such that the sum of its elements is the nearest to goal. So in other words, if the sum of the subsequence's elements is s, then we want to minimize the absolute difference |s - goal|.So we have to find the minimum possible value of |s - goal|. So, if the input is like nums = [8, -8, 16, -1] goal = -3, then the output will be 2 because select the subsequence [8, -8, -1], with a sum of -1. The ...

Read More

Program to find longest substring of all vowels in order in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 07-Oct-2021 680 Views

Suppose we have a string s with only English vowels, we have to find the length of the longest beautiful substring of s. If we cannot find such substring then, return 0. A string is said to be beautiful if it satisfies the following conditions −Each of the 5 vowels must appear at least once in it.Letters must be sorted in alphabetical sequenceSo, if the input is like s = "aaioaaaaeiiouuooaauu", then the output will be 10 because the substring is "aaaaeiiouu" which is beautiful.To solve this, we will follow these steps −vowels := a list of all vowels ['a', ...

Read More

Program to find maximum ice cream bars in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 07-Oct-2021 727 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
Arnab Chakraborty
Updated on 07-Oct-2021 323 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
Arnab Chakraborty
Updated on 07-Oct-2021 254 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 find total similarities of a string and its substrings in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 07-Oct-2021 395 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 number of nice subarrays in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 07-Oct-2021 880 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
Showing 791–800 of 3,768 articles
« Prev 1 78 79 80 81 82 377 Next »
Advertisements