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 ternary operator) or any arithmetic operator ( +, -, *, /).
Input : a = 5 , b = 10, x = 1 Output : y = 10 Input : a = 5, b = 10 , x = 0 Output : y = 5
Asked in : Google Interview
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 implementation
Value assigned to 'y' is 5
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 email@example.com. 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.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Set a variable without using Arithmetic, Relational or Conditional Operator
- Programming puzzle (Assign value without any control statement)
- How to sum two integers without using arithmetic operators in C/C++?
- Print "Even" or "Odd" without using conditional statement
- Implementing ternary operator without any conditional statement
- Program to Assign grades to a student using Nested If Else
- deque assign() function in C++ STL
- list assign() function in C++ STL
- forward_list assign() function in C++ STL
- C++ 17 | New ways to Assign values to Variables
- std::string::assign() in C++
- vector :: assign() in C++ STL
- Conditional or Ternary Operator (?:) in C/C++
- Increment (Decrement) operators require L-value Expression
- Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++
- Comparing String objects using Relational Operators in C++
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Sum of array using pointer arithmetic
- What are the operators that can be and cannot be overloaded in C++?
- new and delete operators in C++ for dynamic memory