Algorithm

Write a function that returns 2 for input 1 and returns 1 for 2

Write a function which returns 1 that 2 is passed and return 2 when 1 is passed.Source : Adobe Interview Experience Set 19 .

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.

C Program1
int invert(int x)
{
   if (x == 1) return 2;
   else return 1;
}

Another solution is to use subtraction

C Program 2
int invertSub(int x)
{
   return (3-x);
}

We can also use bitwise xor operator.

C Program 3
int invertXOR(int x)
{
   return (x ^ 1 ^ 2);
}
READ  Python Programming - Given two strings, find if first string is a subsequence of second

About the author

Wikitechy Editor

Wikitechy Editor

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

X