Twitter | Online | Get Minimum count to form a given Sum.
Anonymous User
1977

You are given an array Arr of size N and two integers S and K. Arr has a feature that its elements absolute value dose not exceed K. You want sum of all elements of Arr equal to given sum S.To Achive this you cann add any number of elements Without violating the property of "absolute value dose not exceed K.

Output:- number of elements needed to be added in Arr such that total sum will become K

Example 1 :-

S=10
k=3
Arr= 3 3 -1

Output:- 2 

Explanation:-
since adding 2 & 3 into current array will result into sum 10

Example 2:-

S=7
k=4
Arr= 3 3 3

Output:- 1 

Explanation:-
since adding -2 into current array will result into sum 7

My solution:-
Find the sum of each Arr and find Absolute diff between the ArrSum and Sum.then take ceil(diff/k) which will be the result.

Anyone has a better solution?

Comments (1)