A number n and a negative base negBase is given to us, we need to represent n in that negative base. Negative base works similar to positive base. For example in base 2 we multiply bits to 1, 2, 4, 8 and so on to get actual number in decimal. In case of base -2 we need to multiply bits with 1, -2, 4, -8 and so on to get number in decimal.
Input : n = 13, negBase = -2 Output : 11101 1*(16) + 1*(-8) + 1*(4) + 0*(-2) + 1*(1) = 13
It is possible to represent a number into any negative base with same procedure (Refer Wiki for details). For simplicity (to get rid of A, B etc characters in output), we are allowing our base to be in between -2 and -10 only.
We can solve this problem similar to solving problem with positive bases but one important thing to remember is, remainder will always be positive whether we work with positive base or negative base but in most compilers, the result of dividing a negative number by a negative number is rounded towards 0, usually leaving a negative remainder.
So whenever we get a negative remainder, we can convert it to positive as below,
Let n = (?negBase) * quotient + remainder = (?negBase) * quotient + negBase ? negBase + negBase = (?negBase) * (quotient + 1) + (remainder + negBase). So if after doing "remainder = n % negBase" and "n = n/negBase", we get negative remainder, we do following. remainder = remainder + (-negBase) n = n + 1 Example : n = -4, negBase = -3 In C++, we get remainder = n % negBase = -4/-3 = -1 n = n/negBase [Next step for base conversion] = -4/-3 = 1 To avoid negative remainder, we do, remainder = -1 + (-negBase) = -1 - (-3) = 2 n = n + 1 = 1 + 1 = 2.
So when we will get negative remainder, we will make it positive by adding absolute value of base to it and adding 1 to our quotient.
Above explained approach is implemented in below code,
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Convert a given Decimal number to its BCD representation
- Find the Nth digit from right in base B of the given number in Decimal base
- Given a number N in decimal base, find the sum of digits in any base B
- Convert all substrings of length 'k' from base 'b' to decimal
- Convert from any base to decimal and vice versa
- Add two integers of different base and represent sum in smaller base of the two
- Check if a number is in given base or not
- Complement of a number with any base b
- Find most significant bit of a number X in base Y
- Check whether a number has consecutive 0's in the given base or not
- Number System and Base Conversions
- Largest value of x such that axx is N-digit number of base b
- Ternary number system or Base 3 numbers
- Count of primes after converting given binary number in base between L to R
- Check if a number is power of k using base changing method
- Number of sub arrays with negative product
- Number of subsequences with negative product
- Find the Largest N digit perfect square number in Base B
- Occurrences of a pattern in binary representation of a number
- C program to count zeros and ones in binary representation of a number