So I was solving the Longest Bitonic Subarray problem on an online platform. Their IDE is giving me a TLE error. I have to print out the max length of a Bitonic Subarray in a given array. Another IDE on my local machine isn't giving me any errors. I have my code attached below. Kindly help and do point out any problems with my code wrt to the problem!
Problem Link: https://www.geeksforgeeks.org/longest-bitonic-subsequence-dp-15/
Error:
runguard: warning: timelimit exceeded (wall time): aborting command
runguard: warning: command terminated with signal 15
Code
// Program to find the max length of bitonic subaarray
#include<iostream>
using namespace std;
int main() {
// test cases
int t;
cin >> t;
int largest = 0;
while (t--) {
// input the array
int n;
int arr[n];
cin >> n;
for (int i = 0; i < n; i++)
cin >> arr[i];
// finding the max length of a bitonic subarray
int count = 1;
largest = 0;
int prevElement = arr[0];
int flag = -1;
int currElement = 0;
for (int i = 1; i < n; i++) {
currElement = arr[i];
if (currElement > prevElement) {
if (flag == 2) {
flag = 0;
largest = max(largest, count);
count = 1;
i--;
prevElement = arr[i];
continue;
}
flag = 1;
count++;
} else if (currElement < prevElement) {
if (flag == 1 || flag == 3) {
flag = 3;
count++;
prevElement = arr[i];
continue;
}
flag = 2;
count++;
} else {
continue;
}
prevElement = arr[i];
}
largest = max(largest, count);
cout << largest << endl;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire