공부기록/백준

[백준] 3273번 두 수의 합

메델 2023. 12. 13. 21:46
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    	Scanner kb = new Scanner(System.in);
    	
    	int n = kb.nextInt();
    	int[] arr = new int[n];
    	
    	for(int i=0; i<n; i++) {
    		arr[i] = kb.nextInt();
    	}
    	
    	int x = kb.nextInt();
    	
    	Arrays.sort(arr);
    	
    	int lt=0, rt = n-1;
    	int count = 0;
    	
    	while(lt<rt) {
    		int sum = arr[lt] + arr[rt];
    		
    		if(sum> x) {
    			rt--;
    		}else if (sum <x) {
    			lt++;
    		}else {
    			count++;
    			lt++;
    			rt--;
    		}
    	}
    	
    	System.out.println(count);

        

    }
}

'공부기록 > 백준' 카테고리의 다른 글

[백준] 5800번 성적 통계  (0) 2023.12.17
[백준] 2023번 수들의 합 2  (0) 2023.12.15
[백준] 2164번 카드2  (0) 2023.12.13
[백준] 1157번 단어 공부  (0) 2023.12.13
[백준] 1253번 좋다  (0) 2023.12.13