# Minimum sum after subtracting multiples of k from the elements of the array

Given an integer and an integer array , the task is to find the minimum possible sum of all the elements of the array after they are reduced by subtracting a multiple of from each element (the result must be positive and every element of the array must be equal after this reduction). If the array cannot be reduced then print . Note that an element may or may not be reduced in the final state of the array.

Examples:

Input: arr[] = {2, 3, 4, 5}, K = 1
Output: 4
Subtract 1 form 2, arr[] = {1, 3, 4, 5}
Subtract 2 from 3, arr[] = {1, 1, 4, 5}
Subtract 3 from 4, arr[] = {1, 1, 1, 5}
Subtract 4 from 5 to make arr[] = {1, 1, 1, 1}, thus giving minimum possible sum as 4.

Input: arr[] = {5, 6, 7}, K = 2
Output: -1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: First, the array needs to be sorted as the problem can be solved using greedy approach.

• Sort the array, if arr < 0 then print -1 as every element needs to be ≥ 0.
• If K == 0 then no element can be reduced further. So in order to have an answer every element of the array must be equal. So the sum of elements is n * arr else print -1.
• Now for the rest of the elements, run a loop from 1 to n and check whether ((arr[i] – arr) % K) == 0 i.e. arr[i] can be reduced to arr.
• If above condition fails for any element, print -1.
• Else if k == 1 then the answer is n i.e. every element will get reduced to 1.
• Else the answer is n * (a % k).

Below is the implementation of the above approach:

## C++

 `// C++ program of the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// function to calculate minimum sum after transformation ` `int` `min_sum(``int` `n, ``int` `k, ``int` `a[]) ` `{ ` ` `  `    ``sort(a, a + n); ` ` `  `    ``if` `(a < 0)  ` `        ``return` `-1; ` `     `  ` `  `    ``// no element can be reduced further ` `    ``if` `(k == 0) { ` ` `  `        ``// if all the elements of the array ` `        ``// are identical ` `        ``if` `(a == a[n - 1]) ` `            ``return` `(n * a); ` `        ``else` `            ``return` `-1; ` `    ``} ` `    ``else` `{ ` `        ``int` `f = 0; ` `        ``for` `(``int` `i = 1; i < n; i++) { ` ` `  `            ``int` `p = a[i] - a; ` ` `  `            ``// check if a[i] can be reduced to a ` `            ``if` `(p % k == 0) ` `                ``continue``; ` `            ``else` `{ ` `                ``f = 1; ` `                ``break``; ` `            ``} ` `        ``} ` ` `  `        ``// one of the elements cannot be reduced ` `        ``// to be equal to the other elements ` `        ``if` `(f) ` `            ``return` `-1; ` `        ``else` `{ ` ` `  `            ``// if k = 1 then all elements can be reduced to 1 ` `            ``if` `(k == 1) ` `                ``return` `n; ` `            ``else` `                ``return` `(n * (a % k)); ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, 3, 4, 5 }; ` `    ``int` `K = 1; ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << min_sum(N, K, arr); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program of the above approach ` ` `  `import` `java.io.*; ` `import` `java.util.*; ` `class` `GFG { ` `     `  `// function to calculate minimum sum after transformation ` `static` `int` `min_sum(``int` `n, ``int` `k, ``int` `a[]) ` `{ ` ` `  `    ``Arrays.sort(a); ` ` `  `    ``if` `(a[``0``] < ``0``)  ` `        ``return` `-``1``; ` `     `  ` `  `    ``// no element can be reduced further ` `    ``if` `(k == ``0``) { ` ` `  `        ``// if all the elements of the array ` `        ``// are identical ` `        ``if` `(a[``0``] == a[n - ``1``]) ` `            ``return` `(n * a[``0``]); ` `        ``else` `            ``return` `-``1``; ` `    ``} ` `    ``else` `{ ` `        ``int` `f = ``0``; ` `        ``for` `(``int` `i = ``1``; i < n; i++) { ` ` `  `            ``int` `p = a[i] - a[``0``]; ` ` `  `            ``// check if a[i] can be reduced to a ` `            ``if` `(p % k == ``0``) ` `                ``continue``; ` `            ``else` `{ ` `                ``f = ``1``; ` `                ``break``; ` `            ``} ` `        ``} ` ` `  `        ``// one of the elements cannot be reduced ` `        ``// to be equal to the other elements ` `        ``if` `(f>``0``) ` `            ``return` `-``1``; ` `        ``else` `{ ` ` `  `            ``// if k = 1 then all elements can be reduced to 1 ` `            ``if` `(k == ``1``) ` `                ``return` `n; ` `            ``else` `                ``return` `(n * (a[``0``] % k)); ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` ` `  ` `  `    ``public` `static` `void` `main (String[] args) { ` `            ``int` `arr[] = { ``2``, ``3``, ``4``, ``5` `}; ` `    ``int` `K = ``1``; ` `    ``int` `N = arr.length; ` `    ``System.out.println(min_sum(N, K, arr)); ` `    ``} ` `} ` `// This code is contributed by shs.. `

