My issue
if i write the code like this i fail in test case
if(x<0 || x%k != 0) cout << -1<< endl;
else cout << x/k<<endl;
if i write it like this, test case gets passed why so?
if(x>0 && x%k == 0) cout << x/k<< endl;
else cout << -1<<endl;
My code
#include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
while(t--){
int n,k,v;
cin>> n>>k>>v;
int arr[n];
int sum_n = 0;
for(int i = 0;i<n;i++){
cin >> arr[i];
sum_n += arr[i];
}
int a = v*(n+k);
int x = a - sum_n;
if(x<0 || x%k != 0) cout << -1<< endl;
else cout << x/k<<endl;
}
return 0;
}
Problem Link: Average Number Practice Coding Problem - CodeChef