금세 떨어진 벚꽃을 아쉬워하듯 진달래, 개나리가 만개했다. 피어난 꽃에 심술이 났는지 미세먼지가 심각했다. 다행히 금방 찾아온 비구름에 다 씻겨나가 선선한 주말을 보낼 수 있었다. 언뜻언뜻 꽃향기가 스치는 4월 3번째 주를 되돌아본다.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
Solution s = new Solution();
System.out.println(s.solution(getInput(br)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static int[] getInput(BufferedReader br) throws IOException {
int len = Integer.parseInt(br.readLine());
int[] arr = new int[len];
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
return arr;
}
}
class Solution{
public int solution(int[] arr) {
int answer = Integer.MAX_VALUE;
int lp = 0;
int rp = arr.length - 1;
while (lp < rp) {
if (Math.abs(answer) >= Math.abs(arr[lp] + arr[rp])) {
answer = arr[lp] + arr[rp];
}
if(arr[lp] + arr[rp] < 0) lp++;
else rp--;
}
return answer;
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
Solution s = new Solution();
System.out.println(s.solution(getInput(br)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static int getInput(BufferedReader br) throws IOException{
return Integer.parseInt(br.readLine());
}
}
class Solution{
public int solution(int target) {
if(target <= 1) return 0;
Siever siever = new Siever(target);
int[] field = siever.getPrimeNumbers();
int answer = 0;
int curValue = 0;
int lp = 0;
int rp = 0;
while(lp <= rp && rp < field.length){
if (curValue < target) {
curValue += field[rp++];
} else {
curValue -= field[lp++];
}
if (curValue == target) {
answer++;
curValue -= field[lp++];
}
}
while (lp < field.length) {
if(curValue == target) answer++;
curValue -= field[lp++];
}
return answer;
}
private class Siever{
int target;
boolean[] primeField;
int numOfPrimeNumbers = 0;
public Siever(int num){
this.target = num;
this.primeField = new boolean[num + 1];
Arrays.fill(primeField, true);
}
public int[] getPrimeNumbers(){
sieve();
int[] primeNumbers = new int[this.numOfPrimeNumbers];
int idx = 0;
for (int i = 2; i < this.primeField.length; i++) {
if (this.primeField[i]) {
primeNumbers[idx++] = i;
}
}
return primeNumbers;
}
private void sieve(){
for (int i = 2; i < this.primeField.length; i++) {
if(this.primeField[i]){
this.numOfPrimeNumbers++;
for (int j = i + i; j < this.primeField.length; j += i) {
this.primeField[j] = false;
}
}
}
}
}
}