AT CODER problem

what is the difference between these two codes?

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll n;
cin>>n;
ll a;
ll count=0;
for(int i=0;i<n;i++)
{
cin>>a;
if(a>10)
{
count+=a-10;
}

  }
 cout<<count<<endl;

return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll n;
cin>>n;
ll a;
ll count=0;
for(int i=0;i<n;i++)
{
cin>>a;
if(a<=10)
{
count=0;
}
else if(a>10)
{
count+=a-10;
}

  }
 cout<<count<<endl;

return 0;
}

first one gives me a right answer while the second one gives a wrong answer.
Problem Link B - Nuts

You are making count=0 whenever you encounter a number less than or equal to 10 and that’s the problem here

if number is <10 count is resetting to 0

#include
#include
#include
#include<math.h>
#include
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif

int N;
cin >> N;
int arr[N];
int count=0,sum=0;

for (int i=0;i<N;i++)
	cin>>arr[i];

for(int i=0;i<N;i++)
{
	if(arr[i]>10)
	{
		count++;
		sum+=arr[i];
	}
}

cout<<sum-(10*count);

return 0;

}

here is my code it may be somewhat helpful

so what happens when the count resets to zero…can you please explain i am not getting it clearly…

so what happens when the count resets to zero…can you please explain i am not getting it clearly

i am saying with respect to your code

let the value of n be 7 and let the inputs be 15 20 25 15 15 6 4 and now for the first 5 inputs of a( i.e 15 20 25 15 as these are greater than 10 the else block is executing and you are adding the value of a-10 to count where count is intialized to zero outside the forloop but as the next input(i.e 6th input is 6 ) the if block is getting executed and the value of count is resetting to zero so you are losing the value of count whenever you are encountering with a number which is less than 10.

Hope you will understand now !!!
thank you !!!

thanks a lot

@strawhat_8502 @sabertooth_69 , use ``` to format your code.

1 Like