i_love_you_jahnavi's blog

By i_love_you_jahnavi, history, 4 hours ago, In English

Guys i have been meaning to ask that there are some codes that i went through for the question here, and the solution code is not running in my normal online compiler. What shall be the reason and how can i solve this problem to avoid this in future ?

»
3 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

What exactly happens? Can you maybe post your code?

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    My code ;

    #include <bits/stdc++.h>
    using namespace std ;
    
    void solve ()
    {
        int n , x;
        cin>>n>>x;
       
     vector <pair<int,int>> points ;
     
      for (int i = 0 ; i < n ; i++)
      {
          int a , b;
          cin>>a>>b;
          points.push_back(make_pair(a , b));
          
      }
      
      int mini = INT_MIN;
      int maxi = INT_MAX;
      
      
      for (auto &it : points)
      {
          int left = min (it . first , it.second);
          int right = max(it.first , it.second);
          mini = max (mini , left );
          maxi = min(maxi , right );
          
      }
    
        
    if (mini <= maxi)
    {
        if (x >= mini && x <= maxi)
        {
            cout <<0<<"\n";
            return;
            
        }
        else 
        {
            cout <<min(abs(x - mini), abs(x - maxi)) << "\n";  
            return ;
            
        }
    }
    
    else 
    {
        cout << -1<<"\n";
       
        
    }
      
       return ;
    
        
    }
    
    
    int main()
    {
        int t ;
        cin>>t;
        while (t--)
        {
            solve ();
            
        }
        return 0;
    } 
    

    Don't know why the online compiler isn't showing any output, and also there was a code that i came across and the same problem happened again, the online compiler wasn't showing any output for that code too.

    Here's the code of that person that i came across ;

    #include <bits/stdc++.h>
    using namespace std;
    int n,x,L,R=1000;
    int main(){
    cin>>n>>x;
    for(int a,b;cin>>a>>b;){
    if(a>b)swap(a,b);
    L=max(L,a),R=min(R,b);
    }
    cout<<(L>R?-1:L<=x&&R>=x?0:L>x?L-x:x-R);
    }
    

    I understood the code but couldn't understand why these codes show no output on the compiler ?

    • »
      »
      »
      2 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it
      Spoiler

      maybe consider not inputing the number of test cases

      • »
        »
        »
        »
        109 minutes ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        thanks man! both codes were accepted