Given three integer **K**, **L** and **R** (range **[L, R]**), the task is to find the minimum number of elements the range must be extended by so that the count of elements in the range is divisible by **K**.

**Examples:**

Input:K = 3, L = 10, R = 10

Output:2

Count of elements in L to R is 1.

So to make it divisible by 3 , increment it by 2.

Input:K = 5, L = 9, R = 12

Output:1

**Approach:**

- Count the total number of elements in the range and store it in a variable named
**count = R – L + 1**. - Now, minimum number of elements that need to be added to the range will be
**K – (count % K).**

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `minimumMoves(` `int` `k, ` `int` `l, ` `int` `r) ` `{ ` ` ` `// Total elements in the range ` ` ` `int` `count = r - l + 1; ` ` ` ` ` `// If total elements are already divisible by k ` ` ` `if` `(count % k == 0) ` ` ` `return` `0; ` ` ` ` ` `// Value that must be added to count ` ` ` `// in order to make it divisible by k ` ` ` `return` `(k - (count % k)); ` `} ` ` ` `// Driver Program to test above function ` `int` `main() ` `{ ` ` ` `int` `k = 3, l = 10, r = 10; ` ` ` `cout << minimumMoves(k, l, r); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `minimumMoves(` `int` `k, ` `int` `l, ` `int` `r) ` `{ ` ` ` `// Total elements in the range ` ` ` `int` `count = r - l + ` `1` `; ` ` ` ` ` `// If total elements are already divisible by k ` ` ` `if` `(count % k == ` `0` `) ` ` ` `return` `0` `; ` ` ` ` ` `// Value that must be added to count ` ` ` `// in order to make it divisible by k ` ` ` `return` `(k - (count % k)); ` `} ` ` ` `// Driver Program to test above function ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `k = ` `3` `, l = ` `10` `, r = ` `10` `; ` ` ` `System.out.print(minimumMoves(k, l, r)); ` ` ` `} ` `} ` `// This code is contributed ` `// by inder_verma.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `def` `minimumMoves(k, l, r): ` ` ` `# Total elements in the range ` ` ` `count ` `=` `r ` `-` `l ` `+` `1` ` ` ` ` `# If total elements are already divisible by k ` ` ` `if` `(count ` `%` `k ` `=` `=` `0` `): ` ` ` `return` `0` ` ` ` ` `# Value that must be added to count ` ` ` `# in order to make it divisible by k ` ` ` `return` `(k ` `-` `(count ` `%` `k)) ` ` ` `# Driver Program to test above function ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `k ` `=` `3` ` ` `l ` `=` `10` ` ` `r ` `=` `10` ` ` `print` `(minimumMoves(k, l, r)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` ` ` `static` `int` `minimumMoves(` `int` `k, ` `int` `l, ` `int` `r) ` `{ ` ` ` `// Total elements in the range ` ` ` `int` `count = r - l + 1; ` ` ` ` ` `// If total elements are already divisible by k ` ` ` `if` `(count % k == 0) ` ` ` `return` `0; ` ` ` ` ` `// Value that must be added to count ` ` ` `// in order to make it divisible by k ` ` ` `return` `(k - (count % k)); ` `} ` ` ` `// Driver Program to test above function ` ` ` `public` `static` `void` `Main () { ` ` ` `int` `k = 3, l = 10, r = 10; ` ` ` `Console.WriteLine(minimumMoves(k, l, r)); ` ` ` `} ` `} ` `// This code is contributed ` `// by inder_verma.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `function` `minimumMoves(` `$k` `, ` `$l` `, ` `$r` `) ` `{ ` ` ` `// Total elements in the range ` ` ` `$count` `= ` `$r` `- ` `$l` `+ 1; ` ` ` ` ` `// If total elements are already divisible by k ` ` ` `if` `(` `$count` `% ` `$k` `== 0) ` ` ` `return` `0; ` ` ` ` ` `// Value that must be added to count ` ` ` `// in order to make it divisible by k ` ` ` `return` `(` `$k` `- (` `$count` `% ` `$k` `)); ` `} ` ` ` `// Driver Program to test above function ` ` ` ` ` `$k` `= 3; ` `$l` `= 10; ` `$r` `= 10; ` ` ` `echo` `minimumMoves(` `$k` `, ` `$l` `, ` `$r` `); ` `// This code is contributed ` `// by inder_verma.. ` ` ` ` ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Count numbers in a range that are divisible by all array elements
- Count of elements not divisible by any other elements of Array
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Minimum steps to make all the elements of the array divisible by 4
- Minimum operations required to make all Array elements divisible by K
- Count of elements on the left which are divisible by current element
- Count of pairs of Array elements which are divisible by K when concatenated
- Maximum count of elements divisible on the left for any element
- Count of elements on the left which are divisible by current element | Set 2
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of elements which are power of 2 in a given range subarray for Q queries
- Count elements in the given range which have maximum number of divisors
- Queries for count of even digit sum elements in given range using MO's Algorithm
- Count of elements having odd number of divisors in index range [L, R] for Q queries
- Queries for the count of even digit sum elements in the given range using Segment Tree.
- Find set of m-elements with difference of any two elements is divisible by k
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Maximum difference elements that can added to a set
- Minimum number of elements to be removed so that pairwise consecutive elements are same

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.