Python Articles

Page 329 of 855

Program to count pairs with XOR in a range in Python

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

Suppose we have an array nums and two values l and r. We need to find the number of nice pairs, where a nice pair is (i, j) such that 0 >= 1 return result // 2 # Divide by 2 to avoid double counting return count_pairs_less_than(nums, r + 1) - count_pairs_less_than(nums, l) # Example usage nums = [4, 1, 7, 2] l = 2 r = 6 result = count_pairs_with_xor_in_range(nums, l, ...

Read More

Program to find maximize score after n operations in Python

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

We have an array nums of size 2*n and need to perform n operations. In each operation i (1-indexed), we select two elements x and y, get a score of i * gcd(x, y), and remove both elements. The goal is to maximize the total score. Algorithm Approach We use dynamic programming with bitmasks to solve this problem. The key insight is that we need to try all possible pairs at each step and choose the combination that gives maximum score ? Use a bitmask to represent which elements are already used For each state, try ...

Read More

Program to find out the maximum value of a 'valid' array in Python

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

Suppose we have an array of n integers 'nums'. Each value in 'nums' represents its 'power'. The array will be evaluated as 'valid' if the length of the array is greater than two and the first and last values of the array are equal. We have to make the array valid by deleting elements from the array so that the remaining elements satisfy the condition. As output, we return the maximum possible power value by adding all the power values of the valid array. So, if the input is like nums = [3, 4, 5, 3, 4], then the ...

Read More

Program to find maximum score of a good subarray in Python

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

Suppose we have an array called nums and a value k. The score of a subarray (i, j) is defined as the minimum value in subarray nums[i..j] multiplied by its length (j-i+1). A good subarray is one where i = minNum while i > -1 and nums[i] >= minNum: i -= 1 # Expand right while elements are >= minNum ...

Read More

Program to make the XOR of all segments equal to zero in Python

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

Suppose we have an array called nums and another value k. The XOR of a segment [left, right] (left 0: for j, prev in enumerate(dp): new_dp[i ^ j] = max(new_dp[i ^ j], prev + cnt) dp = new_dp ...

Read More

Program to find max chunks to make array sorted in Python

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

Suppose we have an array nums, we have to split the array into some number of partitions, and individually sort each of them. After concatenating them we will get one sorted array. We have to find the maximum number of partitions we could have made? So, if the input is like [3, 2, 4, 5, 5], then the output will be 4, as we can make partitions like [3, 2], [4], [5], [5]. Algorithm To solve this, we will follow these steps − real := sort the list nums p1 := 0, p2 := 1, ...

Read More

Program to find out if a vertex in an undirected graph has a lesser cost path in Python

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

Suppose, we are given a weighted, undirected graph. We have to implement a function query that takes two vertices and a cost 'limit' as input and checks if there exists a path with cost less than or equal to the given limit. We return true if such a path exists, otherwise we return false. So, if the input is like ? and the queries are (0, 2, 10), (3, 1, 30), (4, 3, 30). then the output will be ? False True True The result of the first query is False ...

Read More

Program to find maximize palindrome length from subsequences in Python

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

Given two strings s and t, we need to find the maximum length palindrome that can be formed by concatenating a subsequence from s with a subsequence from t. A subsequence preserves the relative order of characters but doesn't need to be contiguous. Problem Approach The key insight is to use dynamic programming to find the longest palindromic subsequence in the combined string, then check all possible ways to form palindromes using characters from both strings ? Concatenate both strings to form a combined string Use DP to find longest palindromic subsequence for any substring Check ...

Read More

Program to check whether one point can be converted to another or not in Python

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

Suppose we have a starting point (sx, sy) and a target point (tx, ty). We need to check whether a sequence of moves exists to transform the start point to the end point. Each move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y). This problem can be solved using a recursive approach by working backwards from the target point to check if we can reach the starting point. Algorithm Steps To solve this problem, we follow these steps ? If sx > tx or sy > ...

Read More

Program to find out the sum of evenly spaced elements in an array in Python

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

Suppose, there is an array nums of size n containing positive integers. We have another array queries that contain integer pairs (pi, qi). For every query in the array queries, the answer will be the sum of numbers in the array nums[j] where pi ≤ j < n and (j - pi) is divisible by qi. We have to return the answer of all such queries, and if it is a large value, we return the answer modulo 10^9 + 7. So, if the input is like nums = [2, 3, 4, 5, 6, 7, 8, 9, 10], queries ...

Read More
Showing 3281–3290 of 8,546 articles
« Prev 1 327 328 329 330 331 855 Next »
Advertisements