-
Notifications
You must be signed in to change notification settings - Fork 1
/
isBitonic.ts
30 lines (23 loc) · 897 Bytes
/
isBitonic.ts
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
/*
Write a function that determines if an array of numbers is a bitonic sequence.
A bitonic sequence is a sequence of numbers in which the numbers are in increasing order,
and after a certain point, they start decreasing.
Extra credit: print the peak number in the sequence!
Example:
> isBitonic([1,2,3,2])
> true // extra credit: 3
> isBitonic([1,2,3])
> false
> isBitonic([3,4,5,5,5,2,1])
> true // extra credit: 5
*/
function checkPeak(number: number, i: number, array: number[]): boolean {
return number >= array[i - 1] && number > array[i + 1];
}
export function isBitonic(arr: number[]): { isBitonic: boolean; peak: number | null } {
const peakIndex = arr.findIndex(checkPeak);
const nextPeak = arr.slice(peakIndex + 1).find(checkPeak);
const hasSinglePeak = !nextPeak && peakIndex >= 1;
const peak = hasSinglePeak ? arr[peakIndex] : null;
return { isBitonic: !!peak, peak };
}