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
|
package theweeklychallenge;
/*
Week 191:
https://theweeklychallenge.org/blog/perl-weekly-challenge-191
Task #1: Twice Largest
You are given list of integers, @list.
Write a script to find out whether the largest item in the list
is at least twice as large as each of the other items.
Compile and Run:
mohammad-anwar/java$ javac theweeklychallenge/TwiceLargest.java
mohammad-anwar/java$ java theweeklychallenge.TwiceLargest
*/
import java.util.Arrays;
import junit.framework.TestCase;
import static junit.framework.Assert.*;
public class TwiceLargest extends TestCase {
public static void main(String[] args) {
junit.textui.TestRunner.run(
theweeklychallenge.TwiceLargest.class
);
}
public void testTwiceLargest() {
assertEquals(twiceLargest(new int[]{1, 2, 3, 4}), -1);
assertEquals(twiceLargest(new int[]{1, 2, 0, 5}), 1);
assertEquals(twiceLargest(new int[]{2, 6, 3, 1}), 1);
assertEquals(twiceLargest(new int[]{4, 5, 2, 3}), -1);
}
public static int twiceLargest(int[] array) {
Arrays.sort(array);
int max = array[array.length-1];
for(int i : array) {
if (i == max) { continue; }
if (i * 2 > max) { return -1; };
}
return 1;
}
}
|