Given three integers N, K and R. The task is to calculate the sum of all those numbers from 1 to N which yields remainder R upon division by K.
Input: N = 20, K = 4, R = 3
3, 7, 11, 15 and 19 are the only numbers that give 3 as the remainder on division with 4.
3 + 7 + 11 + 15 + 19 = 55
Input: N = 15, K = 13, R = 2
- Initialize sum = 0 and take the modulo of each element from 1 to N with K.
- If the remainder is equal to R, then update sum = sum + i where i is the current number that gave R as the remainder on dividing by K.
- Print the value of sum in the end.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find sum of modulo K of first N natural number
- Expressing a fraction as a natural number under modulo 'm'
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Count pairs from two arrays whose modulo operation yields K
- Count pairs whose product modulo 10^9 + 7 is equal to 1
- Count natural numbers whose factorials are divisible by x but not y
- Difference between sum of the squares of first n natural numbers and square of sum
- Sum of sum of all subsets of a set formed by first N natural numbers
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Sum of sum-series of first N Natural numbers
- Sum of series formed by difference between product and sum of N natural numbers
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Modulo power for large numbers represented as strings
- LCM of N numbers modulo M
- Find the first natural number whose factorial is divisible by x
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Maximum subarray sum modulo m
- Divisibility by 3 where each digit is the sum of all prefix digits modulo 10
- Count subarrays having sum modulo K same as the length of the subarray
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.