## Python3

 `# Python 3 program of the above approach ` ` `  `# function to calculate minimum sum  ` `# after transformation ` `def` `min_sum(n, k, a): ` `    ``a.sort(reverse ``=` `False``) ` ` `  `    ``if` `(a[``0``] < ``0``): ` `        ``return` `-``1` `     `  `    ``# no element can be reduced further ` `    ``if` `(k ``=``=` `0``): ` `         `  `        ``# if all the elements of the ` `        ``# array are identical ` `        ``if` `(a[``0``] ``=``=` `a[n ``-` `1``]): ` `            ``return` `(n ``*` `a[``0``]) ` `        ``else``: ` `            ``return` `-``1` `     `  `    ``else``: ` `        ``f ``=` `0` `        ``for` `i ``in` `range``(``1``, n, ``1``): ` `            ``p ``=` `a[i] ``-` `a[``0``] ` ` `  `            ``# check if a[i] can be  ` `            ``# reduced to a ` `            ``if` `(p ``%` `k ``=``=` `0``): ` `                ``continue` `            ``else``: ` `                ``f ``=` `1` `                ``break` `         `  `        ``# one of the elements cannot be reduced ` `        ``# to be equal to the other elements ` `        ``if` `(f): ` `            ``return` `-``1` `        ``else``: ` `             `  `            ``# if k = 1 then all elements ` `            ``# can be reduced to 1 ` `            ``if` `(k ``=``=` `1``): ` `                ``return` `n ` `            ``else``: ` `                ``return` `(n ``*` `(a[``0``] ``%` `k)) ` `     `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``2``, ``3``, ``4``, ``5``] ` `    ``K ``=` `1` `    ``N ``=` `len``(arr) ` `    ``print``(min_sum(N, K, arr)) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# program of the above approach ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `// function to calculate minimum  ` `// sum after transformation ` `static` `int` `min_sum(``int` `n, ``int` `k, ``int``[] a) ` `{ ` ` `  `    ``Array.Sort(a); ` ` `  `    ``if` `(a < 0)  ` `        ``return` `-1; ` ` `  `    ``// no element can be reduced further ` `    ``if` `(k == 0) ` `    ``{ ` ` `  `        ``// if all the elements of the array ` `        ``// are identical ` `        ``if` `(a == a[n - 1]) ` `            ``return` `(n * a); ` `        ``else` `            ``return` `-1; ` `    ``} ` `    ``else`  `    ``{ ` `        ``int` `f = 0; ` `        ``for` `(``int` `i = 1; i < n; i++)  ` `        ``{ ` `            ``int` `p = a[i] - a; ` ` `  `            ``// check if a[i] can be  ` `            ``// reduced to a ` `            ``if` `(p % k == 0) ` `                ``continue``; ` `            ``else`  `            ``{ ` `                ``f = 1; ` `                ``break``; ` `            ``} ` `        ``} ` ` `  `        ``// one of the elements cannot be reduced ` `        ``// to be equal to the other elements ` `        ``if` `(f > 0) ` `            ``return` `-1; ` `        ``else` `        ``{ ` ` `  `            ``// if k = 1 then all elements can  ` `            ``// be reduced to 1 ` `            ``if` `(k == 1) ` `                ``return` `n; ` `            ``else` `                ``return` `(n * (a % k)); ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main () ` `{ ` `    ``int``[] arr = ``new` `int``[] { 2, 3, 4, 5 }; ` `    ``int` `K = 1; ` `    ``int` `N = arr.Length; ` `    ``Console.WriteLine(min_sum(N, K, arr)); ` `} ` `} ` ` `  `// This code is contributed by mits `

## PHP

 ` `

Output:

```4
```

Time Complexity : O(n Log n)

Further Optimizations :
Instead of sorting the array, we can find minimum element in O(n) time. We can check all elements are same or not also in O(n) time.

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.