### Rob and Mars Problem with complete solution

Rob has gone to mars to collect some stones. The bag he is carrying can hold a maximum 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 elements array containing the list of the weights of the common stones.

**Output Specification:**

**Example1:**

**input1:** 10

**input2:** 3

input3:{1,3,5}

arr2=[1,2,3,4,5,6,7,8,9,10]

arr1=[1,3,5]

arr3=[2,4,6,7,8,9,10]

2,4 2,6 2,7, 2,8 4,6

**output: 2**

**Explanation:**

Rob collects one of the following combination of weight sets: (2,4),(2,6),(2,8)

**Example2:**

Input:14 = M

Input2:4 = N

Input3:{4,6,8, 9}

[1,2,3,4,5,6,7,8,9,10,11,12,13,14]

[4,6,8,9]

[1,2,3,5,7,10,11,12,13,14] (M-N)

(1,2,3,5) (1,2,3,7)

**Output:** 4

#### **Approach for solution **

Here Rob has gone to mars for a mission, and he has got a Bag with certain capacity. While returning to earth rob has to bring stone samples from mars for research purpose. And it is given that mars containes stones with different weight also there are some stones which are already present on earth in terms of weight. The condition says that rob has to bring the stones which dosen’t exists on earth already. One more condition says that rob has to bring maximum number of unique stones with utilizing maximum capacity of the Bag. so finally we need to find how many unique stones can br brought by rob at any moment provoided different conditions.

#### **STEPS FOR SOLUTION (Below code follows these steps) **

**a)** Know what is the total capacity of robs bag

**b)** What are the different weights present on mars

**c)** what are the different weights already present on earth

**d)** create an array for both weights ie weights present on mars and weights present on earth.

**e)** Once arrays are created as we need only the unique weights, substract or remove the weights which are present on earth from the weights present on mars. so that we will be left with only unique weights

**f)** No in final step we need to** utilize maximum capacity** and also we need to bring **maximum number of stones** from the mars so, **SORT** the unique array in ascending order.

**g)** Once we get the sorted array the run the loop on array until total weight is <= total capacity of the bag. Simultaniously keep a counter to find how many stones have been selected by rob.

**h)** so at the end we will be returning an integer indicating how many stones are selected by **ROB**.

#### **JAVA CODE **

// This is the java code for Rob and Mars Problem, where we need to find how many stones can be brought from mars following all the conditions provoided in the question mentioned above

<strong>import java.util.Arrays; import java.util.Scanner; public class marsstoneproblem { // This is a mars problem number of stones rob can bring from mars public static void main(String[] args) { int m=10; int n=3; int sum=0; int counter1=0; int[] arr1={1,3,5}; int arr2[]=new int[m]; int arr3[]=new int[m-n]; for(int i=0;i<m;i++){ arr2[i]=i+1; } System.out.println(Arrays.toString(arr2)); int k=0; int counter=0; // to remove duplicate elements from both the arrays for (int i=0;i<arr2.length;i++){ counter=0; for(int j=0;j<arr1.length;j++){ if(arr2[i]==arr1[j]){ break; } else{ counter=counter+1; } } if(counter>=n){ arr3[k]=arr2[i]; k=k+1; } } System.out.println(Arrays.toString(arr3)); for( int i=0;i<arr3.length;i++){ if(sum+arr3[i]<=m){ sum=sum+arr3[i]; counter1=counter1+1; } else{ break; } } System.out.println("The possible number of stones which can be brought from mars are"); System.out.println( counter1); } } </strong>

**Also Read **

Wipro Elite Coding Question Solved

Crack TCS Coding Round By solving These Questions

Sasken Technologies off campus coding question

**Get step by step explanation for above code on our channel **

DattatreyGood information sir

SuhassiniGood explanation sir,

Thank u

shreebenki

Pingback: Xoriant off campus coding question and solution - Is It Actually

Pingback: Count Buildings Hacker Earth coding question with solution - Is It Actually

Pingback: SHL off campus coding Questions - Is It Actually

VinuWant code in cpp