The Wayback Machine - https://web.archive.org/web/20210206212053/https://www.geeksforgeeks.org/conditionally-assign-value-without-using-conditional-arithmetic-operators/
Skip to content
Related Articles

Related Articles

Conditionally assign a value without using conditional and arithmetic operators
  • Difficulty Level : Easy
  • Last Updated : 25 Jan, 2021

Given 4 integers a, b, y, and x, where x can only either 0 and 1 only. The ask is as follows: 

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 1th index respectively. Then store value to ‘y’ by taking ‘x’ as index.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C/C++ program to assign value to y according
// to value of x
 
#include <iostream>
using namespace std;
 
// Function to assign value to y according
// to value of x
int assignValue(int a, int b, int x)
{
    int y;
    int arr[2];
 
    // Store both values in an array
    // value 'a' at 0th index
    arr[0] = a;
 
    // Value 'b' at 1th index
    arr[1] = b;
 
    // Assign value to 'y' taking 'x' as index
    y = arr[x];
 
    return y;
}
 
// Driver code
int main()
{
    int a = 5;
    int b = 10;
    int x = 0;
 
    cout << "Value assigned to 'y' is "
         << assignValue(a, b, x);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to assign value to y according
// to value of x
 
public class GFG {
    static int assignValue(int a, int b, int x)
    {
        int y;
        int arr[] = new int[2];
 
        // Store both values in an array
        // value 'a' at 0th index
        arr[0] = a;
 
        // Value 'b' at 1th index
        arr[1] = b;
 
        // Assign value to 'y' taking 'x' as index
        y = arr[x];
 
        return y;
    }
 
    // Driver Method
    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));
    }
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to assign value to
# y according to value of x
 
# Function to assign value to y
# according to value of x
 
 
def assignValue(a, b, x):
 
    arr = [0] * 2
 
    # Store both values in an array
    # value 'a' at 0th index
    arr[0] = a
 
    # Value 'b' at 1th index
    arr[1] = b
 
    # Assign value to 'y' taking 'x'
    # as index
    y = arr[x]
 
    return y
 
 
# Driver code
if __name__ == "__main__":
 
    a = 5
    b = 10
    x = 0
 
    print("Value assigned to 'y' is",
          assignValue(a, b, x))
 
# This code is contributed by ita_c

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to assign value to y according
// to value of x
using System;
 
public class GFG {
 
    static int assignValue(int a, int b, int x)
    {
        int y;
        int[] arr = new int[2];
 
        // Store both values in an array
        // value 'a' at 0th index
        arr[0] = a;
 
        // Value 'b' at 1th index
        arr[1] = b;
 
        // Assign value to 'y' taking 'x'
        // as index
        y = arr[x];
 
        return y;
    }
 
    // Driver Code
    public static void Main()
    {
        int a = 5;
        int b = 10;
        int x = 0;
 
        Console.Write("Value assigned to "
                      + "'y' is " + assignValue(a, b, x));
    }
}
 
// This code is contributed by nitin mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to assign value
// to y according to value of x
 
// Function to assign value
// to y according to value of x
function assignValue($a, $b, $x)
{
    $y;
    $arr = array(0, 0);
 
    // Store both values in an array
    // value 'a' at 0th index
    $arr[0] = $a;
 
    // Value 'b' at 1th index
    $arr[1] = $b;
 
    // Assign value to 'y'
    // taking 'x' as index
    $y = $arr[$x];
 
    return $y;
}
 
// Driver code
$a = 5;
$b = 10;
$x = 0;
 
echo "Value assigned to 'y' is " .
        assignValue($a, $b, $x);
 
// This code is contributed by Anuj_67
?>

chevron_right


Output

Value assigned to 'y' is 5

Solution 2: 

Using bitwise AND operator.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C/C++ program to assign value to y according
// to value of x
 
#include <iostream>
using namespace std;
 
// Driver Code
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;
}

chevron_right


Reference : https://www.careercup.com/question?id=5135296679116800

This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.
 

 

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up