Find upper and lower case

Please check my code.

#include
#include
using namespace std;

int main()
{

int n;
cin>>n;
int a[n];
for(int i=0; i<n; i++){
	cin>>a[i];
}
int q; cin>>q;
int keys[q];
for(int j=0; j<q; j++){
	cin>>keys[j];
}

bool present = binary_search(a,a+n,q);
    if(!present){
    return -1;
}
    

for(int i=0; i<q; i++){
	auto lb= lower_bound(a,a+n,keys[i]);
	auto ub = upper_bound(a,a+n,keys[i]);
	cout<<(lb-a)<<" "<<(ub-a);

	cout<<endl;
}

return 0;

}

hello @Devansh_29

this code should be inside second for loop.
and in place of return -1 , it should be cout<<-1<<"\n"; continue;
and in place of q,it should be key[i]

1 Like

Here, the upper boound is incremented by 1(maybe because i is incremented), how do I rectify it? I tried to introduce another variable j but it didn’t worked.

for(int i=0,j=0; i<q,j<q; i++,j++)
auto ub = upper_bound(a,a+n,keys[j]);

in place of ub-a ,
print ub-a-1 , becuase upper bound will always give index whose value will be greater than key.

1 Like

thank you so much :innocent: :pray: :pray: