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++
#include <iostream>
using namespace std;
int assignValue( int a, int b, bool x)
{
int arr[] = {a, b};
return (arr[x]);
}
int main()
{
int y = assignValue(3, 7, 0);
cout << y;
return 0;
}
|
C
#include <stdio.h>
#include <stdbool.h>
int assignValue( int a, int b, bool x)
{
int arr[] = {a, b};
return (arr[x]);
}
int main()
{
int y = assignValue(3, 7, 0);
printf ( "%d" ,y);
return 0;
}
|
Java
class GFG {
static int assignValue( int a, int b, int x)
{
int arr[] = {a, b};
return (arr[x]);
}
public static void main(String[] args)
{
int y = assignValue( 3 , 7 , 0 );
System.out.println(y);
}
}
|
Python3
def assignValue(a, b, x):
arr = [a, b]
return (arr[x])
y = assignValue( 3 , 7 , 0 )
print (y)
|
C#
using System;
public class GFG {
static int assignValue( int a, int b, int x)
{
int []arr = {a, b};
return (arr[x]);
}
public static void Main()
{
int y = assignValue(3, 7, 0);
Console.WriteLine(y);
}
}
|
PHP
<?php
function assignValue( $a , $b , $x )
{
$arr = array ( $a , $b );
return ( $arr [ $x ]);
}
$y = assignValue(3, 7, 0);
echo $y ;
?>
|
Javascript
<script>
function assignValue(a , b , x) {
var arr = [ a, b ];
return (arr[x]);
}
var y = assignValue(3, 7, 0);
document.write(y);
</script>
|
Time complexity : O(1)
Auxiliary Space : O(1)
Alternate Solution:
C++
#include <iostream>
using namespace std;
int assignValue( int a, int b, bool x)
{
return (1 - x)*a + x*b;
}
int main()
{
int y = assignValue(3, 7, 0);
cout << y;
return 0;
}
|
C
#include <stdio.h>
#include <stdbool.h>
int assignValue( int a, int b, bool x)
{
return (1 - x)*a + x*b;
}
int main()
{
int y = assignValue(3, 7, 0);
printf ( "%d" ,y);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int assignValue( int a, int b, int x)
{
return ( 1 - x) * a + x * b;
}
public static void main (String[] args)
{
int y = assignValue( 3 , 7 , 0 );
System.out.println(y);
}
}
|
Python3
def assignValue(a, b, x):
return ( 1 - x) * a + x * b
y = assignValue( 3 , 7 , 0 )
print (y)
|
C#
using System;
class GFG {
static int assignValue( int a, int b, int x)
{
return (1 - x) * a + x * b;
}
public static void Main()
{
int y = assignValue(3, 7, 0);
Console.WriteLine(y);
}
}
|
Javascript
<script>
function assignValue(a , b , x) {
return (1 - x) * a + x * b;
}
var y = assignValue(3, 7, 0);
document.write(y);
</script>
|
Time complexity : O(1)
Auxiliary Space : O(1)
Thanks to Forrest Smith for suggesting the above solution.
This article is contributed by Maajid Bashir. If you like neveropen 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 neveropen main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!