# r's and (r-1)'s Complement of Numbers

Here, we are going to learn about the **r's compliment and (r-1)'s compliment of the numbers** with examples.

Submitted by Saurabh Gupta, on October 24, 2019

For a number system having its base/radix as **r**, we can define two types of complement for the corresponding number system which are as follows:

## 1) (r-1)'s complement

The **(r-1)'s complement of a number** in any number system with base **r** can be found out by subtracting every single digit of a number by **r-1**.

**For Example:** In the binary number system, the base is **2**. Hence, its **(r-1)'s** i.e., **(2-1 =1)'s** complement can be obtained by subtracting each bit from 1, i.e., **1's** complement for **001** can also be calculated by subtracting **001** from **111** which will be **(111-001) = (110) _{2}**.

Similarly, in the octal number system, the base is **8** so its **7's** complement can be calculated by subtracting each bit by **7**, i.e., **7's** complement for **347** in octal number system can be calculated by subtracting **347** from **777** which will yield **(777 – 347) = (430) _{8}**.

## 2) r's complement

The **r's complement** of a non-zero number in any number system with base **r** can be calculated by adding **1** to the LSB of its **(r-1)'s** complement.

**For Example:** In binary number system, **2's** complement of **001** can be calculated by adding 1 to the LSB of its 1'complement (i.e., **110 + 1**) = **(111) _{2}**.

Similarly, in octal number system, **8's** complement of **347** can be calculated by adding 1 to the LSB of its **7'complement** (i.e., **430 + 1**) = **(431) _{8}**.

### 9's and 10's complement in Decimal Number System

We already know that the decimal number system has its base as 10, As we have already discussed above, 9's complement of decimal number can be found out by subtracting its each by 9.

**Example 1:** Calculate 9's complement of (2457)^{10}

**Solution:** 9999 – 2457 = (7542)^{10}

Now, 10's complement of a decimal number can be calculated by adding 1 to the LSB of the 9's complement.

**Example 2:** Calculate 10's complement of (2457)^{10}

**Solution:** 10's complement for (2457)^{10} is calculated by adding 1 to (7542)^{10} which is the 9's complement. Therefore, 10's complement of (2457)^{10} is (7543)^{10}.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.