why it is showing TLE
Arrays-intersection of two arrays
instead of this code:
for(int i=0;i<n;i++){
if(nums1[i]==nums2[i]){
result.push_back(nums1[i]);
}
}
use the below modified loop :
int i=0,j=0;
while(i<n and j<n){
if(num1[i]==num2[j]){
result.push_back(num1[i]);
i++;j++;
}else if(num1[i]<num2[j]){
i++;
}else{
j++;
}
}
Reference Code:
Okay i got it thanks but why complexity is getting increased in mine code
Your code checks only of one case - (when first element of both array is same):
for(int i=0;i<n;i++){
if(nums1[i]==nums2[i]){
result.push_back(nums1[i]);
}
}
as in this loop you are not checking for the case when elements of arrays are not equal…