#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
usingnamespace std;
// It is NOT mandatory to use the provided template. You can handle the IO section differently.
int main() {
/* The code required to enter n,k, candies is provided*/
int N, K;
cin >> N >> K;
longlongint temp;
vector<longlongint> numbers;
numbers.resize(N);
for (int i=0; i<N; i++){
cin>>temp;
cout<<"Temp is "<<temp<<endl;
numbers.push_back(temp);
}
for(int i=0;i<N;i++){
cout<<numbers[i]<<endl;
}
cin.clear();
longlongint unfairness = 1000000;
cout << unfairness << "\n";
/** Write the solution code here. Compute the result, store in the variable unfairness --
and output it**/
sort(numbers.begin(),numbers.end());
for(int i=0;i<K;i++){
cout<<numbers[i]<<endl;
}
for(int i=0; i<=N-K;i++){
if((numbers[K+i-1]-numbers[i])<unfairness){
unfairness=numbers[K+i-1]-numbers[i];
cout << unfairness << "\n";
}
}
cout << unfairness << "\n";
return 0;
}
If I comment/remove that line the contents of array is fine. What is wrong with using resize? When and how to use it?
resize is changing logical size of container. If new size is less than current, some elements are deleted, if it is larger, some element are created and value-initialized. For integers it is 0.
So after line 17 you have vector without any elements. After line 18 you have vector with N zeroes. After push_back you have N zeroes and one number pushed.