Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 289 of 2109
Program to find number of different subsequences GCDs in Python
Suppose we have an array nums with positive values. We have to find the number of different GCDs among all non-empty subsequences of nums. As we know, the GCD of a sequence of numbers is the greatest value that divides all the numbers in the sequence evenly. So, if the input is like nums = [4, 6, 18], then the output will be 4 because: gcd([4]) = 4 gcd([6]) = 6 gcd([18]) = 18 gcd([4, 6]) = 2 gcd([4, 18]) = 2 gcd([6, 18]) = 6 gcd([4, 6, 18]) = 2 So all different GCD ...
Read MoreProgram to find maximum number of groups getting fresh donuts in Python
Suppose we have a value batchSize and an array groups where groups[i] denotes that there is a group of groups[i] customers that will visit the shop. So there is a donuts shop that bakes donuts in batches of given batchSize. But they have one rule: they must serve all of the donuts of a batch before serving any donuts of the next batch. Each customer will get exactly one donut. When a group enters the shop, all customers of that group must be served before addressing any next groups. One group may be happy if they all get fresh ...
Read MoreProgram to maximize number of nice divisors in Python
Given a number pf representing the number of prime factors, we need to construct a positive number n that maximizes the number of nice divisors. A divisor is considered "nice" when it is divisible by every prime factor of n. Problem Requirements The number of prime factors of n (may or may not be distinct) is at most pf The number of nice divisors of n is maximized Return the count of nice divisors modulo 109 + 7 For example, if pf = 5, we can construct n = 200 with prime factors [2, 2, ...
Read MoreProgram to count pairs with XOR in a range in Python
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 MoreProgram to find maximize score after n operations in Python
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 MoreProgram to find out the maximum value of a 'valid' array in Python
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 MoreProgram to find maximum score of a good subarray in Python
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 MoreProgram to make the XOR of all segments equal to zero in Python
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 MoreProgram to find max chunks to make array sorted in Python
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 MoreProgram to find out if a vertex in an undirected graph has a lesser cost path in Python
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