-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJohnAndGcdList.java
39 lines (37 loc) · 1.21 KB
/
JohnAndGcdList.java
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
package com.harrymdev.hackerrank.solutions;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class JohnAndGcdList {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while (t-- > 0) {
int n = scn.nextInt();
int maxNum = 0;
int maxIndex = -1;
int prev = 0;
List<Integer> numList = new ArrayList<>();
for (int i = 0; i < n; i++) {
int num = scn.nextInt();
numList.add(num);
int nextNum = num * num;
if (i > 0) {
int nextNum2 = prev * num;
if (nextNum2 > nextNum) {
nextNum = nextNum2;
}
}
if (nextNum > maxNum) {
maxNum = nextNum;
maxIndex = i;
}
prev = num;
}
numList.add(maxIndex, maxNum);
System.out.println(numList.stream().map(Object::toString).collect(Collectors.joining(" ")));
}
scn.close();
}
}