Skip to content

Commit ed693d3

Browse files
committed
[GCD] baekjoon-14476
1 parent 61229a1 commit ed693d3

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
| 01 | | [Baekjoon-2003 수들의 합 2](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/TimeComplexity/P2003) | 2-pointer |
2121
| 02 | ⭐️ | [Baekjoon-2805 나무 자르기](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/TimeComplexity/P2805) | Parametric Search |
2222
| 03 | ⭐️ | [Baekjoon-2143 두 배열의 합](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/TimeComplexity/P2143) | Lower Bound & Upper Bound |
23-
| 03 | | [Baekjoon-2748 피보나치 수 2](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/TimeComplexity/P2748) | DP |
24-
| 03 | | [Baekjoon-1806 부분합](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/TimeComplexity/P1806) | 2-pointer |
23+
| 04 | | [Baekjoon-2748 피보나치 수 2](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/TimeComplexity/P2748) | DP |
24+
| 05 | | [Baekjoon-1806 부분합](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/TimeComplexity/P1806) | 2-pointer |
2525

2626
## Data Structure
2727

@@ -52,4 +52,13 @@
5252
| :-: | :----: | :----------------------------------------------------------------------------------------------------------------------- | :--- |
5353
| 01 | | [Indexed Tree Example](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/IndexedTree/IndexedTreeTest.java) | |
5454
| 02 | ⭐️ | [Baekjoon-2042 구간 합 구하기](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/IndexedTree/P2042) | |
55-
| 02 | ⭐️ | [Baekjoon-2243 사탕상자](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/IndexedTree/P2243) | |
55+
| 03 | ⭐️ | [Baekjoon-2243 사탕상자](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/IndexedTree/P2243) | |
56+
57+
## Number Theory
58+
59+
### GCD
60+
61+
| # || Problem | Note |
62+
| :-: | :----: | :------------------------------------------------------------------------------------------------------------------ | :------------------------ |
63+
| 01 | | [GCD Example](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/GCD/GCDTest.java) | |
64+
| 02 | | [Baekjoon-14476 최대공약수 하나 빼기](https://github.com/Seogeurim/Algorithm-practice/tree/master/src/GCD/P14476) | 누적합 |

src/GCD/GCDTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package GCD;
2+
3+
public class GCDTest {
4+
5+
public static void main(String[] args) {
6+
System.out.println("[Get GCD By Euclidean algorithm]");
7+
System.out.println(gcd(132, 36));
8+
System.out.println(gcd(36, 132));
9+
}
10+
11+
static int gcd(int a, int b) {
12+
System.out.println("=========================");
13+
System.out.println("gcd(" + a + ", " + b + ")");
14+
while (b != 0) {
15+
int r = a % b;
16+
a = b;
17+
b = r;
18+
System.out.println("gcd(" + a + ", " + b + ")");
19+
}
20+
return a;
21+
}
22+
}

src/GCD/P14476/Main.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package GCD.P14476;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileInputStream;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
import java.util.StringTokenizer;
8+
9+
public class Main {
10+
11+
static int N;
12+
static int[] nums;
13+
static int answer = 0, K;
14+
15+
public static void main(String[] args) throws Exception {
16+
// System.setIn(new FileInputStream("src/NumberTheory/gcd/P14476/input.txt"));
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
StringTokenizer st;
19+
20+
N = Integer.parseInt(br.readLine());
21+
22+
nums = new int[N];
23+
st = new StringTokenizer(br.readLine());
24+
for (int i = 0; i < N; i++) {
25+
nums[i] = Integer.parseInt(st.nextToken());
26+
}
27+
28+
int[] gcdLtoR = new int[N];
29+
int[] gcdRtoL = new int[N];
30+
31+
gcdLtoR[0] = nums[0];
32+
for (int i = 1; i < N; i++) {
33+
gcdLtoR[i] = gcd(gcdLtoR[i-1], nums[i]);
34+
}
35+
36+
gcdRtoL[N - 1] = nums[N - 1];
37+
for (int i = N - 2; i >= 0; i--) {
38+
gcdRtoL[i] = gcd(gcdRtoL[i+1], nums[i]);
39+
}
40+
41+
// System.out.println(Arrays.toString(gcdLtoR));
42+
// System.out.println(Arrays.toString(gcdRtoL));
43+
44+
for (int i = 0; i < N; i++) {
45+
int gcd = 0;
46+
if (i - 1 < 0) {
47+
gcd = gcdRtoL[i + 1];
48+
} else if (i + 1 >= N) {
49+
gcd = gcdLtoR[i - 1];
50+
} else {
51+
gcd = gcd(gcdLtoR[i - 1], gcdRtoL[i + 1]);
52+
}
53+
if (nums[i] % gcd != 0 && answer < gcd){
54+
answer = gcd;
55+
K = nums[i];
56+
}
57+
}
58+
59+
if (answer == 0) System.out.println(-1);
60+
else System.out.println(answer + " " + K);
61+
}
62+
63+
static int gcd(int a, int b) {
64+
while (b != 0) {
65+
int r = a % b;
66+
a = b;
67+
b = r;
68+
}
69+
return a;
70+
}
71+
}

src/GCD/P14476/input.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
5
2+
8 12 20 32 36

0 commit comments

Comments
 (0)