Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 36 of 377

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

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

In Python, finding the total similarities of a string with all its suffixes is a string processing problem that can be efficiently solved using the Z-algorithm. The similarity between two strings is defined as the length of the longest common prefix. For example, if we have the string "pqpqpp", its suffixes are "pqpqpp", "qpqpp", "pqpp", "qpp", "pp", and "p". The similarities with the original string are 6, 0, 3, 0, 1, and 1 respectively, giving a total sum of 11. Understanding the Z-Algorithm The Z-algorithm efficiently computes the longest common prefix between a string and each of ...

Read More

Program to count nice pairs in an array in Python

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

Suppose we have an array called nums with non-negative values. We have to find the number of 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: 0 ≤ i < j < size of nums nums[i] + rev(nums[j]) is same as nums[j] + rev(nums[i]) Note: Here rev() reverses only the positive part of an integer, so if rev(564) is there, it means 465, but if rev(540) is ...

Read More

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

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

Converting time from numerical format (hours and minutes) to text format is a common programming task. This involves translating numbers into words and applying English time conventions like "quarter past", "half past", and "to" for times after 30 minutes. Understanding the Time Format Rules The conversion follows these English time conventions − 8:00 : eight o'clock 8:01 : one minute past eight 8:10 : ten minutes past eight 8:15 : quarter past eight 8:30 : half past eight 8:40 : twenty minutes to nine 8:45 : quarter to nine 8:47 : thirteen minutes to nine ...

Read More

Program to check whether two sentences are similar or not in Python

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

Two sentences are considered similar when we can transform one sentence into another by adding words at the beginning, end, or both. This means one sentence can be a subsequence of another with additional words only at the edges. For example, if we have "we live at city Kolkata" and "city Kolkata", they are similar because we can add "we live at " to the beginning of the second sentence to get the first one. Algorithm To check if two sentences are similar, we follow these steps − Split both sentences into word lists Ensure ...

Read More

Program to find minimum remove required to make valid parentheses in Python

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

When working with parentheses validation, we often need to remove the minimum number of invalid parentheses to make a string valid. A string with parentheses is considered valid when every opening parenthesis has a corresponding closing parenthesis in the correct order. Problem Statement Given a string containing parentheses '(' and ')' along with lowercase English characters, we need to remove the minimum number of parentheses to make the string valid. A valid parentheses string satisfies these criteria: The string is empty or contains only lowercase characters, or The string can be written as AB (A concatenated ...

Read More

Program to find numbers with same consecutive differences in Python

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

Given a number N (length of digits) and K (absolute difference), we need to find all N-digit numbers where the absolute difference between every two consecutive digits equals K. Numbers cannot have leading zeros except for the single digit 0. For example, if N = 4 and K = 7, valid numbers include 1818 (|1-8|=7, |8-1|=7, |1-8|=7) and 2929, but 0707 is invalid due to the leading zero. Algorithm Approach We use a breadth-first search (BFS) approach with a queue ? If N = 1, return all single digits [0, 1, 2, ..., 9] Initialize ...

Read More

Program to find how many swaps needed to sort an array in Python

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

Finding the minimum number of swaps needed to sort an array is a classic problem. We need to consider both ascending and descending order, then return the minimum swaps required. So, if the input is like nums = [2, 5, 6, 3, 4], then the output will be 2 because initially nums has [2, 5, 6, 3, 4]. If we swap numbers 6 and 4, the array will be [2, 5, 4, 3, 6]. Then, if we swap the numbers 5 and 3, the array will be [2, 3, 4, 5, 6]. So 2 swaps are needed to make ...

Read More

Program to find maximum value at a given index in a bounded array in Python

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

Suppose we have three values: n, index and maxSum. We need to construct an array nums and find the maximum possible value at nums[index] that satisfies the following conditions: Size of nums is n All elements in nums are positive |nums[i] - nums[i+1]|

Read More

Program to find partition array into disjoint intervals in Python

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

Given an array nums, we need to partition it into two disjoint subarrays called left and right such that: Each element in left subarray is less than or equal to each element in right subarray Both left and right subarrays are non-empty Left subarray has the smallest possible size We have to find the length of left after such a partitioning. So, if the input is like nums = [5, 0, 3, 8, 6], then the output will be 3 because left array will be [5, 0, 3] and right subarray will be [8, 6]. ...

Read More

Program to find maximum number of consecutive values you can make in Python

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

Suppose we have an array called coins with n elements, representing the coins that we own. The value of the ith coin is denoted as coins[i]. We can make some value x if we can select some of our n coins such that their values sum up to x. We have to find the maximum number of consecutive values that we can get with the coins starting from and including 0. So, if the input is like coins = [1, 1, 3, 4], then the output will be 10, because ? 0 = [] (empty selection) 1 ...

Read More
Showing 351–360 of 3,768 articles
« Prev 1 34 35 36 37 38 377 Next »
Advertisements