My issue
i cannot get this problem
My code
#include <iostream>
using namespace std;
int main()
{
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
int n, l, r, h = 0, s = 0;
cin >> n >> l >> r;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
for (int i = 0; i < n; i++)
{
if (arr[i] >= l && r >= arr[i])
{
h++;
}
else s--;
}
cout << h << " " << s << endl;
}
return 0;
}
Problem Link: Moody Chef Practice Coding Problem - CodeChef
This problem was about to monitor current “satisfaction” of Chef, determinated by each number he was looking while explorting the array.
- He starts with 0
- If a number is within L and R, then satisfaction += 1
- If a number is NOT within L and R, then satisfaction -= 1
- We should be monitoring all the current satisfactions, and store the maximal, and the minimal
Basically that. Print the maximal and the minimal stored.
Imagine the sample test case:
4 1 3
4 3 2 1
L = 1, R = 3
You start with:
current_satisfaction = 0
maximal_satisfaction = 0
minimal_satistaction = 0
-
Is “4” within 1 and 3?
No
current_satistaction = -1
maximal_satisfaction = 0
minimal_satistaction = -1
-
Is “3” within 1 and 3?
Yes
current_satistaction = 0
maximal_satisfaction = 0
minimal_satistaction = -1
-
Is “2” within 1 and 3?
Yes
current_satistaction = 1
maximal_satisfaction = 1
minimal_satistaction = -1
-
Is “1” within 1 and 3?
Yes
current_satistaction = 2
maximal_satisfaction = 1
minimal_satistaction = -1
Then:
Maximal = 2
Minimal = -1
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void solve(){
ll N,L,R;
cin >> N >> L >> R;
vector<ll> A(N);
for(ll i=0; i<N; i++){
cin >> A[i];
}
ll current_satisfaction = 0;
ll max_satis = 0;
ll min_satis = 0;
for(ll i=0; i<N; i++){
if ((L <= A[i]) && (A[i] <= R)){
current_satisfaction ++;
}
else if ((A[i] <= L) || (R <= A[i])){
current_satisfaction --;
}
max_satis = max(max_satis, current_satisfaction);
min_satis = min(min_satis, current_satisfaction);
}
cout << max_satis << " " << min_satis << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin >> T;
while(T--){
solve();
}
}