How to solve it?

2*x=a+k

For what minimum value of a , x<=a ?
k is constant (can be either even or odd).
x , a , k >= 0

my code:

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll t;
	cin>>t;
	while(t--)
	{
	    ll a,k;
	    
	    cin>>a>>k;
	    
	 
	    
	    if(a==0 and k%2==0){
	    cout<<k<<"\n";
	    continue;
	    }
	    
	    
	    if((a+k)%2==0 and (a+k)/2<=a)
        {
            
            cout<<a<<"\n";
            continue;
            
        }
	    
	    
	    ll s=a+k;
	    
	    if(s%2!=0)
	    s++ ,a++;
	    
	    else
	    s+=2,a+=2;
	    
	   
	    
	    while((s/2)>a)
	    {
	      if(s%2!=0)
	      s++ ,a++;  
	      
	      else
	      s+=2  ,a+=2;
	    }
	    
	    
	      
	    
	   cout<<a<<"\n"; 
	   
	   
	    
	}
	    
}

This is from Yesterday’s Codeforces Div 2?

yes , but my code uses while loop , i want to know any mathematical relation related to it . If so then tell me that topic.

Also want to know is that equation valid for
x,k,a < 0

Example:

a=5 ,k=8
2*x=a+k
2*x=5+8=13

if a is increased by 1 a=6
2*x=6+8=14

so x=7 i.e x>a ( 7>6) so not possible

again increase a by 2 a=8
2*x=8+8=16

so x=8 i.e x==a ( 8==8) so 8 is the minimum value of a

if(n<k)
{
cout<<k-n<<endl;
} else if(k==0)
{
if(n%2)
cout<<1<<endl;
else cout<<0<<endl;
} else if(n>k)
{
if((n-k)%2)
cout<<1<<endl;
else cout<<0<<endl;
} else {
cout<<0<<endl;
}
This Was Mine
You can check

so to find the value of a it needs 5 conditions:

when a<k
a=k

when a>=k and a%2==0 and k%2==0
a=a

when a>=k and a%2!=0 and k%2!=0
a=a

when a>k and a%2!=0 and k%2==0
a=a+1

when a>k and a%2==0 and k%2!=0
a=a+1

1 Like

Yes Basically , the formula should give us a integer type value , so we need to avoid odd number

1 Like