# Help reduce time limit

Problem code: STACKS
I’m getting compile time error ,all test cases works fine .

``````#include<iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n,i,j,count=0,size;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){ //ARRARY1 INPUT
cin>>arr[i];
}
int arr2[n];
arr2[0]=arr[0];
bool check=false;
//LOGIC
for( i=0 ,j=1;i<j, j<n;i++, j++){
check=false;

if(arr[j]>arr2[i]){
count++;
arr2[count]=arr[j];
//cout<<arr2[count]<<"\n";

}else
{
for(int k=0;k<=count;k++){ //runs till count
if(arr[j] <arr2[k]){
arr2[k]=arr[j];

check=true;
i--;
break;
} if(check) break;
}

}
}
int c = count+1;
cout<<c<<"\t";
for(i=0;i<=count;i++){
cout<<arr2[i]<<" ";
}
cout<<endl;
}

``````

Do this ,
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
} at the start of the main() write this.

You can use binary search instead of linear search.
check below my Python Code

``````import bisect
for _ in range(int(input())):
N=int(input())
arr=list(map(int,input().split()))
a=[]
a.append(arr[0])
j=0
for i in range(1,len(arr)):
if(arr[i]>=a[j]):
a.append(arr[i])
j+=1
#print(a)
else:
idx=bisect.bisect_right(a,arr[i])
if(idx<=j):
a[idx]=arr[i]
print(len(a),*a)
``````

** bisect is predefine binary search algorithm in python .
** binary_search is also predefine function in cpp && use upper_bound or lower_bound in binary search in cpp

Thanks ,but can you explain how it supposed to work.
Also it’s doesn’t reduces time complexity ,still thanks for replying .