So i was solving this question leetcode
link to LC
I gave a solution of O(m+n)
but that gave me time limit exceeded error.
So to check if my logic was correct or not i jumped to the local editor.
and run the code.
To my surprise it showed my a blank console.
and i stuck in a infinite loop?
#include<vector>
#include<iostream>
using namespace std;
class Solution {
public:
vector<int> result;
void Calc(vector<int>& BgArr, vector<int>& SmArr) {
bool streak = false;
cout<<"your inside the function";
cout<<"about to enter loop";
auto BgIt = BgArr.begin(), SmIt = SmArr.begin();
while (BgIt != BgArr.end() && SmIt != SmArr.end()) {
if (*BgIt == *SmIt && streak==false) {
result.push_back(*BgIt);
SmIt++;
BgIt++;
streak=true;
}else if(*BgIt != *SmIt){
if(streak==true){
cout<<"exited function inside loop";
return;
}
BgIt++;
}
}
cout<<"exited function outside loop";
return;
}
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
if (nums1.size() > nums2.size()) {
Calc(nums1, nums2);
} else {
Calc(nums2, nums1);
}
return result;
}
};
int main(){
vector<int> arr1={1,2,2,1} ;
vector <int> arr2={2,2};
Solution obj1;
vector<int> res=obj1.intersect(arr1,arr2);
for(auto i:res){
cout<<i;
}
return 0;
}