Southbay Robotic and Coding Center
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Challenge 2

Go down

Challenge 2 Empty Challenge 2

Post by Admin Mon Aug 21, 2017 12:21 pm

Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the previous one exactly by 1 . He may need some additional statues to be able to accomplish that. Help him figure out the minimum number of additional statues needed.

Example

For statues = [6, 2, 3, 8] , the output should be
makeArrayConsecutive2(statues) = 3 .

Ratiorg needs statues of sizes 4 , 5 and 7 .

Input/Output
•[input] array.integer statues

An array of distinct non-negative integers.

Guaranteed constraints:
1 ≤ statues.length ≤ 10 ,
0 ≤ statues[i] ≤ 20 .


•[output] integer

The minimal number of statues that need to be added to existing statues such that it contains every integer size from an interval [L, R] (for some L, R ) and no other sizes.

Admin
Admin
Admin

Posts : 25
Join date : 2017-08-17
Location : Torrance CA

http://southbayrobot.com

Back to top Go down

Challenge 2 Empty Re: Challenge 2

Post by Admin Mon Aug 28, 2017 2:51 pm

int makeArrayConsecutive2(int[] statues) {

int max=0;
int min=20;
for (int i=0; i<statues.length; i++) {
if (statues[i]>max) {
max=statues[i];
}

if (statues[i]<min) {
min=statues[i];
}
}

int minItem=max-min-statues.length+1;

return minItem;

}
Admin
Admin
Admin

Posts : 25
Join date : 2017-08-17
Location : Torrance CA

http://southbayrobot.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum