Programming puzzle (Assign value without any control statement)
Last Updated :
20 Jul, 2022
Given four integers 'a', 'b', 'y' and 'x', where 'x' can only be either zero or one. Your task is as follows:
- If 'x' is zero assign value 'a' to the variable 'y'
- If 'x' is one assign value 'b' to the variable 'y'.
It is not allowed to use any conditional operator (including the ternary operator).
Examples :
Input : a = 3, b = 7, x = 1
Output : y = 7
Input : a = 3, b = 7, x = 0
Output : y = 3
The idea is to create an array of size two where the first element is 'a' and the second element is 'b'. We use x as an index in the array.
Implementation:
C++
// CPP program to pick a value among two
// according to value of a third variable.
#include <iostream>
using namespace std;
// Returns a if x is 0 and returns
// b if x is 1.
int assignValue(int a, int b, bool x)
{
int arr[] = {a, b};
return(arr[x]);
}
// Driver code
int main()
{
int y = assignValue(3, 7, 0);
cout << y;
return 0;
}
C
// C program to pick a value among two
// according to value of a third variable.
#include <stdio.h>
#include <stdbool.h>
// Returns a if x is 0 and returns
// b if x is 1.
int assignValue(int a, int b, bool x)
{
int arr[] = {a, b};
return(arr[x]);
}
// Driver code
int main()
{
int y = assignValue(3, 7, 0);
printf("%d",y);
return 0;
}
// This code is contributed by kothvvsaakash.
Java
// Java program to pick a value among two
// according to value of a third variable.
class GFG {
// Returns a if x is 0 and returns
// b if x is 1.
static int assignValue(int a, int b, int x)
{
int arr[] = {a, b};
return (arr[x]);
}
// Driver code
public static void main(String[] args)
{
int y = assignValue(3, 7, 0);
System.out.println(y);
}
}
// This code is contributed by Smitha Dinesh Semwal.
Python3
# Python 3 program to
# pick a value among two
# according to value
# of a third variable.
# Returns a if x
# is 0 and returns
# b if x is 1.
def assignValue(a, b, x):
arr = [a, b]
return(arr[x])
# Driver code
y = assignValue(3, 7, 0)
print(y)
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# program to pick a value among two
// according to value of a third variable.
using System;
public class GFG {
// Returns a if x is 0 and returns
// b if x is 1.
static int assignValue(int a, int b, int x)
{
int []arr = {a, b};
return (arr[x]);
}
// Driver code
public static void Main()
{
int y = assignValue(3, 7, 0);
Console.WriteLine(y);
}
}
// This code is contributed by PrinciRaj1992
PHP
<?php
// PHP program to pick a value
// among two according to value
// of a third variable.
// Returns a if x is 0 and
// returns b if x is 1.
function assignValue($a, $b, $x)
{
$arr = array($a, $b);
return($arr[$x]);
}
// Driver code
$y = assignValue(3, 7, 0);
echo $y;
// This code is contributed by ajit
?>
JavaScript
<script>
// javascript program to pick a value among two
// according to value of a third variable.
// Returns a if x is 0 and returns
// b if x is 1.
function assignValue(a , b , x) {
var arr = [ a, b ];
return (arr[x]);
}
// Driver code
var y = assignValue(3, 7, 0);
document.write(y);
// This code is contributed by todaysgaurav
</script>
Time complexity : O(1)
Auxiliary Space : O(1)
Alternate Solution:
C++
// C++ program to pick a value among two
// according to value of a third variable.
#include <iostream>
using namespace std;
// Returns a if x is 0 and returns
// b if x is 1.
int assignValue(int a, int b, bool x)
{
return (1 - x)*a + x*b;
}
// Driver code
int main()
{
int y = assignValue(3, 7, 0);
cout << y;
return 0;
}
C
// C program to pick a value among two
// according to value of a third variable.
#include <stdio.h>
#include <stdbool.h>
// Returns a if x is 0 and returns
// b if x is 1.
int assignValue(int a, int b, bool x)
{
return (1 - x)*a + x*b;
}
// Driver code
int main()
{
int y = assignValue(3, 7, 0);
printf("%d",y);
return 0;
}
// This code is contributed by kothvvsaakash.
Java
// Java program to pick a value among two
// according to value of a third variable.
import java.io.*;
class GFG {
// Returns a if x is 0 and returns
// b if x is 1.
static int assignValue(int a, int b, int x)
{
return (1 - x) * a + x * b;
}
// Driver code
public static void main (String[] args)
{
int y = assignValue(3, 7, 0);
System.out.println(y);
}
}
// This code is contributed by ShubhamCoder
Python3
# Python3 program to pick a value among two
# according to the value of a third variable.
# Returns a if x is 0 and returns
# b if x is 1.
def assignValue(a, b, x):
return (1 - x) * a + x * b
# Driver code
y = assignValue(3, 7, 0)
print(y)
# This code is contributed by ShubhamCoder
C#
// C# program to pick a value among two
// according to value of a third variable.
using System;
class GFG {
// Returns a if x is 0 and returns
// b if x is 1.
static int assignValue(int a, int b, int x)
{
return (1 - x) * a + x * b;
}
// Driver code
public static void Main()
{
int y = assignValue(3, 7, 0);
Console.WriteLine(y);
}
}
// This code is contributed by ShubhamCoder
JavaScript
<script>
// Javascript program to pick a value among two
// according to value of a third variable.
// Returns a if x is 0 and returns
// b if x is 1.
function assignValue(a , b , x) {
return (1 - x) * a + x * b;
}
// Driver code
var y = assignValue(3, 7, 0);
document.write(y);
// This code contributed by Rajput-Ji
</script>
Time complexity : O(1)
Auxiliary Space : O(1)
Thanks to Forrest Smith for suggesting the above solution.
Similar Reads
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 Exp
9 min read
Assignment Operators in Programming Assignment operators in programming are symbols used to assign values to variables. They offer shorthand notations for performing arithmetic operations and updating variable values in a single step. These operators are fundamental in most programming languages and help streamline code while improvin
7 min read
Assignment Operators in C++ In C++, the assignment operator forms the backbone of computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language i.e. assign some value to the variables in
6 min read
Assignment Operators in C In C, assignment operators are used to assign values to variables. The left operand is the variable and the right operand is the value being assigned. The value on the right must match the data type of the variable otherwise, the compiler will raise an error.Let's take a look at an example:C#include
4 min read
Java Assignment Operators with Examples Operators constitute the basic building block of any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they
7 min read