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
Articles by Arnab Chakraborty
Page 33 of 377
Program to find closest room from queries in Python
Given an array of rooms where rooms[i] = [roomId_i, size_i] and an array of queries where queries[j] = [preferred_j, minSize_j], we need to find the closest room for each query based on specific criteria. For each query, we need to find a room that: Has size of at least minSize_j |roomId - preferred_j| is minimized In case of tie, choose the room with smallest id Return -1 if no such room exists Example If rooms = [[2, 2], [1, 2], [3, 2]] and queries = [[3, 1], [3, 3], [5, 2]], the output is ...
Read MoreProgram to find maximum building height in Python
Suppose we have a value n and another list of pairs called restrictions. We want to build n new buildings in a city with specific constraints. Buildings are placed in a line and labeled from 1 to n. Each restriction has the format restrictions[i] = (id_i, max_height_i), indicating that building id_i must have height less than or equal to max_height_i. The city has the following height restrictions − The height of each building must be 0 or positive values. First building height must be 0. ...
Read MoreProgram to find XOR sum of all pairs bitwise AND in Python
Suppose we have two arrays arr1 and arr2. The XOR sum of a list is the bitwise XOR of all its elements. If the list has only one element, then its XOR sum will be the element itself. Now, consider a list containing the result of arr1[i] AND arr2[j] (bitwise AND) for every index pair (i, j) where 0 ≤ i < length of arr1 and 0 ≤ j < length of arr2. We have to find the XOR sum of that list. So, if the input is like arr1 = [5, 3, 4] and arr2 = [2, 6], ...
Read MoreProgram to find minimum number of operations to make string sorted in Python
Suppose we have a string s. We have to perform the following operation on s until we get a sorted string − Select largest index i such that 1 ≤ i < length of s and s[i] < s[i - 1]. Select largest index j such that i ≤ j < length of s and s[k] < s[i - 1] for all the possible values of k in the range [i, j] inclusive. Exchange two characters at indices i - 1 and j. ...
Read MoreProgram 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 More