My issue
Why is it displaying time limit exceeded ?
What is the error ?
My code
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
int t1s, t2s;
int A[10];
while(t>0){
for(int i = 0; i < 10; i++)
{
cin >> A[i];
}
for(int i = 2; i < 11; i+2){
t2s += A[i];
}
for(int i = 1; i < 10; i+2){
t1s += A[i];
}
if(t1s > t2s){
cout << 1;
} else if(t2s > t1s){
cout << 2;
} else{
cout << 0;
}
t--;
}
}
Learning course: Beginner DSA in C++
Problem Link: Practice Problem in - CodeChef
there are lot of flaws in ur code my frnd
main reason for TLE is the increament section of for loop which u did as “i+2” but it should be “i+=2”
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
int t1s, t2s; <------------------- should be initialised to 0
int A[10];
while(t>0){
for(int i = 0; i < 10; i++)
{
cin >> A[i];
}
for(int i = 2; i < 11; i+2){ <---------------------- i+=2
t2s += A[i];
}
for(int i = 1; i < 10; i+2){<------------------------i+=2
t1s += A[i];
}
if(t1s > t2s){
cout << 1;
} else if(t2s > t1s){
cout << 2;
} else{
cout << 0;
}
t--;
}
}
i didn’t point out a lot more
just try it on ur own and atlast see
my correct submission
Hope it helps Keep Grinding!!!
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t>0){
int A[10];
int t1s = 0, t2s = 0;
for(int i = 0; i < 10; i++)
{
cin >> A[i];
}
for(int i = 0; i < 10; i+=2){
t1s += A[i];
}
for(int i = 1; i < 10; i+=2){
t2s += A[i];
}
if(t1s > t2s){
cout <<1<<'\n';
} else if(t2s > t1s){
cout <<2<<'\n';
} else{
cout <<0<<'\n';
}
t--;
}
}
Thank you so much!
I have corrected my errors and the code ran successfully.
Actually, I have always used ‘i++’ in for loops so didn’t realize the error in ‘i+2’.
