Given 4 integers a, b, y, and x, where x can assume the values of either 0 or 1 only. The following question is asked:
If 'x' is 0,
Assign value 'a' to variable 'y'
Else (If 'x' is 1)
Assign value 'b' to variable 'y'.
Note: – You are not allowed to use any conditional operator (including the ternary operator) or any arithmetic operator (+, -, *, /).
Examples :
Input : a = 5 , b = 10, x = 1
Output : y = 10
Input : a = 5, b = 10 , x = 0
Output : y = 5
Asked in: Google Interview
Solution 1:
An Idea is to simply store both ‘a’ and ‘b’ in an array at 0th and 1st index respectively. Then store value to ‘y’ by taking ‘x’ as an index.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int assignValue( int a, int b, int x)
{
int y;
int arr[2];
arr[0] = a;
arr[1] = b;
y = arr[x];
return y;
}
int main()
{
int a = 5;
int b = 10;
int x = 0;
cout << "Value assigned to 'y' is "
<< assignValue(a, b, x);
return 0;
}
|
Java
public class GFG {
static int assignValue( int a, int b, int x)
{
int y;
int arr[] = new int [ 2 ];
arr[ 0 ] = a;
arr[ 1 ] = b;
y = arr[x];
return y;
}
public static void main(String[] args)
{
int a = 5 ;
int b = 10 ;
int x = 0 ;
System.out.println( "Value assigned to 'y' is "
+ assignValue(a, b, x));
}
}
|
Python3
def assignValue(a, b, x):
arr = [ 0 ] * 2
arr[ 0 ] = a
arr[ 1 ] = b
y = arr[x]
return y
if __name__ = = "__main__" :
a = 5
b = 10
x = 0
print ( "Value assigned to 'y' is" ,
assignValue(a, b, x))
|
C#
using System;
public class GFG {
static int assignValue( int a, int b, int x)
{
int y;
int [] arr = new int [2];
arr[0] = a;
arr[1] = b;
y = arr[x];
return y;
}
public static void Main()
{
int a = 5;
int b = 10;
int x = 0;
Console.Write( "Value assigned to "
+ "'y' is " + assignValue(a, b, x));
}
}
|
PHP
<?php
function assignValue( $a , $b , $x )
{
$y ;
$arr = array (0, 0);
$arr [0] = $a ;
$arr [1] = $b ;
$y = $arr [ $x ];
return $y ;
}
$a = 5;
$b = 10;
$x = 0;
echo "Value assigned to 'y' is " .
assignValue( $a , $b , $x );
?>
|
Javascript
<script>
function assignValue(a , b , x) {
var y;
var arr = Array(2);
arr[0] = a;
arr[1] = b;
y = arr[x];
return y;
}
var a = 5;
var b = 10;
var x = 0;
document.write( "Value assigned to 'y' is "
+ assignValue(a, b, x));
</script>
|
Output
Value assigned to 'y' is 5
Solution 2:
Using bitwise AND operator.
C++
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 10;
int x = 1;
int y;
if (x & 1)
y = b;
else
y = a;
cout << "Value assigned to 'y' is " << y << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static void main (String[] args)
{
int a = 5 ;
int b = 10 ;
int x = 1 ;
int y;
if ((x & 1 ) != 0 )
y = b;
else
y = a;
System.out.println( "Value assigned to 'y' is " + y);
}
}
|
Python3
a = 5
b = 10
x = 1
y = 0
if ((x & 1 ) ! = 0 ):
y = b
else :
y = a
print ( "Value assigned to 'y' is " , y)
|
C#
using System;
public class GFG
{
static public void Main ()
{
int a = 5;
int b = 10;
int x = 1;
int y;
if ((x & 1) != 0)
y = b;
else
y = a;
Console.WriteLine( "Value assigned to 'y' is " + y);
}
}
|
Javascript
<script>
var a = 5;
var b = 10;
var x = 1;
var y;
if ((x & 1) != 0)
y = b;
else
y = a;
document.write( "Value assigned to 'y' is " + y);
</script>
|
Output
Value assigned to 'y' is 10
Reference: https://www.careercup.com/question?id=5135296679116800
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
13 Sep, 2023
Like Article
Save Article