dimanche 28 janvier 2018

Test Case Failure of vector with size 10

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.

This is a problem from codefights. Here is my code written below

#include <iostream>
#include <vector>
#include<algorithm>

using std::vector;

int makeArrayConsecutive2(std::vector <int> statues) {

    vector<int>::size_type size = statues.size();
    sort( statues.begin(), statues.end() );
    int counter = 0;
    for( int i = 0; i<size; i++ )
    {
        int dif =  statues[i+1] - statues[i] - 1; 
        if( dif >= 1 ) {counter+=dif;}
    }
    return counter;
}
int main()
{
  vector<int> c = {1,2,3,4,5,6,7,8,9,10};

  std :: cout<<"You need "<<makeArrayConsecutive2(c)<<" statues"<<std::endl;

  return 0;
}

When I run code with this certain value of vector c it outputs misunderstanding value.All other cases runs correct, but when I declare 10 dimensional vector(I mean vector with 10 values) it doesn't work correct .Could you please explain what's the problem?

Aucun commentaire:

Enregistrer un commentaire