-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatNumbers.cs
More file actions
68 lines (60 loc) · 2.37 KB
/
RepeatNumbers.cs
File metadata and controls
68 lines (60 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms.Problem.InterviewCake
{
[TestClass]
public class RepeatNumbers
{
public static int FindRepeat(int[] items)
{
int floor = 1;
int ceiling = items.Length - 1;
while (floor < ceiling)
{
// Divide our range 1..n into an upper range and lower range
// (such that they don't overlap)
// Lower range is floor..midpoint
// Upper range is midpoint+1..ceiling
int midpoint = floor + (ceiling - floor) / 2;
int lowerRangeFloor = floor;
int lowerRangeCeiling = midpoint;
int upperRangeFloor = midpoint + 1;
int upperRangeCeiling = ceiling;
// Count number of items in lower range
int itemsInLowerRange = items.Count(item => item >= lowerRangeFloor && item <= lowerRangeCeiling);
int distinctPossibleIntegersInLowerRange = lowerRangeCeiling - lowerRangeFloor + 1;
if (itemsInLowerRange > distinctPossibleIntegersInLowerRange)
{
// There must be a duplicate in the lower range
// so use the same approach iteratively on that range
floor = lowerRangeFloor;
ceiling = lowerRangeCeiling;
}
else
{
// There must be a duplicate in the upper range
// so use the same approach iteratively on that range
floor = upperRangeFloor;
ceiling = upperRangeCeiling;
}
}
// Floor and ceiling have converged
// We found a number that repeats!
return floor;
}
[TestMethod]
public void LongArrayTest()
{
// var numbers = new int[] { 4, 1, 4, 8, 3, 2, 7, 6, 5 };
// var numbers = new int[] { 5, 8, 4, 1, 3, 2, 7, 6, 8 };
var numbers = new int[] { 1, 3, 9, 8, 4, 6, 1, 2, 5, 7 };
var expected = 1;
var actual = FindRepeat(numbers);
Assert.AreEqual(expected, actual);
}
}
}