Problem Statement
John and Brus are training for a card game tournament. John is practicing his shuffling technique. John is using a deck of n cards, numbered 1 to n from top to bottom. This initial deck is called the main deck. There are three additional decks on the table, called the left, right and resulting decks. These three decks are initially empty. To shuffle the deck, John will repeat the following sequence of actions until the main deck contains less than two cards:
- Move one card from the top of the main deck to the top of the left deck, then one card from the top of the main deck to the top of the right deck, then one card from top of the main deck to the top of the left deck, and so on, until the main deck is empty.
- Repeat the following left times: Move one card from the top of the left deck to the bottom of the left deck.
- Repeat the following right times: Move one card from the top of the right deck to the bottom of the right deck.
- Move one card from the top of the left deck to the top of the resulting deck.
- Move one card from the top of the right deck to the top of the resulting deck.
- While the left deck is not empty, move one card from the top of the left deck to the top of the main deck.
- While the right deck is not empty, move one card from the top of the right deck to the top of the main deck.
Definition
- Class:
- TheCardShufflingDivOne
- Method:
- shuffle
- Parameters:
- int, int, int
- Returns:
- int
- Method signature:
- int shuffle(int n, int left, int right)
- (be sure your method is public)
Constraints
- n will be between 1 and 1,000,000, inclusive.
- left and right will each be between 0 and 1,000,000, inclusive.
Examples
3
0
0
Returns: 1
The resulting deck will contain cards in the same order as the main deck at the beginning.
3
1
1
Returns: 3
Here the order of cards in the resulting deck (from top to bottom) will be 3, 2, 1.
5
0
0
Returns: 2
17
12
21
Returns: 17
5
6
0
Returns: 2
3
8
8
Returns: 1
4
0
3
Returns: 1
48
9
66
Returns: 47
25
92
51
Returns: 17
88
27
69
Returns: 66
73
18
96
Returns: 49
45
4
55
Returns: 5
23
95
58
Returns: 11
34
35
30
Returns: 13
27
65
89
Returns: 16
5
79
75
Returns: 1
84
49
7
Returns: 24
61
88
82
Returns: 59
81
59
99
Returns: 57
11
43
99
Returns: 1
148
317
244
Returns: 113
125
429
819
Returns: 16
388
966
560
Returns: 99
573
599
410
Returns: 217
545
711
713
Returns: 318
523
541
350
Returns: 440
434
915
369
Returns: 76
227
528
616
Returns: 178
605
753
294
Returns: 564
784
858
166
Returns: 590
757148
850833
413055
Returns: 200126
971125
597709
567065
Returns: 319142
749388
890847
766255
Returns: 512006
239573
37691
597006
Returns: 229403
615545
51110
289218
Returns: 573794
341523
427628
215491
Returns: 253050
16434
544122
446450
Returns: 15465
90227
426790
752135
Returns: 45406
41605
220562
908065
Returns: 32411
655784
970659
417541
Returns: 6039
1000000
949322
910342
Returns: 257220
1000000
991773
965620
Returns: 33667
1000000
990803
965933
Returns: 641167
999999
951001
913357
Returns: 47064
999999
998369
967226
Returns: 791604
999999
990852
966291
Returns: 17052
1000000
1000000
1000000
Returns: 368436
345
6
54
Returns: 47
999999
287
2987
Returns: 939833
999100
0
517222
Returns: 263033
1000000
4367
544768
Returns: 66015
100000
0
100000
Returns: 9066
1000000
21
32
Returns: 666038
1000000
1001
9999
Returns: 637626
999771
123457
75431
Returns: 594309
959564
956314
955622
Returns: 118319
1000000
0
0
Returns: 9026
2
0
0
Returns: 2
100
12
23
Returns: 6
1000000
77777
97896
Returns: 361394
1000000
999998
999991
Returns: 341388
1000000
2
3
Returns: 447596
989992
9881
984082
Returns: 311065
3
197
956
Returns: 3
4
0
0
Returns: 1