Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
Suppose we have a number n and another value k. We need to find the total number of pairs from the first N natural numbers whose sum is divisible by k. Given an array A with first N natural numbers [1, 2, 3, ..., n], we have to find pairs A[i] and A[j] where i < j and (A[i] + A[j]) % k == 0. Example If n = 10 and k = 4, the output will be 10 because there are 10 pairs whose sum is divisible by 4: [(1, 3), (1, 7), (2, 6), ... Read More
Finding the best rational approximation to π (pi) within given constraints is a classic problem in number theory. We need to find a fraction n/d where the denominator d falls between minimum and maximum values, and the fraction is as close to π as possible. Problem Statement Given two integers minimum and maximum, find a common fraction n/d such that: min ≤ d ≤ max |n/d - π| is minimized If multiple fractions satisfy this condition, return the one with the smallest denominator Algorithm Overview The solution uses the Farey sequence approach to ... Read More
Given a number n, we need to find the probability that any proper divisor of n would be an even perfect square number. A proper divisor is any positive divisor of n except n itself. So, if the input is like n = 36, then the output will be 1/8 because there are eight proper divisors of 36, these are {1, 2, 3, 4, 6, 9, 12, 18} and among them only one number (4) is both a perfect square and even. Algorithm To solve this, we will follow these steps − If n mod ... Read More
Given an array nums and a pair (x, y), we need to determine whether the value of find(x, y) is odd or even. The find() function is defined recursively: find(x, y) = 1 if x > y find(x, y) = nums[x] ^ find(x+1, y) otherwise Where ^ represents the XOR (exclusive OR) operation. Understanding the Problem Let's trace through an example with nums = [3, 2, 7] and (x, y) = (1, 2): find(1, 2) = nums[1] ^ find(2, 2) ... Read More
Given an array colors representing colors for vertices of a regular n-gon polygon, we need to find the number of special subsets that meet specific conditions. Each vertex is randomly colored with one of n different colors. Problem Conditions A special subset must satisfy these requirements: The subset size must be at least two When we remove vertices in the subset (and their adjacent edges), the remaining vertices form continuous paths None of these paths should contain two vertices of the same color If the answer is large, return the result modulo 10^9 + ... Read More
Suppose we have a grid with n rows and m columns. Amal and Bimal are playing a strategic game on this grid with specific movement rules. Game Rules The game follows these rules: Amal places a white lotus tile somewhere in the top row Bimal places a caterpillar tile somewhere in the bottom row Amal starts first, and they play alternately Amal can move to any of the 8 adjacent cells (including diagonals) Bimal's caterpillar can only move left, right, or stay in the same position Amal's goal is to catch Bimal using minimum moves Bimal's ... Read More
Suppose there is a dangerous virus that grows rapidly. The probability of virus cells growing by a factor x is 0.5, and the probability of growing by a factor y is also 0.5. Starting with a single virus cell, we need to calculate the expected number of virus cells after time t. If the result is large, we return it modulo 10^9+7. The expected growth factor at each time step is (x + y) / 2, since each factor has probability 0.5. Problem Analysis At each time step, the virus can grow by factor x or y ... Read More
Suppose we have four numbers a, b, c and d. We need to find the number of pairs (x, y) that satisfy the equation: x² + y² = (x*a) + (y*b) where x is in range [1, c] and y is in range [1, d]. For example, if a = 2, b = 3, c = 2, d = 4, then the output will be 1 because one valid pair is (1, 1). Mathematical Approach We can rearrange the equation x² + y² = x*a + y*b as a quadratic in y: y² - b*y + ... Read More
We need to find the last digit of a sequence S for a given value n. The sequence S is defined by a mathematical formula involving powers of 2. Understanding the Problem The sequence formula is complex, but we can simplify it to find only the last digit. For n = 2, we calculate specific terms and sum them to get 126, where the last digit is 6. Algorithm Steps To solve this problem efficiently ? Initialize total = 0 and temp = 1 While temp ≤ n, add (2^temp mod 10) to total ... Read More
Suppose we are generating a number triangle where each element in a row is the sum of three numbers above it ? 1 1 1 1 1 2 3 2 1 1 3 6 7 6 3 1 1 4 10 16 19 16 10 4 1 ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance