Problem Statement
Ternary search is one of many algorithms in which we need to split a range of integers into three equal (or almost equal) parts. That will be your task in this problem.
A half-open interval [x,y) is the set of integers that are greater than or equal to x and strictly less than y. Hence, [x,y) = { x, x+1, x+2, ..., y-2, y-1 }. For example, [3,7) = { 3, 4, 5, 6 }.
You are given two
Return {b, c}. That is, return a
Definition
- Class:
- ThreePartSplit
- Method:
- split
- Parameters:
- int, int
- Returns:
- int[]
- Method signature:
- int[] split(int a, int d)
- (be sure your method is public)
Notes
- The formula "n div 3" means "n divided by 3, rounded down". For example, 14 div 3 = 4.
Constraints
- a will be between 0 and 10^8, inclusive.
- d will be between a+3 and 10^8, inclusive.
Examples
0
6
Returns: {2, 4 }
We are given the range [0,6) = {0,1,2,3,4,5}. We need to split it into three parts, each containing at least (6 div 3) = 2 elements. Clearly, the only valid solution is to split it into {0,1}, {2,3}, and {4,5}, that is, intervals [0,2), [2,4), and [4,6). Thus, we must have b=2 and c=4, and we must return {2,4}.
10
14
Returns: {11, 12 }
When splitting the interval [10,14) into three roughly equal parts, we have multiple valid options: Split it into [10,11), [11,12), and [12,14). Split it into [10,11), [11,13), and [13,14). Split it into [10,12), [12,13), and [13,14). Hence, there are three valid return values: {11, 12}, {11, 13}, and {12, 13}. Your solution may return any one of these three.
127
345
Returns: {199, 271 }
0
100000000
Returns: {33333333, 66666666 }
57738620
74308313
Returns: {63261851, 68785082 }
60890672
76610214
Returns: {66130519, 71370366 }
52669103
82619242
Returns: {62652482, 72635861 }
5490675
56174495
Returns: {22385281, 39279887 }
35407527
64632919
Returns: {45149324, 54891121 }
1226180
42356098
Returns: {14936152, 28646124 }
30876379
31909169
Returns: {31220642, 31564905 }
48195185
59353897
Returns: {51914755, 55634325 }
47439601
80661580
Returns: {58513594, 69587587 }
45890888
74865365
Returns: {55549047, 65207206 }
32026925
97864709
Returns: {53972853, 75918781 }
4735057
16844269
Returns: {8771461, 12807865 }
48699376
77625098
Returns: {58341283, 67983190 }
51483060
68223645
Returns: {57063255, 62643450 }
13032361
87081599
Returns: {37715440, 62398519 }
30461759
93846145
Returns: {51589887, 72718015 }
63220606
99882012
Returns: {75441074, 87661542 }
14783505
55923193
Returns: {28496734, 42209963 }
32075749
74526545
Returns: {46226014, 60376279 }
35109973
47908112
Returns: {39376019, 43642065 }
71495688
75884514
Returns: {72958630, 74421572 }
5383446
64954137
Returns: {25240343, 45097240 }
41903666
75529436
Returns: {53112256, 64320846 }
29653664
81382227
Returns: {46896518, 64139372 }
15292186
84209166
Returns: {38264512, 61236838 }
27794650
50984921
Returns: {35524740, 43254830 }
20025590
59577419
Returns: {33209533, 46393476 }
2038795
22856691
Returns: {8978093, 15917391 }
73416107
84175572
Returns: {77002595, 80589083 }
35668351
68616779
Returns: {46651160, 57633969 }
69386059
90477545
Returns: {76416554, 83447049 }
63325269
95051093
Returns: {73900543, 84475817 }
2984114
77432999
Returns: {27800409, 52616704 }
46434549
57933395
Returns: {50267497, 54100445 }
23713243
63054542
Returns: {36827009, 49940775 }
13209403
18342176
Returns: {14920327, 16631251 }
46178284
70786583
Returns: {54381050, 62583816 }
35818037
59582023
Returns: {43739365, 51660693 }
60652
87593894
Returns: {29238399, 58416146 }
16425002
55575540
Returns: {29475181, 42525360 }
9014669
49559704
Returns: {22529680, 36044691 }
2781207
35466737
Returns: {13676383, 24571559 }
5597578
18849325
Returns: {10014827, 14432076 }
24317463
99206390
Returns: {49280438, 74243413 }
26997684
66396815
Returns: {40130727, 53263770 }
75645855
76146682
Returns: {75812797, 75979739 }
1340478
93286024
Returns: {31988993, 62637508 }
100
105
Returns: {101, 104 }
As the original interval contains d-a = 5 elements, the requirement is that each of the three new intervals must contain at least one element. The return value of our solution corresponds to splitting the original interval into intervals of length 1, 3, and 1.
100
106
Returns: {102, 104 }