AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Mystery Temple

There is a mysterious temple in Ignite. The door of the temple is always closed. It can only be opened by a unique procedure. There are two boxes and N items outside the temple. Srini visits the temple many times. Each time Srini visits the temple, the number of items N outside the door of the temple is changed but each time he anyhow manages to know the cost of those N items. The door of the temple can only be opened if those "N items" are distributed in those two boxes such that the sum of cost of items in one box is equal to the sum of cost of items in other box. Srini is trying to do such a distribution so as to open the door of the temple. You have to help him with the distribution so as to open the door of the temple.
Input
The first line contain the number of T test cases i.e the number of time Srini visits the temple. Next lines contains the description of those test cases. For the first line contain number of items "N". The second line contains cost of those N items.
Output
?Yes? if he is able to open the door,?No? if he is not able to open the door.
Constraints
1 <= T <= 10
1 <= N <= 100

Sample Input:
3
5
4 3 5 5 3
4
1 2 3 4
2
1 2
Sample Output:
Case #1: Yes
Case #2: Yes
Case #3: No

corejava x 336
programming x 153
Posted On : 2016-11-15 12:24:01.0
profile Boopalan - anyforum.in Boopalan
3730
up-rate
5
down-rate

Answers


Please check below code:

import java.util.Scanner;

public class MysteryTemple {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int T=input.nextInt();
int N=0;
Integer[] items;
int sum=0;
int item=0;
if(1 <= T && T <= 10 ){
for(int i=1;i<=T;i++){
N=input.nextInt();
if(1 <= N && N<= 100){
items=new Integer[N];
sum=0;
for(int j=1;j<=N;j++){
item=input.nextInt();
items[j-1]=item;
sum=sum+item;
}
System.out.println("Case #"+i+": "+(findMinRec(items,items.length,0,sum)==0?"Yes":"No"));
}else{
System.out.println("Invalid Input");
}
}
}else{
System.out.println("Invalid Input");
}
}

public static int findMinRec(Integer arr[], int i, int sumCalculated, int sumTotal)
{
// If we have reached last element. Sum of one
// subset is sumCalculated, sum of other subset is
// sumTotal-sumCalculated. Return absolute difference
// of two sums.
if (i==0)
return Math.abs((sumTotal-sumCalculated) - sumCalculated);


// For every item arr[i], we have two choices
// (1) We do not include it first set
// (2) We include it in first set
// We return minimum of two choices
return Math.min(findMinRec(arr, i-1, sumCalculated+arr[i-1], sumTotal),
findMinRec(arr, i-1, sumCalculated, sumTotal));
}

// Returns minimum possible difference between sums
// of two subsets
int findMin(Integer arr[], int n)
{
// Compute total sum of elements
int sumTotal = 0;
for (int i=0; i<n; i++)
sumTotal += arr[i];

// Compute result using recursive function
return findMinRec(arr, n, 0, sumTotal);
}
}

Note:
------------------------
while providing input paste all inputs together and put one blank line at last like below
3
5
4 3 5 5 3
4
1 2 3 4
2
1 2
.........(Blank line)

Posted On : 2016-11-18 00:53:26
Satisfied : 2 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185319357
Reply This Thread
up-rate
3
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in