Loading Similar Posts
Where:
a
is the number of 2-wheeled vehicles
b
is the number of 4-wheeled vehicles
This can be simplified as: a + 2b = W / 2
Let X = W / 2
. We need to count all integer pairs (a, b)
such that: a = X - 2b ≥ 0
This leads to:0 ≤ b ≤ X / 2
Which gives us: floor(X / 2) + 1 distinct combinations.
If W
is odd, no valid solution exists.
Thus:
If W
is odd → 0 combinations
If W
is even → answer is (W / 4) + 1
(which is equivalent to floor((W / 2) / 2) + 1
)
#include<bits/stdc++.h>
using namespace std;
int main(){
int n ;
cin >> n;
vector<int> arr(n);
for(int i =0 ;i <n;i++){
cin >> arr[i];
}
vector<int> ans;
for(int i = 0;i<n;i++){
if (arr[i]%2 == 0){
int a = arr[i]/4;
ans.push_back(a+1);
}else{
ans.push_back(0);
}
}
for(int i = 0;i<n;i++){
cout << ans[i] << " ";
}
cout << endl;
}
O(n) — processing each wheel count once.
O(n) — storing results in an output array.