Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 35 of 377

Program to find maximum element after decreasing and rearranging in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 262 Views

We are given an array and need to perform operations to satisfy these conditions: The first element must be 1 The absolute difference between any two adjacent elements must be at most 1 We can perform two operations any number of times: Decrease any value to a smaller positive number Rearrange elements in any order We need to find the maximum possible value after performing operations to satisfy the conditions. Example If the input is arr = [3, 3, 2, 3, 2], the output will be 3. We can decrease ...

Read More

Program to implement seat reservation manager in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 2K+ Views

A seat reservation manager system tracks the availability of n seats numbered from 1 to n. We need to implement a SeatReserveManager class that can reserve the smallest available seat and unreserve specific seats efficiently. Problem Requirements The SeatReserveManager class should have: Constructor that takes n as input and initializes n seats numbered from 1 to n. Initially all seats are available. reserve() method that finds the smallest-numbered unreserved seat, reserves it, and returns its number. unreserve(seatNumber) method that unreserves a previously reserved seat with the given seat number. Algorithm Approach We use ...

Read More

Program to find smallest value of K for K-Similar Strings in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 297 Views

Given two anagram strings s and t, we need to find the minimum number of swaps required to transform s into t. Two strings are K-similar if we can swap exactly K pairs of characters in s to make it equal to t. For example, if s = "abc" and t = "bac", we need only 1 swap to transform "abc" to "bac" by swapping characters at positions 1 and 2. Algorithm We use BFS (Breadth-First Search) to find the minimum number of swaps ? neighbors() function: Generates all possible strings after one valid swap ...

Read More

Program to find closest subsequence sum in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 546 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. In other words, if the sum of the subsequence's elements is s, then we want to minimize the absolute difference |s - goal|. We have to find the minimum possible value of |s - goal|. Example If the input is nums = [8, -8, 16, -1] and goal = -3, then the output will be 2 because we select the subsequence [8, -8, -1], with a sum ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 693 Views

A beautiful substring contains all five vowels (a, e, i, o, u) in alphabetical order, with each vowel appearing at least once. We need to find the longest such substring in a given string. Problem Definition A string is beautiful if it satisfies these conditions − Each of the 5 vowels must appear at least once in it Letters must be sorted in alphabetical sequence For example, in string "aaioaaaaeiiouuooaauu", the substring "aaaaeiiouu" is beautiful and has length 10. Algorithm We use a two-pointer sliding window approach − Initialize vowels list ...

Read More

Program to find maximum ice cream bars in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 739 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 1, 4, 0, 2 (after sorting: costs [1, 2, ...

Read More

Program to find maximum XOR for each query in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 340 Views

Suppose we have a presorted array called nums of size n and a value m. 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. Example Walkthrough If the input is like nums ...

Read More

Program to find length of longest fibonacci subsequence in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 273 Views

A Fibonacci-like subsequence is a sequence where each element (starting from the third) is the sum of the two preceding elements. Given a strictly increasing array, we need to find the length of the longest Fibonacci-like subsequence. Definition A sequence X₁, X₂, ..., Xₙ is Fibonacci-like if: n >= 3 Xᵢ + Xᵢ₊₁ = Xᵢ₊₂ for all valid i Algorithm We'll use dynamic programming with a dictionary to track subsequence lengths ? Convert array to set for O(1) lookups Use a Counter to store lengths of subsequences ending at each pair ...

Read More

Program to count number of nice subarrays in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 891 Views

Suppose we have an array called nums and another value k. We have to find the number of nice subarrays. A subarray is said to be nice if it contains exactly k odd numbers. 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] that contain exactly 3 odd numbers each. Algorithm To solve this problem, we will follow these steps − Create a list odd_indices to store indices ...

Read More

Program to find minimum absolute sum difference in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 517 Views

Suppose we have two positive valued arrays nums1 and nums2 of the same size. The absolute sum difference of these two arrays is the sum of |nums1[i] - nums2[i]| for each 0 [2, 2, 6], or Replace 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. Algorithm To solve this, we will follow these steps ? If nums1 is ...

Read More
Showing 341–350 of 3,768 articles
« Prev 1 33 34 35 36 37 377 Next »
Advertisements