You stopped taking input as soon as you found Red Alert and that brings Red Alert for your submission.
Found the mistake. Thank you for your quick response!!!
whats the problem in this solution??
#include
using namespace std;
int main() {
// your code goes here
int t,n,d,h;
cin>>t;
while(t–){
int sum=0;
cin>>n>>d>>h;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}
for(int i=0; i<n; i++){
if(arr[i]!=0){
sum+=arr[i];
}
else{
sum-=d;
}
}
if(sum<=h)
cout<<“NO”<<endl;
else{
cout<<“YES”<<endl;
}
}
return 0;
}
- you are not checking the condition when sum<d and if sum<d then directly u need to make sum as zero
- the value of sum can be greater than H even before iterating the entire loop so even if on one day the value of SUM>H then red alert is possible so it is advisable to break the loop whenever u find sum>H and then print accordingly and for this create a flag variable and intialize it to zero and if Sum>H make the flag as 1 and break the loop and now outside for loop print yes or no accordingly by keeping a if condition by checking if your flag is 1 print yes else print NO
I hope this explanation helps you !!!
CAN SOMEONE POINT MY MISTAKE
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main() {long long t;cin>>t;while(t–){ll n,d,h,a,s=0;cin>>n>>d>>h;
for(ll i=0;i<n;i++){
cin>>a; s+=a;if(a==0) s-=d;if(s<0) s=0;
if(s>h){ cout<<“YES”<<"\n";break;}
} if(h>=s) cout<<“NO”<<"\n";
}
// your code goes here
return 0;
}
CAN ANYBODY POINT OUT MY MISTAKE ??
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
long long N,D,H;
cin>>N>>D>>H;
long long arr[N];
long long prefix[N];
for(int i=0;i<N;i++){
cin>>arr[i];
if(arr[i]==0){
arr[i]=-D;
}
}
prefix[0]=arr[0];
for(int i=1;i<=N;i++){
prefix[i]=prefix[i-1]+arr[i];
}
long long max=*max_element(prefix,prefix+N);
if(max>H){
cout<<"YES\n";
}
else{
cout<<"NO\n";
}
}
return 0;
}
Out of bounds access on sample test input:
[simon@simon-laptop][18:56:26]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling sudhanshu_290-REDALERT.cpp
+ g++ -std=c++14 sudhanshu_290-REDALERT.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv -fno-sanitize-recover
+ set +x
Successful
[simon@simon-laptop][18:56:30]
[~/devel/hackerrank/otherpeoples]>echo "4
4 2 6
1 3 0 2
2 1 100
1 100
4 2 3
1 2 0 2
3 7 10
5 3 9
" | ./a.out
sudhanshu_290-REDALERT.cpp:24:40: runtime error: index 4 out of bounds for type 'long long int [*]'
can you please find the error in my code ??
https://www.codechef.com/viewsolution/49327418
#include<bits/stdc++.h>
using namespace std;
int main() {
int t,d,h,n,w,flag;
scanf(“%d”,&t);
for(int i=0;i<t;i++){
scanf(“%d %d %d”,&n,&d,&h);
int a[n];
w=0;
flag=0;
for(int i=0;i<n;i++){
scanf("%d ",&a[i]);
if(a[i]>0)w=w+a[i];
if(a[i]==0) {
if(w>=d)w=w-d;
else if(w<d) w=0;
}
if(w>h){
flag++;
}
}
if(flag==0)printf("N0\n");
else printf("YES\n");
}
return 0;
}
You’re printing out N0
instead of NO
Array indexing for prefix array is wrong, use N+1
You should not break while taking the input if you find s>h… all inputs of test case are not taken till then
for every day , you have to check whether there is red alert or not . but you are checking at last which is wrong
Can someone say why this code is not working
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int redalert(int arr[],int n,int d,int h,int rain)
{
for(int i=0;i<n;i++){
if(arr[i] > 0){
rain = rain+arr[i];
}
if(arr[i] == 0){
if(rain < d){
rain=0;
}
else{
rain = rain-d;
}
}
if(rain > h){
return 1;
}
}
}
int main()
{
ll t;
cin>>t;
while(t--){
int n,d,h,rain=0;
cin>>n>>d>>h;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int flag = redalert(arr,n,d,h,rain);
if(flag == 1){
cout<<"YES"<<"\n";
}
else{
cout<<"NO"<<"\n";
}
}
}
Fix this compiler warning and ask again
[simon@simon-laptop][08:55:07]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling sarvesh_1361-REDALERT.cpp
+ g++ -std=c++14 sarvesh_1361-REDALERT.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv -fno-sanitize-recover
sarvesh_1361-REDALERT.cpp: In function ‘int redalert(int*, int, int, int, int)’:
sarvesh_1361-REDALERT.cpp:43:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
+ set +x
Successful
T=int(input())
for i in range(T):
r=list(map(int,input().split()))
a=list(map(int,input().split()))
s=0
Z=0
for j in range(len(a)):
if a[j]>0:
s=s+a[j]
if s>r[2]:
print(“YES”)
Z=Z+1
break
if a[j]==0:
if a[j-1]<r[1]:
s=0
else:
s=s-r[1]
if Z==0:
print(“NO”)
can you please find my mistake??
whats the problem with this code??
#include
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t!=0)
{
int n,d,h;
cin>>n>>d>>h;
int a[n];
int sum=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]>0)
{
sum+=a[i];
}
else
{
if(sum<d)
{
sum=0;
}
else
{
sum=sum-d;
}
}
if(sum>h)
{
cout<<“YES”<<endl;
break;
}
}
if(sum<=h)
{
cout<<“NO”<<endl;
}
t–;
}
return 0;
}
Consider the test input:
1
3 4 10
7 0 8