문제
접근 방식
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main_16938 {
static int[] numList;
static int answer;
static int N,L,R,X;
static int[] A;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
L = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
X = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
A = new int[N];
for(int i=0;i<N;i++) {
A[i] = Integer.parseInt(st.nextToken());
}
answer = 0;
for(int i=2;i<=N;i++) {
numList = new int[i];
combi(0,0,i);
}
System.out.println(answer);
}
public static void combi(int start, int cnt, int target) {
if(cnt == target) {
if(isSumBetweenLR() && isDiffBiggerThanX()) answer++;
return;
}
for(int i=start;i<N;i++) {
numList[cnt] = i;
combi(i+1,cnt+1,target);
}
}
public static boolean isSumBetweenLR() {
int sum = 0;
for(int i=0;i<numList.length;i++) {
sum+=A[numList[i]];
}
if(sum >= L && sum <= R) return true;
return false;
}
public static boolean isDiffBiggerThanX() {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i=0;i<numList.length;i++) {
if(min > A[numList[i]]) min = A[numList[i]];
if(max < A[numList[i]]) max = A[numList[i]];
}
if(max - min >=X) return true;
return false;
}
}