You are on page 1of 7

Name: D.

SreePreetham Reddy

Pin No.: 222010326059

Class: CSDS

1) A chocolate factory is packing chocolates into the packets. The chocolate packets
here represent an array arrt of N number of integer values. The task is to find the
empty packets(0) of chocolate and push it to the end of the conveyor belt(array).

For Example:

 N=7 and arr = [4,5,0,1.9,0,5,0].


 There are 3 empty packets in the given set. These 3 empty packets
represented as O should be pushed towards the end of the array

Example 1:

Input:

 7  – Value of N
 [4,5,0,1,0,0,5] – Element of arr[O] to arr[N-1],While input each element is
separated by newline

Output:

 4519500

Example 2:

Input:

 6
 — Value of N.
 [6,0,1,8,0,2] – Element of arr[0] to arr[N-1], While input each element is
separated by newline

Output:

 618200
Solution:
import java.util.*;

class Solution

public static void main (String[]args)

Scanner sc = new Scanner (System.in);

int n = sc.nextInt ();

int arr[] = new int[n];

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

arr[i] = sc.nextInt ();

int count = 0;

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

if (arr[i] != 0)

arr[count++] = arr[i];

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

arr[i] = 0;

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

System.out.print (arr[i] + " ");

2) Mr. Rao is relocating from place A to B. The moving truck has a maximum
capacity C. There are ‘N’ items in the house where each item has a corresponding
value (Vi) and weight(Wi). Mr. Rao has to carry only the most valuable items whose
total weight does not exceed the capacity of truck. The task here is to find those
items (single or combination of items) whose total value (v) will be the maximum and
their corresponding weight(w) will not exceed truck capacity(c). Here,

 N= No. of items
 C= Maximum capacity of the truck, an integer value,
 W[0 to N-1]- An array consisting weight of each item
 V[0 to N-1] – An array consisting value of each item.

Example 1:

Input :

 4  -> Value of N
 80 -> Value of C
 [10,45,60,90] -> Elements of array v[], where each element is separated by
new line.
 [15,20,30,40] -> Elements of array  w[], where each element is separated by
new line.

Output: 150

Explanation:

 Value=10 weight=15
 Value=45 weight = 20
 Value = 60 weight=30
 Value=90 weight=40

The subsets that can  be formed from the array V[], their corresponding weight W[]
and comparison with c=80

Va

Value Total Value Weight Total Weight Inv


10+45 55 15+20 35 Valid
10+60 70 15+30 45 Valid
10+90 100 15+40 55 Valid
45+60 105 20+30 50 Valid
45+90 135 20+40 60 Valid
60+90 150 20+40 70 Valid
10+45+60 115 20+40 65 Valid
10+60+90 160 20+40 85 Invalid
Va

Value Total Value Weight Total Weight Inv


10+45+90 145 20+40 75 Valid
 

From the above table, it is perceived that particularly for the valid items, maximum
value=150 and their corresponding weight is 70. So the output should be 150.

The input format for testing

First Input – Accept value for N (positive integer number). 

Second input : Accept value for C (Positive integer number),

Third input – Accept N number of positive integer number for array v[0…N-1], where
each value is separated by a new line.

Fourth input – Accept N positive integer numbers for array [0….N-1], where each
value is separated by a new line.

The output format for testing

The output should be a positive integer number (Check the output in Example 1)

Constraints:

 0<=N<=10
 0 <= C<=500
 1<=V[i]<=500

Solution:
import java.util.*;

class Solution

public static int solve(int c,int w[],int val[],int n)

{
if(n==0 || c==0)

return 0;

if(c<w[n-1])

return solve(c,w,val,n-1);

else

return Math.max(solve(c,w,val,n-1),val[n-1]+solve(c-w[n-1],w,val,n-1));

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

int c=sc.nextInt();

int v[]=new int[n];

int w[]=new int[n];

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

v[i]=sc.nextInt();

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

w[i]=sc.nextInt();

System.out.println(solve(c,w,v,n));

3) One programming language has the following keywords that cannot be used as
identifiers:

break, case, continue, default, defer, else, for, func, goto, if, map, range, return,
struct, type, var

Write a program to find if the given word is a keyword or not

Test cases
Case 1

 Input – defer
 Expected Output – defer is a keyword

Case 2

 Input – While
 Expected Output – while is not a keyword

Solution:

import java.util.Scanner;

public class Prep

public static void main(String args[])

String str[]= {"break", "case", "continue", "default", "defer", "else","for",


"func", "goto",

"if", "map", "range", "return", "struct", "type", "var"};

int flag = 0;

Scanner sc = new Scanner(System.in);

String input=sc.nextLine();

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

if(str[i].equals(input)){

flag = 1;

break;

}
if(flag==1){

System.out.println(input+" is a keyword");

else{

System.out.println(input+" is not a keyword");

You might also like