Although this solution is based on Java, the thought process can be applied everywhere.
Your decision is almost correct and optimized. Using multiple for loops will slow down the LOT, and should be avoided whenever possible! Since your array is already pre-sorted, a 1 for loop is enough for you.
Your assumption that you made a mistake in the last test case seems to be wrong, since an increment means that you can only do +1 (and indeed, most questions limit this assignment to only increments).
What you missed was the maximum range of integers.
If they pass Integer.MAX_VALUE, your amount will be overflowed and will be negative. Therefore, your sum variable should be of a larger type. double or BigInteger should work (BigInteger would be better).
Also, when they pass MAX_VALUE twice, your +1 course will also be full and become negative. So you want your curr and prev to be of a larger type too. it takes a long time to do this.
public static double calculateMinSumSorted(int[] input){ double sum = input[0]; long prev = input[0]; long cur; for(int i = 1 ; i < input.length ; i++){ cur = input[i]; if(cur <= prev){ cur = ++prev; } prev = cur; sum += cur; } return sum; }
Here are some of the test cases I used:
@Test public void testSimpleArray(){ double test1 = muas.calculateMinSumSorted(new int[]{1,2,3,4}); Assert.assertEquals(10, test1, 0.1); } @Test public void testBeginningSameValues(){ double test1 = muas.calculateMinSumSorted(new int[]{2,2,3,4}); Assert.assertEquals(14, test1, 0.1); } @Test public void testEndingSameValues(){ double test1 = muas.calculateMinSumSorted(new int[]{1,2,4,4}); Assert.assertEquals(12, test1, 0.1); } @Test public void testAllSameValues(){ double test1 = muas.calculateMinSumSorted(new int[]{1,1,1,1}); Assert.assertEquals(10, test1, 0.1); } @Test public void testOverMaxIntResult(){ double test1 = muas.calculateMinSumSorted(new int[]{1,2,3,3,4,4,4,4,4,Integer.MAX_VALUE}); System.out.println(test1); Assert.assertEquals(2147483692.0, test1, 0.1); } @Test public void testDoubleMaxIntArray(){ double test1 = muas.calculateMinSumSorted(new int[]{2,2,3,4,5,6,7,8,9, Integer.MAX_VALUE, Integer.MAX_VALUE}); Assert.assertEquals(4294967349.0, test1, 0.1); } @Test public void testDoubleMinIntArray(){ double test1 = muas.calculateMinSumSorted(new int[]{Integer.MIN_VALUE, Integer.MIN_VALUE,2,2,3,4,5,6,7,8,9}); Assert.assertEquals(-4294967241.0, test1, 0.1); }