Minimum Sum of Non-Negative Elements
Coding Problem Keys
Minimum Sum of Non-Negative Elements
Problem Statement
Given a non-negative integer array Arr having size N. Each element of the array will carry a different value. This means no two elements can have the same values. The candidate has to do this with minimal changes in the original value of elements, making every element as least as much value as it originally had.Find the minimum sum of all the elements that can be set the array for.
Input Format
First Input Line: Accept a single positive integer value for N representing the size of Arr[].
Second Input Line: Accept the N number of integer values separated by a new line, representing the original values assigned to each element.
Output Format
The output must be a non-negative integer only (See the output format in examples).Violation of Input Criteria: System should display a message as a "Wrong Input".
Additional messages in the output will result in the failure of the test cases.
Boundary Condition(s)
1 <= N <= 201 <= Arr[i] <= 100
Example Input/Output 1
Input
3 → Value of N, represents the size of Arr2 → Value of Arr[0]
2 → Value of Arr[1]
4 → Value of Arr[2]
Output
9
Explanation
As two elements have the same value, max value for one of them needs to be incremented to 3.
He can set the array with 2 + 3 + 4 = 9.
Example Input/Output 2
Input
2 → Value of N, represents the size of Arr3 → Value of Arr[0]
4 → Value of Arr[1]
5 → Value of Arr[2]
Output
Wrong Input
Explanation
Here N = 2, so we need to provide value of only two elements but we are providing value of three elements.
So result is "Wrong Input".
Instructions
The system does not allow any kind of hard-coded input value/values.
The written program code by the candidate will be verified against the inputs which are supplied from the system.
Solution
Programming Language: Python 3 Language
def minSum(n,l):
if(len(l)!=n):
return "Wrong Input"
res=[]
for i in l:
if i not in res:
res.append(i)
else:
i+=1
res.append(i)
return sum(res)
n=int(input())
l=[]
while True:
try:
x=input()
if(x==" "):
break
l.append(int(x))
except:
break
print(minSum(n,l))
# Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments