**CML01- FUN IN MATHS**

**Our chefs from CodeChef kitchen, are excited to present the first item. We all have some memories of school life. Also, many of us have some fun in Math class. All of our school life has gone through X, Y, theta, beta etc. Let’s try to solve simple math by remembering that X and Y. You are given two positive Integer X and Y. You have to find the Yth number which is not divisible by X.**

**Example** : - X=4, Y=5

**All the numbers that are not divisible by 4 is 1 , 2 , 3 , 5 , 6 , 7 , 9 , 10 , 11 , 13………… . . … . … . .**

**So the 5th ( Yth ) number is 6**

**Input** : First line X. Second Line Y.

**Output** : Print Yth number which is not divisible by X

**Note** : The value of X is greater than 1 because every number is divisible by 1.

**Sample Input** :

**4**

**5**

**Sample Output** :

**6**

**Explanation** :

**X=4, Y=5**

**All the numbers that are not divisible by 4 is 1 , 2 , 3 , 5 , 6 , 7 , 9 , 10 , 11 , 13………… . . … . … . .**

**So the 5th(Yth) number is 6**

SOLUTION(CML01):

import java.util.*;
import java.lang.*

*;*

/* Name of the class has to be “Main” only if the class is public. */

class Codechef

{

public static void main (String[] args)

{

Scanner sc=new Scanner(System.in);

int x=sc.nextInt();

int y=sc.nextInt();

int c=0;

int i=0;

while(c!=y) {

i++;

if((i%x)!=0)

c++;

}

System.out.println(i);

```
}
```

}

**CML02- HELP ANSH**

**Ansh is the topper of the class. He solves all the challenges very easily. He is everyone’s favourite especially girls. One day Ansh was discussing a Math problem with Mehak. our Chef wants you guys to help Ansh and also to warm up ur Brain with this problem. You are given an array. You have to check whether there is two number present in an array whose sum is 10.**

**Input** :

**N [Size of array]**

**N separated Integers A0, A1,……A(N-1)**

**Output** :

**If two numbers are present then**

**return True**

**else return False**

**Sample Input** :

**5**

**1 2 3 7 8**

**Sample Output** :

**True**

**Explanation** :

**7+3 =10**

**SOLUTION(CML02):**

import java.util.*;
import java.lang.*;

/* Name of the class has to be “Main” only if the class is public. */

class Codechef

{

public static void main (String[] args) throws java.lang.Exception

{

Scanner sc = new Scanner(System.in);

int N = sc.nextInt();

int[] Ar = new int[N];

for(int i=0;i<N;i++){

Ar[i] = sc.nextInt();

}

for(int i=0;i<N-1;i++){

for(int j=i+1;j<N;j++){

if(Ar[i]+Ar[j]==10){

System.out.println(“True”);

System.exit(0);

}

}

}

System.out.println(“False”);

}

}

**CML03- MINIMUM STEPS**

**Jay and Ved are playing a game called Quick-Finder. There are N rooms in a housing array (Room number [0,1,2,……,n-1]). Ved will hide in one room and Jay will wait in another room till Ved hides. After that Jay will start finding Ved. He must move through one room at a time, either moving left or right. The House array is circular, so when the last room in the house array is reached in either direction, the next room is at the other end of the house array. But the catch is that our chef likes Jay, so he will tell Jay the room number of Ved but the condition is that Jay has to reach Ved’s room in the minimum number of left or right moves.**

**Input** :

**Given the number of rooms in a housing array- N (N >0)**

**You are given Jay’s starting room number – K (0<=K<=N-1)**

**you are given Ved’s room number - V (0<=V<=N-1)**

**Output** :

**Determine the minimum number of left or right moves to reach ved’s room. In other words , a minimum number of step to reach from K to V.**

**Sample Input** :

**5**

**1**

**4**

**Sample Output** :

**2**

**Explanation** :

**There are 5 rooms in a house array, starting from 0 to 4. Jay is currently in Room number 1. He has to go to room number 4 of the house array. So if he starts his journey from the right side then the pattern he will follow … 1 → 2 → 3-> 4. He has to take 3 steps. But if he starts his journey from the left side thEN he has to follow 1 → 0 → 4, So it requires only 2 steps. So Minimum from both left and the right side is left move of 2 Step.**

**Solution:**

/* package codechef; // don’t place package name! */

import java.util.*;
import java.lang.*;

/* Name of the class has to be “Main” only if the class is public. */

class Codechef

{

public static void main (String[] args) throws java.lang.Exception

{

Scanner sc = new Scanner(System.in);

//System.out.println(“Enter the number of rooms:”);

int n=sc.nextInt();

//System.out.println(“Enter Jay’s starting room number:”);

int j = sc.nextInt();

//System.out.println(“Enter Ved’s starting room number”);

int v = sc.nextInt();

int []arr = new int[n];

for(int i=0;i<n;i++){

if(i==v)

arr[i]=v;

}

for(int i=0;i<n;i++){

if(i==j)

arr[i]=j;

}

int a=0,b=0;

a=arr[v]-arr[j];

b=arr[j]-arr[v];

if(a<b){

if(Math.abs(b)>Math.abs((n-b)))

System.out.println(Math.abs(n-b));

else

System.out.println(Math.abs(b));

```
}
else{
if(Math.abs(a)>Math.abs((n-a)))
System.out.println(Math.abs(n-a));
else
System.out.println(Math.abs(a));
}
}
```

}

**CML04- STUDENT n PROFFESOR**

**The next session in a college requires two lessons to be taught. The i-th lesson is interesting by pi units for the professor and by qi units for the students.**

**### The pair of lessons i and j (i’<'j) is called great if pi+pj ‘>’ qi+qj (i.e. it is more interesting for the professor).**

**Your task is to find the number of great pairs of lessons.**

**Input** :

**The first line of the input contains one integer n (2 ≤n≤ 2⋅10^5) — the number of lessons.**

**The second line of the input contains n integers a1,a2,…,an (1≤ pi ≤10^9), where pi is the interestingness of the i-th lesson for the professor.**

**The third line of the input contains n integers b1,b2,…,bn (1≤qi≤10^9), where qi is the interestingness of the i-th lesson for the students.**

**Output** :

**Print one integer — the number of great pairs of lesson.**

**Examples** :

**Input** :

**5**

**4 8 2 6 2**

**4 5 4 1 3**

**Output** :

**7**

**Solution:**

/* package codechef; // don’t place package name! */

import java.util.*;
import java.lang.*;

/* Name of the class has to be “Main” only if the class is public. */

class Codechef

{

public static void main (String[] args) throws java.lang.Exception

{

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int[] l2 = new int[n];

for (int i = 0; i < n; i++)

l2[i] = sc.nextInt();

int[] l3 = new int[n];

for (int i = 0; i < n; i++)

l3[i] = sc.nextInt();

// Arrays.sort(l2);

// Arrays.sort(l3);

int count=0;

for(int i=0;i<n;i++)

{

for(int j=i+1;j<n;j++)

{

int prof=l2[i]+l2[j];

int stud=l3[i]+l3[j];

if(prof>stud)

{

count++;

}

}

}

System.out.println(count);

```
}
```

}

**CML05- JEWEL ROBBERY**

**A boy name called “bahu” is very lonely and unable to eat food so he has gone for a jewellery robbery and he has a limitation to rob the jewellery he robs until his bag is full and then he escapes. And the bank has different items gold, silver, platinum, diamond etc…. each item has a certain cost or price and it also has a weight.**

**Now finally he robs according to the weight of his bag he can’t rob more than that and he needs more profit in such a manner he will rob and one fine day of his robbery he went to a jewellery shop and he decided to sell them and get profit based on their weight such that his profit should be maximum enough.**

**NOTE: he robs until his bag gets full enough of jewellers or a minimum of that he cant carry more than that, which means (<=) he follows that condition (< or =) but his profit should be maximum enough.**

**INPUT** :

**Number of items : (N)**

**BAG WEIGHT : (w)**

**PROFIT Array [ ]**

**WEIGHT Array [ ]**

**OUTPUT** :

**Profit**

**Example** :

**INPUT** :

**5**

**8**

**0 1 2 5 6**

**0 2 3 4 5**

**OUTPUT** :

**8**

**Explanation** :

**Number of items(n) :n=5**

**BAG WEIGHT : (8KG’S)**

**PROFIT : 0 1 2 5 6**

**WEIGHT : 0 2 3 4 5**

**His bag weight is 8kgs which means he should go for the items(weights) which are more than 8 because his main motto is just to fill the bag or tries to maximize the bag 1st rob on weight 5**

**2nd rob on weight 3 Weight5+ weight 3 =profit(6+2) = 8 profit**

**Example2** :

**INPUT** :

**7**

**15**

**10 5 15 7 6 18 3**

**2 3 5 7 1 4 1**

**OUTPUT** :

**54**

**Explanation** :

**Number of items : N=7**

**BAG WEIGHT : (15 KG’S)**

**PROFIT : 10 5 15 7 6 18 3**

**WEIGHT: 2 3 5 7 1 4 1**

**1st rob on weight 4 ( now,he can rob upto 15-4=11 and he earned 18)**

**2nd rob on weight 5(now, he can rob upto 11-5=6 and he earned 18+the profit on 5 is (15)= 18+15=33)**

**3rd rob on weight 2(now,he can rob upto 6-2=4 and he earned 18+15+the profit on weight 2 is (10)=18+15+10=43)**

**4th rob on weight 3 now,he can rob upto 4-3=1 and he earned 18+15+10+the profit on 3 is(5)=48)**

**5th rob on weight 1 now,he can rob upto 1-1=0 and he earned 18+15+10+ 5+the profit on 1 is(6)=54)**

**SOLUTION-**

import java.util.*;
import java.lang.*;

/* Name of the class has to be “Main” only if the class is public. */

class Codechef{

```
static int function(int n,int w,int profit[],int weight[])
{
int arr[][] = new int[n + 1][w+ 1];
for (int i = 0; i<= n; i++) {
for (int j = 0; j<= w; j++) {
if (i == 0 || j == 0)
arr[i][j] = 0;
else if (weight[i - 1]<= j)
arr[i][j] = Math.max(profit[i - 1] + arr[i - 1][j - weight[i - 1]], arr[i - 1][j]);
else
arr[i][j] = arr[i - 1][j];
}
}
return arr[n][w];
}
public static void main(String args[])throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int w=sc.nextInt();
int profit[] = new int[n];
for(int i=0;i<n;i++){
profit[i]=sc.nextInt();
}
int weight[] = new int[n];
for(int i=0;i<n;i++){
weight[i]=sc.nextInt();
}
System.out.println(function(n,w, profit,weight));
} }
```