Can you find mistake in following code..?

My issue

can find mistake in this code…?

My code

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

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    int ans = 0;
	    int n,m;
	    cin>>n>>m;
	    vector<int>f(n);
	    vector<int>c(m);
	    bool ft = true;
	    for(int i=0;i<n;i++)cin>>f[i];
	    for(int i=0;i<m;i++)cin>>c[i];
	    int i = 0;
	    int j = 0;
	    while(i<n && j<n){
	        if(f[i]<c[j]){
	            i++;
	            if(ft)continue;
	            else{
	                ans++;
	                ft=true;
	            }
	        }
	        else{
	            j++;
	            if(!ft)continue;
	            else{
	                ft=false;
	                ans++;
	            }
	        }
	    }
	    if(i<n)if(!ft)ans++;
	    if(j<m)if(ft)ans++;
	  
	  cout<<ans<<endl;   
	}

}

Problem Link: College Life 5 Practice Coding Problem - CodeChef

@jaggadaku005
unable to find the test case for which your code is failing , but i have used much similar logic.
plzz refer my c++ code.
hope it will help

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

int main() {
	// your code goes here
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        int a[n],b[m];
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        for(int i=0;i<m;i++)
        {
            cin>>b[i];
        }
        int i=0,j=0;
        int ans=0,fnd=1;
        while(i<n&&j<m)
        {
            if(fnd)
            {
                if(a[i]>b[j])
                {
                    ans++;
                    j++;
                    fnd=0;
                }
                else
                i++;
            }
            else
            {
                if(b[j]>a[i])
                {
                    ans++;
                    i++;
                    fnd=1;
                }
                else
                j++;
            }
        }
        if(fnd&&j<m)
        ans++;
        if(!fnd&&i<n)
        ans++;
        cout<<ans<<endl;
    }
}
1 Like

Thank you actually i put n in both the condition in while loop…!

1 Like