Carvans TLE why? help

#include
#include<bits/stdc++.h>
using namespace std;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);

int t;

cin>>t;

while(t--){long int cn;
cin>>cn;
short int s[cn];
long  int temp=cn;
 long int temp2=cn;
 int x=0;

    // for(int j=1;j<=cn;j++){
    //     cin>>s[j];
    // }
    while(cn--){
        cin>>s[temp-cn];
    }
   
//   for(int i=2;i<=temp;i++){
//       if(s[i]<=s[i-1]){
//           x++;
//       }
//   }
while(temp--){if(temp==temp2-1){
    continue;
}
    if(s[temp2-temp]<=s[temp2-temp-1]){
        x++;
    }
}


    cout<<x+1<<endl;

}

return 0;

}

[simon@simon-laptop][11:45:38]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling rsiitr4-CARVAN.cpp
+ g++ -std=c++14 rsiitr4-CARVAN.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][11:45:42]
[~/devel/hackerrank/otherpeoples]>echo "3
1
10
3
8 3 6
5
4 5 1 2 3
" | ./a.out
1
rsiitr4-CARVAN.cpp:35:24: runtime error: index 3 out of bounds for type 'short int [*]'
2
2

This line:

if(s[temp2-temp]<=s[temp2-temp-1]){

gives an out-of-bounds access for the sample test input.

1 Like