File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
java/inflearn/problemsolving Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ package inflearn .problemsolving ;
2+
3+ import java .util .Scanner ;
4+
5+ public class P08_합이같은부분집합_DFS {
6+
7+ static int n , total = 0 ;
8+ static String answer = "NO" ;
9+ static boolean flag = false ;
10+
11+ public static void DFS (int d , int sum , int [] arr ) {
12+ if (flag ) return ;
13+ if (sum > total /2 ) return ;
14+ if (d == n ) {
15+ if ((total - sum ) == sum ) {
16+ answer = "YES" ;
17+ flag = true ;
18+ }
19+ } else {
20+ DFS (d +1 , sum + arr [d ], arr );
21+ DFS (d +1 , sum , arr );
22+ }
23+ }
24+
25+ public static void main (String [] args ) {
26+ // 6
27+ // 1 3 5 6 7 10 => YES
28+ Scanner in = new Scanner (System .in );
29+ n = in .nextInt ();
30+
31+ int [] arr = new int [n ];
32+ for (int i = 0 ; i < n ; i ++) {
33+ arr [i ] = in .nextInt ();
34+ total += arr [i ];
35+ }
36+ DFS (0 , 0 , arr );
37+ System .out .println (answer );
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments