# Tag Archives: modular-arithmetic

## Given two numbers a and b find all x such that a % x = bNovember 21, 2017

Given two numbers a and b find all x such that a % x = b . Examples: Input : a = 21, b =… Read More »

## Products of ranges in an arrayNovember 13, 2017

Given an array A[] of size N. Solve Q queries. Find the product in range [L, R] under modulo P ( P is Prime). Examples:… Read More »

## Exponential Squaring (Fast Modulo Multiplication)November 11, 2017

Given two numbers base and exp, we need to compute baseexp under Modulo 10^9+7 Examples: Input : base = 2, exp = 2 Output :… Read More »

## Subsequences of size three in an array whose sum is divisible by mNovember 3, 2017

Given an array A[] (1

## Program for product of arrayOctober 23, 2017

Given an array, find product of all array elements. Examples: Input : ar[] = {1, 2, 3, 4, 5} Output : 120 Product of array… Read More »

## Print k numbers where all pairs are divisible by mOctober 19, 2017

Given an integer array and two numbers k and m. Print k numbers from the array, such that difference between any two pairs is divisible… Read More »

## Distributing M items in a circle of size N starting from K-th positionAugust 23, 2017

M items are to be delivered in a circle of size M. Find the position where the M-th item will be delivered if we start… Read More »

## Print last k digits of a^b (a raised to power b)July 12, 2017

Given positive integers k, a and b we need to print last k digits of a^b ie.. pow(a, b). Input Constraint: k <= 9, a… Read More »

## Discrete logarithm (Find an integer k such that a^k is congruent modulo b)June 29, 2017

Given three integers a, b and m. Find an integer k such that where a and m are relatively prime. If it is not possible… Read More »

## Find all pairs (a, b) in an array such that a % b = kJune 25, 2017

Given an array with distinct elements, the task is to find the pairs in the array such that a % b = k, where k… Read More »

## Finding ‘k’ such that its modulus with each array element is sameJune 18, 2017

Given an array of n integers .We need to find all ‘k’ such that arr[0] % k = arr[1] % k = ……. = arr[n-1]… Read More »

## Maximum modulo of in all the pairs of array where arr[i] >= arr[j]June 16, 2017

Given an array of n integers. Find the maximum value of arr[i] mod arr[j] where arr[i] >= arr[j] and 1 <= i, j <= n… Read More »

## Fibonacci modulo pJune 11, 2017

The Fibonacci sequence is defined as = + where = 1 and = 1 are the seeds. For a given prime number p, consider a… Read More »

## Using Chinese Remainder Theorem to Combine Modular equationsMay 16, 2017

Given N modular equations: A ≅ x1mod(m1) . . A ≅ xnmod(mn) Find x in the equation A ≅ xmod(m1*m2*m3..*mn) where mi is prime, or… Read More »

## Compute nCr % p | Set 3 (Using Fermat Little Theorem)May 4, 2017

Given three numbers n, r and p, compute value of nCr mod p. Here p is a prime number greater than n. Here nCr is… Read More »