Given two positive integer n and k. n can be represented as the sum of 1s and 2s in many ways, using multiple numbers of terms. The task is to find the minimum number of terms of 1s and 2s use to make the sum n and also number of terms must be multiple of k. Print “-1”, if no such number of terms exists.
Input : n = 10, k = 2 Output : 6 10 can be represented as 2 + 2 + 2 + 2 + 1 + 1. Number of terms used are 6 which is multiple of 2. Input : n = 11, k = 4 Output : 8 10 can be represented as 2 + 2 + 2 + 1 + 1 + 1 + 1 + 1 Number of terms used are 8 which is multiple of 4.
Observe, the maximum number of terms used to represent n as the sum of 1s and 2s is n, when 1 are added n number of times. Also, the minimum number of terms will be n/2 times of 2s and n%2 times 1s are added. So, iterate from minimum number of terms to maximum number of terms and check if there is any multiple of k.
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Minimum number of changes required to make the given array an AP
- Minimum number of moves to make all elements equal
- Minimum number of operations on an array to make all elements 0
- Minimum increment/decrement to make array non-Increasing
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Number of flips to make binary string alternate | Set 1
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
- Find a number which give minimum sum when XOR with every number of array of integers
- Minimum number of cubes whose sum equals to given number N
- Minimum number of items to be delivered
- Minimum prime number operations to convert A to B
- Minimum number of stacks possible using boxes of given capacities
- Minimum number of integers required to fill the NxM grid
- Minimum removals in a number to be divisible by 10 power raised to K
- Minimum number of moves after which there exists a 3X3 coloured square