Mars Stone Problem Description Rob has gone to Mars to collect some stones. The nag he is carrying can hold a mximum weight of M. There are M stones weighing from 1 to M in Mars. There are N stones on Mars that are similar to the ones on Earth. Find the number of stones he can bring from Mars such that none of them are similar to any stone on Earth. Input Specification: input1: M, denoting the size of the bag and the number of different stone weights present on Mars. input2: N, denoting the number of common stones on Earth and Mars. input3: An N element array containing the list of the weights of the common stones. Output Specification: Your function should return the maximum unique stones that can be collected from Mars. Example 1: input1: 10 input2: 3 input3: {1, 3, 5} Output: 2 Explanation: Rob collects one of the following stone weight sets: (2, 4), (2, 6) or (2, 8).
Answers
Answered by
54
Answer:
import java.util.ArrayList;
import java.util.List;
public class Mars {
public static void main(String[] args) {
int i1=12;
int i2=4;
int i3[]= {1,5,7,9};
System.out.print(cal(i1,i2,i3));
}
private static int cal(int i1, int i2, int[] i3) {
int sum=i1;
int max=Integer.MIN_VALUE;
List<Integer> l=new ArrayList<>();
for(int i=0;i<i3.length;i++)
{
l.add(i3[i]);
}
for(int i=1;i<=i1;i++)
{
sum=sum-i;
int count=0;
if(!l.contains(i))
{
for(int j=i+1;j<=i1;j++)
{
if(!l.contains(j) && (sum-j)>=0)
{
sum=sum-j;
count++;
}
}
max=Math.max(count, max);
}
}
return max+1;
}
}
Explanation:
Similar questions
Math,
4 months ago
Science,
4 months ago
English,
4 months ago
Computer Science,
8 months ago
Chemistry,
1 year ago