# Tag Archives: modular-arithmetic

## 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 »

## Multiply large integers under large moduloMay 1, 2017

Given an integer a, b, m. Find (a * b ) mod m, where a, b may be large and their direct multiplication may cause… Read More »

## Modulo 10^9+7 (1000000007)April 29, 2017

In most of the programming competitions, problems are required to answer the result in 10^9+7 modulo. The reason behind this is to have problems for… Read More »

## Number of substrings divisible by 6 in a string of integersApril 13, 2017

Given a string consisting of integers 0 to 9. The task is to count the number of substrings which when convert into integer are divisible… Read More »

## Subset with no pair sum divisible by KMarch 8, 2017

Given an array of integer numbers, we need to find maximum size of a subset such that sum of each pair of this subset is… Read More »

## Rearrange an array in maximum minimum form | Set 2 (O(1) extra space)March 7, 2017

Given a sorted array of positive integers, rearrange the array alternately i.e first element should be the maximum value, second minimum value, third-second max, fourth-second… Read More »

## Maximum subarray sum modulo mMarch 2, 2017

Given an array of n elements and an integer m. The task is to find the maximum value of the sum of its subarray modulo… Read More »