Programming Articles

Page 295 of 2547

Program to find maximum XOR for each query in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 349 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 282 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 899 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 525 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

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 412 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 450 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 720 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 501 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 464 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
Showing 2941–2950 of 25,466 articles
« Prev 1 293 294 295 296 297 2547 Next »
Advertisements