Assign other value to a variable from two possible values
Suppose a variable x can have only two possible values a and b, and you wish to assign to x the value other than its current one. Do it efficiently without using any conditional operator.
Note: We are not allowed to check current value of x.
Examples:
Input : a = 10, b = 15, x = a
Output : x = 15
Explanation x = 10, currently x has value of a (which is 10), we need to change it to 15.
Input : a = 9, b = 11, x = b
Output : x = 9
We could solve this problem using if condition, but we are not allowed to do that.
if (x == a)
x = b;
else x = a;
We could have used Ternary operator, it also checks the current value of x and based on that it assigns new value. So we cannot use this approach as well
x = x == a ? b : a;
But, we are not allowed to check value of x, so none of the above solutions work.
Solution 1: Using arithmetic operators only we can perform this operation
x = a + b - x
This way the content of x will alternate between a and b every time it gets executed
C++
#include <bits/stdc++.h>
using namespace std;
void alternate( int & a, int & b, int & x)
{
x = a + b - x;
}
int main()
{
int a = -10;
int b = 15;
int x = a;
cout << "x is : " << x;
alternate(a, b, x);
cout << "\nAfter change " ;
cout << "\nx is : " << x;
}
|
Java
import java.util.*;
class solution
{
static void alternate( int a, int b, int x)
{
x = a + b - x;
System.out.println( "After change" + "\n" + " x is : " +x);
}
public static void main(String args[])
{
int a = - 10 ;
int b = 15 ;
int x = a;
System.out.println( "x is : " +x);
alternate(a, b, x);
}
}
|
Python3
def alternate(a,b,x):
x = a + b - x
print ( "After change x is:" ,x)
if __name__ = = '__main__' :
a = - 10
b = 15
x = a
print ( "x is:" ,x)
alternate(a,b,x)
|
C#
using System;
class gfg
{
public void alternate( ref int a, ref int b, ref int x)
{
x = a + b - x;
}
}
class geek
{
public static int Main()
{
gfg g = new gfg();
int a = -10;
int b = 15;
int x = a;
Console.WriteLine( "x is : {0}" , x);
g.alternate( ref a, ref b, ref x);
Console.WriteLine ( "After change " );
Console.WriteLine( "x is : {0}" , x);
return 0;
}
}
|
PHP
<?php
function alternate (& $a , & $b , & $x )
{
$x = $a + $b - $x ;
}
$a = -10;
$b = 15;
$x = $a ;
echo "x is : " , $x ;
alternate( $a , $b , $x );
echo "\nAfter change " ;
echo "\nx is : " , $x ;
?>
|
Javascript
<script>
function alternate(a , b , x) {
x = a + b - x;
document.write( "After change" + "<br/>" + " x is : " + x);
}
var a = -10;
var b = 15;
var x = a;
document.write( "x is : " + x+ "<br/>" );
alternate(a, b, x);
</script>
|
Output:
x is : -10
After change
x is : 15
Time Complexity: The time complexity of this approach is O(1)
Space Complexity: The space complexity of this approach is O(1)
Solution 2: A better and efficient approach is using the bitwise XOR operation.
x = a^b^x
C++
#include <bits/stdc++.h>
using namespace std;
void alternate( int & a, int & b, int & x)
{
x = a ^ b ^ x;
}
int main()
{
int a = -10;
int b = 15;
int x = a;
cout << "x is : " << x;
alternate(a, b, x);
cout << "\nAfter exchange " ;
cout << "\nx is : " << x;
return 0;
}
|
Java
class GFG {
static int alternate( int a, int b, int x) {
return x = a ^ b ^ x;
}
public static void main(String[] args) {
int a = - 10 ;
int b = 15 ;
int x = a;
System.out.print( "x is : " + x);
x = alternate(a, b, x);
System.out.print( "\nAfter exchange " );
System.out.print( "\nx is : " + x);
}
}
|
Python3
def alternate(a, b, x):
x = a ^ b ^ x
print ( "After exchange" )
print ( "x is" , x)
a = - 10
b = 15
x = a
print ( "x is" , x)
alternate(a, b, x)
|
C#
using System;
public class GFG {
static int alternate( int a, int b, int x) {
return x = a ^ b ^ x;
}
public static void Main() {
int a = -10;
int b = 15;
int x = a;
Console.Write( "x is : " + x);
x = alternate(a, b, x);
Console.Write( "\nAfter exchange " );
Console.Write( "\nx is : " + x);
}
}
|
PHP
<?php
function alternate(& $a , & $b , & $x )
{
$x = $a ^ $b ^ $x ;
}
$a = -10;
$b = 15;
$x = $a ;
echo "x is : " , $x ;
alternate( $a , $b , $x );
echo "\nAfter exchange " ;
echo "\nx is : " , $x ;
?>
|
Javascript
<script>
function alternate(a , b , x) {
return x = a ^ b ^ x;
}
var a = -10;
var b = 15;
var x = a;
document.write( "x is : " + x);
x = alternate(a, b, x);
document.write( "<br/>After exchange " );
document.write( "<br/>x is : " + x);
</script>
|
Output:
x is : -10
After exchange
x is : 15
Time Complexity: The time complexity of this approach is O(1)
Space Complexity: The space complexity of this approach is O(1)
Solution 3:
Using the multiplication operator, we can perform the operation:
x = a * b / x
This way the content of x will alternate between a and b.
This approach has only one step:
Step 1: x = a * b / x
C++
#include <bits/stdc++.h>
using namespace std;
void alternate( int & a, int & b, int & x)
{
x = a * b / x;
}
int main()
{
int a = -10;
int b = 15;
int x = a;
cout << "x is : " << x;
alternate(a, b, x);
cout << "\nAfter change " ;
cout << "\nx is : " << x;
}
|
Java
import java.util.*;
public class Main {
public static int alternate( int a, int b, int x) {
x = a * b / x;
return x;
}
public static void main(String[] args) {
int a = - 10 ;
int b = 15 ;
int x = a;
System.out.println( "x is : " + x);
x=alternate(a, b, x);
System.out.println( "\nAfter change " );
System.out.println( "x is : " + x);
}
}
|
Python3
def alternate(a, b, x):
x = a * b / / x
return x
def main():
a = - 10
b = 15
x = a
print ( "x is :" , x)
x = alternate(a, b, x)
print ( "\nAfter change " )
print ( "x is :" , x)
if __name__ = = '__main__' :
main()
|
C#
using System;
public class Program
{
static void alternate( ref int a, ref int b, ref int x)
{
x = a * b / x;
}
public static void Main()
{
int a = -10;
int b = 15;
int x = a;
Console.WriteLine( "x is : " + x);
alternate( ref a, ref b, ref x);
Console.WriteLine( "\nAfter change " );
Console.WriteLine( "x is : " + x);
}
}
|
Javascript
function alternate(a, b,x )
{
x = a * b / x;
return x;
}
let a = -10;
let b = 15;
let x = a;
console.log( "x is : " + x);
x = alternate(a, b, x);
console.log( "After change " );
console.log( "x is : " + x);
|
Output:
x is : -10
After exchange
x is : 15
Time Complexity: The time complexity of this approach is O(1)
Auxiliary Space: The space complexity of this approach is O(1)
Last Updated :
06 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...