Just like base 2 Binary numeral system having 0s and 1s as digits, Ternary(Trinary) Numeral System is a base 3 number system having 0s, 1s and -1 as digits.
It’s better to use alphabet ‘Z’ in place of -1, since while denoting full ternary number -1 looks odd in between 1s and 0s.
Conversion of decimal into Balanced Ternary:
As in binary conversion, first represent the decimal number into the normal ternary system having 0, 1, 2 as reminders.
Now Iterating from the lowest digit safely skip any 0s and 1s, however turn 2 into Z and add 1 to the next digit. Turn 3 into 0 on the same terms( such digits are not present in the number initially but they can be encountered after increasing some 2s. )
Balanced Ternary: 1ZZZ1Z
Balanced Ternary: 111Z101
Recovering original decimal number from a balanced ternary number:-
Procedure:- Similarly as it’s done in binary to decimal conversion
There are two robots allowed to move in steps on x-axis starting from 0.
They can make several steps starting from 0 but there are some limitations on their movement.
In step robot will move exact units of distance.
In each step robot must choose one of the two directions left (x- coordinate decreases) or right (x-coordinate increases), in a particular step only one robot will move and another will wait.
It is not allowed to skip any step.
Given two integers x1 and x2. Robot 1 and 2 are separately required to cover their respective distances x1 and x2. Is it possible??
If it is possible you won otherwise you lose.
There is only one balanced ternary representation of each Decimal number (distance here), this means there is only one way to cover a particular distance satisfying above rules.
So, if it is possible to cover distances x1 and x2 such that when one robot moves other remains still and both can’t remain still at the same time then it’s a victory.
First represent x1 and x2 as balanced ternary number using above procedure.
Iterate from LSB check:-
At a time(step) only one value should be 1 or Z.
Both can’t be 0 at the same time(step).
If rule breaks at any step it’s your lose otherwise you won.
Input: x1 = 6890, x2 = 18252
Balanced ternary representation of x1 = 01001101ZZ
Balanced ternary representation of x2 = 10Z1001000
Input: x1 = 18, x2 = 45
Balanced ternary representation of x1 = 01Z00
Balanced ternary representation of x2 = 1ZZ00
Iterate bitwise over both the arrays and break wherever rule breaks.
To do first make length of both arrays equal by adding 0s at beginning of the shortest one, such that length becomes same.
Below is the implementation of the above approach:
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Game Theory (Normal - form game) | Set 1 (Introduction)
- Combinatorial Game Theory | Set 2 (Game of Nim)
- Combinatorial Game Theory | Set 1 (Introduction)
- Expectimax Algorithm in Game Theory
- The prisoner's dilemma in Game theory
- Pareto Optimality and its application in Game Theory
- Minimax Algorithm in Game Theory | Set 1 (Introduction)
- Minimax Algorithm in Game Theory | Set 5 (Zobrist Hashing)
- Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)
- Combinatorial Game Theory | Set 3 (Grundy Numbers/Nimbers and Mex)
- Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI - Finding optimal move)
- Minimax Algorithm in Game Theory | Set 4 (Alpha-Beta Pruning)
- Minimax Algorithm in Game Theory | Set 2 (Introduction to Evaluation Function)
- Min steps to empty an Array by removing a pair each time with sum at most K
- Count number of steps to cover a distance if steps can be taken in powers of 2
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.