알고리즘

<백준> 3273번 자바 알고리즘 [투 포인터]

changha. 2022. 4. 13. 22:51
import java.util.*;

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