No "IFs" or "Elses" Swap Number Brain Teaser



By IAN MALOBA
28 June 2024 | 5 mins read





Today I came across an interesting brain teaser while browsing through r/csMajors on Reddit. It is supposedly a question someone was given during an interview.

“Given a function that accepts a number (either 5 or 7), return the opposite number. Give a solution on how to implement this without using an if statement.”

It's a fun challenge. There are many solutions implemented by other users, but first let me show you a solution I thought of --> using modulus:

A modulus operation finds the remainder when one number is divided by another. Therefore this property can be used to swap the numbers 5 and 7.

Here's the function:

function swap(n) {
  return 7 - (n % 5);
}

How it Works:

  • The modulus operation n % 5 will give the remainder when n (which can only be 5 or 7 in this problem) is divided by 5.
  • When n is 5, 5 % 5 is 0. i.e 
    7 - (5 % 5) = 7 - 0 = 7
    
  • When n is 7, 7 % 5 is 2. 
    7 - (7 % 5) = 7 - 2 = 5
    

 

Well, someone argued that it is not as effective as some of the solutions below(e.g using XOR)....but it does the job, so why not ðŸ¤·

 

Group Solutions from Reddit Users

Below are solutions that others came up with.

 

GROUP SOLUTION 1: XOR

function swap(n) {
  // XOR to toggle the second bit.
  return n ^ 2;
}

The XOR (exclusive OR) solution return n ^ 2; uses bitwise operations to toggle the second bit of the input number. When n is 5 (binary 101), XOR with 2 (binary 010) results in 7 (binary 111). When n is 7 (binary 111), XOR with 2 (binary 010) results in 5 (binary 101). 

 

GROUP SOLUTION 2: ARRAY LOOKUP

function swap(n) {
  return [7, 0, 5][n - 5];
}

The array lookup solution return [7, 0, 5][n - 5]; uses the input number to index into a predefined array. Subtracting 5 from the input number ensures that the input becomes 0 for 5 and 2 for 7. The array [7, 0, 5] is indexed to return 7 when the input is 5, and 5 when the input is 7. The middle element 0 is a placeholder for invalid input, ensuring that the correct number is returned based on the index.

 

GROUP SOLUTION 3: SUBSTRING

function swap(n) {
  return parseInt("705".charAt(n - 5));
}

The substring solution return parseInt("705".charAt(n - 5)); treats the input number as an index to extract a character from the string "705". Subtracting 5 from the input number gives 0 for 5 and 2 for 7. The characters at these positions in the string "705" are '7' and '5' respectively, which are then converted back to integers using parseInt.

 

GROUP SOLUTION 4: NEGATIVE DISTANCE

function swap(n) {
  return 12 - n;
}

The negative distance solution return 12 - n; relies on simple arithmetic. Since 12 is the sum of 5 and 7, subtracting the input number from 12 directly gives the opposite number. If n is 5, 12 - 5 results in 7, and if n is 7, 12 - 7 results in 5. 

 

GROUP SOLUTION 5: DIVISION

function swap(n) {
  return 35 / n;
}

The division solution return 35 / n; uses the fact that the product of 5 and 7 is 35. By dividing 35 by the input number, you get the opposite number. If n is 5, 35 / 5 results in 7, and if n is 7, 35 / 7 results in 5. This solution leverages the multiplicative relationship between the two numbers to achieve the swap.

 

GROUP SOLUTION 6: DICTIONARY / HASHMAP

function swap(n) {
  return {5: 7, 7: 5}[n];
}

The dictionary/hashmap solution return {5: 7, 7: 5}[n]; uses a key-value pair structure to map the input number to its opposite. When n is 5, the hashmap returns 7, and when n is 7, it returns 5. 

 

Now, below is the obvious implementation that is not allowed. It is a simple if statement: 

function swap(n) {
  if (n == 5) {
    return 7;
  }
  return 5;
}

 

Try to think of more solutions, have fun!

 




<< Previous

HOME



9



TAGS:


RELATED ARTICLES:
  1. 3 Huge Mistakes Django Developers Make
  2. Launching A Web Dev. Startup: JupiterWeb.xyz
  3. Signal Basics of Raspberry Pi IR Receiver