Write a function which returns 1 that 2 is passed and return 2 when 1 is passed.

Source: Adobe Interview Experience | Set 19 (For MTS)

A **simple solution **is to compare the passed value with 1 and 2.

`int` `invert(` `int` `x) ` `{ ` ` ` `if` `(x == 1) ` `return` `2; ` ` ` `else` `return` `1; ` `} ` |

*chevron_right*

*filter_none*

Another solution is to **use subtraction**

`int` `invertSub(` `int` `x) ` `{ ` ` ` `return` `(3-x); ` `} ` |

*chevron_right*

*filter_none*

We can also **use bitwise xor operator**.

`int` `invertXOR(` `int` `x) ` `{ ` ` ` `return` `(x ^ 1 ^ 2); ` `} ` |

*chevron_right*

*filter_none*

This article is contributed by **Anuj**. 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 DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Write your own strcmp that ignores cases
- Write your own atoi()
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Generate 0 and 1 with 25% and 75% probability
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Maximize the expression (A AND X) * (B AND X) | Bit Manipulation
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Number of subsets with same AND, OR and XOR values in an Array
- Count subsequences with same values of Bitwise AND, OR and XOR
- Minimum XOR of OR and AND of any pair in the Array
- Maximum Bitwise AND pair (X, Y) from given range such that X and Y can be same
- Construct the Array using given bitwise AND, OR and XOR
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Find a number M < N such that difference between their XOR and AND is maximum
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR