인프런 커뮤니티 질문&답변
코드 확인 부탁드립니다.
해결된 질문
작성
·
321
0
package inflearn.ch03;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
5. 연속된 자연수의 합(투 포인터)
 */
public class Problem_5 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int[] array = new int[N];
        for(int i = 0; i < N; i++){
            array[i] = i+1;
        }
        solution(N,array);
    }
    public static void solution(int N, int[] array){
        int start = 0;
        int end = 0;
        int sum = 0;
        int count = 0;
        while (end < N-1){
            sum += array[end++];
            if(sum == N){
                count++;
            }
            while (sum >= N){
                sum -= array[start++];
                if(sum == N){
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}강의와 다르게 범위를 줄이지 않고 while 문으로 사용해서 정답 통과를 했는데 나중에 범위가 커지면 문제가 발생할 수 있을까요?






답변 감사드립니다.