Minimum sum after subtracting multiples of k from the elements of the array
Last Updated :
23 Aug, 2022
Given an integer K 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 K 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 -1
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
Approach: First, the array needs to be sorted as the problem can be solved using the greedy approach.
- Sort the array, if arr[0] < 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[0] else print -1.
- Now for the rest of the elements, run a loop from 1 to n and check whether ((arr[i] - arr[0]) % K) == 0 i.e. arr[i] can be reduced to arr[0].
- If the 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[0] % k).
Below is the implementation of the above approach:
C++
// C++ program of the above approach
#include <bits/stdc++.h>
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] < 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[0]
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
int main()
{
int arr[] = { 2, 3, 4, 5 };
int K = 1;
int N = sizeof(arr) / sizeof(arr[0]);
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[0]
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[0]
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] < 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[0]
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 ()
{
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
<?php
// PHP program of the above approach
// function to calculate minimum
// sum after transformation
function min_sum($n, $k, $a)
{
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
{
$f = 0;
for ($i = 1; $i <$n; $i++)
{
$p = $a[$i] - $a[0];
// check if a[i] can be reduced to a[0]
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
$arr = array(2, 3, 4, 5 );
$K = 1;
$N = count($arr);
echo min_sum($N, $K, $arr);
// This code is contributed by inder_verma
?>
JavaScript
<script>
// Javascript program of the above approach
// function to calculate minimum sum after transformation
function min_sum(n, k, a)
{
a.sort();
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 {
let f = 0;
for (let i = 1; i < n; i++) {
let p = a[i] - a[0];
// check if a[i] can be reduced to a[0]
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
let arr = [ 2, 3, 4, 5 ];
let K = 1;
let N = arr.length;
document.write(min_sum(N, K, arr));
</script>
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Further Optimizations:
Instead of sorting the array, we can find the minimum element in O(n) time. We can check whether all elements are the same or not also in O(n) time.
The steps involved in this approach are as follows:
- Find the minimum element in the array and store it in variable name val.
- Now if val < 0 then print -1 as every element needs to be ? 0 and if the minimum element is greater or equal to 0 then every element will be greater than 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 we check this by iterating a loop and if all are equal then the answer will be n * val else print -1.
- Now for the rest of the elements, iterate a loop and check whether ((arr[i] – val) % K) == 0 i.e. arr[i] can be reduced to val.
- If the 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 * (val % k).
Below is the code for the above approach:
C++
// C++ program of the above approach
#include <bits/stdc++.h>
using namespace std;
// function to calculate minimum sum after transformation
int min_sum(int n, int k, int a[])
{
// Finding minimum element in the array
int val=INT_MAX;
for(int i=0;i<n;i++)
{
val=min(a[i],val);
}
if (val < 0)
return -1;
// no element can be reduced further
if (k == 0) {
// check if all the elements of the array
// are identical or not
for(int i=0;i<n;i++)
{
if(val!=a[i])
return -1;
}
return (n * val);
}
else {
int f = 0;
for (int i = 0; i < n; i++) {
int p = a[i] - val;
// check if a[i] can be reduced to val
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 * (val % k));
}
}
}
// Driver code
int main()
{
int arr[] = { 2, 3, 4, 5 };
int K = 1;
int N = sizeof(arr) / sizeof(arr[0]);
cout << min_sum(N, K, arr);
return 0;
}
// This code is contributed by Pushpesh raj
Java
// Java program of above approach
import java.io.*;
import java.util.*;
class GFG{
static int min_sum(int n,int k,int[] a)
{
// Finding minimum element in the array
int val=Integer.MAX_VALUE;
for(int i=0;i<n;i++)
{
val=Math.min(a[i],val);
}
if (val < 0)
return -1;
// no element can be reduced further
if (k == 0) {
// check if all the elements of the array
// are identical or not
for(int i=0;i<n;i++)
{
if(val!=a[i])
return -1;
}
return (n * val);
}
else {
int f = 0;
for (int i = 0; i < n; i++) {
int p = a[i] - val;
// check if a[i] can be reduced to val
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 * (val % 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 aditya942003patil
Python3
# Python3 program of the above approach
# function to calculate minimum sum after transformation
def min_sum(n, k, a):
# Finding minimum element in the array
val = 10000000;
for i in range(n):
val = min(a[i], val);
if (val < 0):
return -1;
# no element can be reduced further
if (k == 0) :
# check if all the elements of the array
# are identical or not
for i in range(n):
if(val != a[i]):
return -1;
return (n * val);
else :
f = 0;
for i in range(n):
p = a[i] - val;
# check if a[i] can be reduced to val
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 * (val % k));
# Driver code
arr = [ 2, 3, 4, 5 ];
K = 1;
N = len(arr);
print(min_sum(N, K, arr));
# This code is contributed by phasing17
C#
// C# program of the above approach
using System;
class GFG{
static int min_sum(int n, int k, int[] a)
{
// Finding minimum element in the array
int val = Int32.MaxValue;
for(int i = 0; i < n; i++)
{
val = Math.Min(a[i],val);
}
if (val < 0)
return -1;
// no element can be reduced further
if (k == 0) {
// check if all the elements of the array
// are identical or not
for(int i = 0; i < n; i++)
{
if(val !=
a[i])
return -1;
}
return (n * val);
}
else {
int f = 0;
for (int i = 0; i < n; i++) {
int p = a[i] - val;
// check if a[i] can be reduced to val
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 * (val % k));
}
}
}
// Driver Code
public static void Main ()
{
int[] arr = { 2, 3, 4, 5 };
int K = 1;
int N = arr.Length;
Console.WriteLine(min_sum(N, K, arr));
}
}
// This code is contributed by Aman Kumar
JavaScript
// JavaScript program of the above approach
// function to calculate minimum sum after transformation
function min_sum(n, k, a)
{
// Finding minimum element in the array
let val = 10000000;
for(var i = 0; i < n; i++)
{
val = Math.min(a[i], val);
}
if (val < 0)
return -1;
// no element can be reduced further
if (k == 0) {
// check if all the elements of the array
// are identical or not
for(var i = 0; i < n; i++)
{
if(val != a[i])
return -1;
}
return (n * val);
}
else {
var f = 0;
for (var i = 0; i < n; i++) {
var p = a[i] - val;
// check if a[i] can be reduced to val
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 * (val % k));
}
}
}
// Driver code
let arr = [ 2, 3, 4, 5 ];
let K = 1;
let N = arr.length;
console.log(min_sum(N, K, arr));
// This code is contributed by phasing17
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Minimum sum of the elements of an array after subtracting smaller elements from larger
Given an array arr, the task is to find the minimum sum of the elements of the array after applying the following operation: For any pair from the array, if a[i] > a[j] then a[i] = a[i] - a[j]. Examples: Input: arr[] = {1, 2, 3} Output: 3 modified array will be {1, 1, 1} Input: a = {2, 4, 6} Outp
5 min read
Find minimum number K such that sum of array after multiplication by K exceed S
Given an array arr[] of N elements and an integer S, the task is to find the minimum number K such that the sum of the array elements does not exceed S after multiplying all the elements by K.Examples: Input: arr[] = { 1 }, S = 50 Output: 51 Explanation: The sum of array elements is 1. Now the multi
4 min read
Minimum removal of elements from end of an array required to obtain sum K
Given an integer K and an array A[] of size N, the task is to create a new array with sum K with minimum number of operations, where in each operation, an element can be removed either from the start or end of A[] and appended to the new array. If it is not possible to generate a new array with sum
15+ min read
Minimize the product of Array elements after K increments
Given an array of non-negative integers arr[] and an integer K, the task is to minimize the product of array elements by performing at K increment operations on the array elements. Examples: Input: arr[] = [0, 9], K = 5Output: 0Explanation: Since 0 is present the, minimum product that can be obtaine
9 min read
Find K such that repeated subtraction of K from Array elements make the Array equal
Given an array arr[] of size N, the task is to find the value of an integer K such that its repeated subtraction from array elements will make all array elements in minimum operations. Example: Input: arr[] = {5, 3, 3, 7}Output: 2Explanation: Minimum 2 operations must be performed:1st operation: sub
5 min read
Find final Array after subtracting maximum from all elements K times
Given an array arr[] of N integers and an integer K, the task is to do the below operations with this array K times. Operations are as follows: Find the maximum element (say M) from the array.Now replace every element with M-arri. where 1 ⤠i ⤠N. Examples: Input: N = 6, K = 3, arr[] = {5, 38, 4, 96
9 min read
Minimum operations to make GCD of array a multiple of k
Given an array and k, we need to find the minimum operations needed to make GCD of the array equal or multiple of k. Here an operation means either increment or decrements an array element by 1. Examples: Input : a = { 4, 5, 6 }, k = 5 Output : 2 Explanation : We can increase 4 by 1 so that it becom
8 min read
Maximize MEX by adding or subtracting K from Array elements
Given an arr[] of size N and an integer, K, the task is to find the maximum possible value of MEX by adding or subtracting K any number of times from the array elements. MEX is the minimum non-negative integer that is not present in the array Examples: Input: arr[]={1, 3, 4}, K = 2Output: 2Explanati
7 min read
Minimizing array sum by subtracting larger elements from smaller ones
Given an array of n elements perform, we need to minimize the array sum. We are allowed to perform the below operation any number of times. Choose any two elements from the array say A and B where A > B and then subtract B from A. Examples: Input : 1 arr[] = 1 Output : 1 There is no need to apply
5 min read
Minimum sum of values subtracted from array elements to make all array elements equal
Given an array arr[] consisting of N positive integers, the task is to find the sum of all the array elements required to be subtracted from each array element such that remaining array elements are all equal. Examples: Input: arr[] = {1, 2}Output: 1Explanation: Subtracting 1 from arr[1] modifies ar
4 min read