Coding Questions
Coding Questions
Problem Statement –
Input :
200 -> Value of V
540 -> Value of W Output : TW =130 FW=70
Explanation:
130+70 = 200 vehicles
(70*4)+(130*2)= 540 wheels
Constraints :
● 2<=W
● W%2=0
● V<W
Code:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int v=sc.nextInt();
int w=sc.nextInt();
float res=((4*v)-w)/2;
if(w>=2 && (w%2==0) && v<w)
System.out.println("TW= "+(int)(res)+" FW= "+(int)(v-res));
else
System.out.println("INVALID INPUT");
}
}
0
2. Problem Statement –
Given a string S(input consisting) of ‘*’ and ‘#’. The length of the string is
variable. The task is to find the minimum number of ‘*’ or ‘#’ to make it a
valid string. The string is considered valid if the number of ‘*’ and ‘#’ are
equal. The ‘*’ and ‘#’ can be at any position in the string.
Note : The output will be a positive or negative integer based on number
of ‘*’ and ‘#’ in the input string.
Example 1:
Input 1:
Output :
Code:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
String str="Hello";
int count1=0,count2=0;
for(int i=0;i< str.length();i++)
{
if(str.charAt(i)=='*')
count1++;##
else if(str.charAt(i)=='#')
count2++;
}
System.out.println(count1-count2);
}
}
3. Problem Statement:
Given an integer array Arr of size N the task is to find the count of
elements whose value is greater than all of its prior elements.
Note : 1st element of the array should be considered in the count of the
result.
For example,
Arr[]={7,4,8,2,9}
As 7 is the first element, it will consider in the result.
8 and 9 are also the elements that are greater than all of its previous
elements.
Since total of 3 elements is present in the array that meets the
condition.
Hence the output = 3.
Example 1:
Input
5 -> Value of N, represents size of Arr
7-> Value of Arr[0]
4 -> Value of Arr[1]
8-> Value of Arr[2]
2-> Value of Arr[3]
9-> Value of Arr[4]
Output :
3
Example 2:
5 -> Value of N, represents size of Arr
3 -> Value of Arr[0]
4 -> Value of Arr[1]
5 -> Value of Arr[2]
8 -> Value of Arr[3]
9 -> Value of Arr[4]
Output :
5
Constraints
● 1<=N<=20
● 1<=Arr[i]<=10000
Code:
import java.util.*;
class Main
{
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;imax)
{
max=arr[i];
count++;
}
}
System.out.println(count);
}
}
4. Problem Statement:
A parking lot in a mall has RxC number of parking spaces. Each parking
space will either be empty(0) or full(1). The status (0/1) of a parking
space is represented as the element of the matrix. The task is to find
index of the prpeinzta row(R) in the parking lot that has the most of the
parking spaces full(1).
Note :
RxC- Size of the matrix
Elements of the matrix M should be only 0 or 1.
Example 1:
Input :
3 -> Value of R(row)
3 -> value of C(column)
[0 1 0 1 1 0 1 1 1] -> Elements of the array M[R][C] where each element
is separated by new line.
Output :
3 -> Row 3 has maximum number of 1’s
Example 2:
input :
4 -> Value of R(row)
3 -> Value of C(column)
[0 1 0 1 1 0 1 0 1 1 1 1] -> Elements of the array M[R][C]
Output :
4 -> Row 4 has maximum number of 1’s
Code:
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int row=sc.nextInt();
int col=sc.nextInt();
int arr[][]=new int[row][col];
for(int i=0;i< row;i++)
for(int j=0;j< col;j++)
arr[i][j]=sc.nextInt();
int max=0,count=0,index=0;
for(int i=0;i< row;i++)
{
count=0;
for(int j=0;j< col;j++)
{
if(arr[i][j]==1)
count++;
}
if(count>max)
{
max=count;
index=i+1;
}
}
System.out.println(index);
}
}
5. Problem Statement:
A party has been organised on cruise. The party is organised for a
limited time(T). The number of guests entering (E[i]) and leaving (L[i]) the
party at every hour is represented as elements of the array. The task is
to find the maximum number of guests present on the cruise at any
given instance within T hours.
Example 1:
Input :
● 5 -> Value of T
● [7,0,5,1,3] -> E[], Element of E[0] to E[N-1], where input each
element is separated by new line
● [1,2,1,3,4] -> L[], Element of L[0] to L[N-1], while input each
element is separate by new line.
Output :
8 -> Maximum number of guests on cruise at an instance.
Explanation:
1st hour:
Entry : 7 Exit: 1
No. of guests on ship : 6
2nd hour :
Entry : 0 Exit : 2
No. of guests on ship : 6-2=4
Hour 3:
Entry: 5 Exit: 1
No. of guests on ship : 4+5-1=8
Hour 4:
Entry : 1 Exit : 3
No. of guests on ship : 8+1-3=6
Hour 5:
Entry : 3 Exit: 4
No. of guests on ship: 6+3-4=5
Hence, the maximum number of guests within 5 hours is 8.
Example 2:
Input:
4 -> Value of T
[3,5,2,0] -> E[], Element of E[0] to E[N-1], where input each element is
separated by new line.
[0,2,4,4] -> L[], Element of L[0] to L[N-1], while input each element in
separated by new line
Output:
6
Cruise at an instance
Explanation:
Hour 1:
Entry: 3 Exit: 0
No. of guests on ship: 3
Hour 2:
Entry : 5 Exit : 2
No. of guest on ship: 3+5-2=6
Hour 3:
Entry : 2 Exit: 4
No. of guests on ship: 6+2-4= 4
Hour 4:
Entry: 0 Exit : 4
No. of guests on ship : 4+0-4=0
Constraints:
● 1<=T<=25
● 0<= E[i] <=500
● 0<= L[i] <=500
Code:
import java.util.*;
class Main
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int t = sc.nextInt ();
int e[] = new int[t];
int l[] = new int[t];
for (int i = 0; i < t; i++)
e[i] = sc.nextInt ();
6. Problem Statement:
At a fun fair, a street vendor is selling different colours of balloons. He
sells N number of different colours of balloons (B[]). The task is to find
the colour (odd) of the balloon which is present odd number of times in
the bunch of balloons.
Note: If there is more than one colour which is odd in number, then the
first colour in the array which is present odd number of times is
displayed. The colours of the balloons can all be either upper case or
lower case in the array. If all the inputs are even in number, display the
message “All are even”.
Example 1:
● 7 -> Value of N
● [r,g,b,b,g,y,y] -> B[] Elements B[0] to B[N-1], where each input
element is sepārated by ṉew line.
Output :
● r: 1 balloon
● g: 2 balloons
● b: 2 balloons
● y : 2 balloons
Hence , r is only the balloon which is odd in number.
Example 2:
Input:
● 10 -> Value of N
● [a,b,b,b,c,c,c,a,f,c] -> B[], elements B[0] to B[N-1] where input each
element is separated by new line.
Output :
b-> ‘b’ colour balloon is present odd number of times in the bunch.
Explanation:
From the input array above:
● a: 2 balloons
● b: 3 balloons
● c: 4 balloons
● f: 1 balloons
Here, both ‘b’ and ‘f’ have odd number of balloons. But ‘b’ colour balloon
occurs first.
Hence , b is the output.
Constraints:
● 3<=N<=50
● B[i]={{a-z} or {A-Z}}
import java.util.HashMap;
import java.util.Map;
Code:
import java.util.*;
class Main
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
char arr[] = new char[n];
for (int i = 0; i < n; i++)
arr[i] = sc.next ().charAt (0);
int lower[] = new int[26];
int upper[] = new int[26];
for (int i = 0; i < n; i++)
{
if ((arr[i] >= 'A') && (arr[i] <= 'Z'))
upper[arr[i] - 'A']++;
else if ((arr[i] >= 'a') && (arr[i] <= 'z'))
lower[arr[i] - 'a']++;
}
boolean flag = false;
char ch = '\0';
for (int i = 0; i < n; i++)
{
if ((arr[i] >= 'A') && (arr[i] <= 'Z'))
{
if (upper[arr[i] - 'A'] % 2 == 1)
{
ch = (char) (arr[i]);
flag = true;
break;
}
}
else if ((arr[i] >= 'a') && (arr[i] <= 'z'))
{
if (lower[arr[i] - 'a'] % 2 == 1)
{
ch = (char) (arr[i]);
flag = true;
break;
}
}
if (flag == true)
System.out.println (ch);
else
System.out.println ("All are even");
}
}
7. Problem Statement:
There is a JAR full of candies for sale at a mall counter. JAR has the
capacity N, that is JAR can contain maximum N candies when JAR is
full. At any point of time. JAR can have M number of Candies where
M<=N. Candies are served to the customers. JAR is never remain empty
as when last k candies are left. JAR if refilled with new candies in such a
way that JAR get full.
Write a code to implement above scenario. Display JAR at counter with
available number of candies. Input should be the number of candies one
customer can order at point of time. Update the JAR after each purchase
and display JAR at Counter.
Input Value
3
Output Value
NUMBER OF CANDIES SOLD : 3
NUMBER OF CANDIES AVAILABLE : 7
Input Value
0
Output Value
INVALID INPUT NUMBER OF
CANDIES LEFT : 10
Code:
import java.util.Scanner;
class Main{
public static void main(String[] args) {
int n = 10, k = 5;
int num;
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
if(num >= 1 && num <= 5) {
System.out.println("NUMBER OF CANDIES SOLD : "
+ num);
System.out.print("NUMBER OF CANDIES LEFT : " +
(n - num));
} else {
System.out.println("INVALID INPUT");
System.out.print("NUMBER OF CANDIES LEFT : " +
n);
}
}
}
8. Problem Statement:
Selection of MPCS exams include a fitness test which is conducted on
ground. There will be a batch of 3 trainees, appearing for running test in
track for 3 rounds. You need to record their oxygen level after every
round. After trainee are finished with all rounds, calculate for each
trainee his average oxygen level over the 3 rounds and select one with
highest oxygen level as the most fit trainee. If more than one trainee
attains the same highest average level, they all need to be selected.
Display the most fit trainee (or trainees) and the highest average oxygen
level.
Note:
Example 1:
INPUT VALUES
95
92
95
92
90
92
90
92
90
OUTPUT VALUES
Trainee Number : 1
Trainee Number : 3
Note:
Input should be 9 integer values representing oxygen levels entered in
order as
Round 1
Round 2
● Oxygen value of trainee 1
● Oxygen value of trainee 2
● Oxygen value of trainee 3
Round 3
Code:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int[][] trainee = new int[3][3];
int[] average = new int[3];
int max = 0;
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
trainee[i][j] = sc.nextInt();
if(trainee[i][j] < 1 || trainee[i][j] > 100) {
trainee[i][j] = 0;
}
}
}
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
average[i] = average[i] + trainee[j][i];
}
average[i] = average[i] / 3;
}
for(int i = 0; i < 3; i++) { if(average[i] > max) {
max = average[i];
}
}
for(int i = 0; i < 3; i++) {
if(average[i] == max) {
System.out.println("Trainee Number : " + (i +
1));
}
if(average[i] <70) {
System.out.print("Trainee is Unfit");
}
}
}
9.
10. `Problem Statement:
A washing machine works on the principle of Fuzzy System, the weight
of clothes put inside it for washing is uncertain But based on weight
measured by sensors, it decides time and water level which can be
changed by menus given on the machine control area.
For low level water, the time estimate is 25 minutes, where
approximately weight is between 2000 grams or any nonzero positive
number below that.
“INVALID INPUT”.
Example:
Input value
2000
Output value
Time Estimated: 25 minutes
Code:
public class WashingMachine {
Example 1:
Enter your PlainText: All the best
Enter the Key: 1
Code:
return encryptedMessage.toString();
}
Take input as
1. Number of Interior walls
2. Number of Exterior walls
3. Surface Area of each Interior 4. Wall in units of square feet
Surface Area of each Exterior Wall in units of s
Code:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int ni, ne, i = 0;
Float cost = 0, temp;
Scanner sc = new Scanner(System.in);
ni = sc.nextInt();
ne = sc.nextInt();
if(ni < 0 || ne < 0) {
System.out.print("INVALID INPUT");
}
else {
for(i = 0; i < ni; i++) {
temp = sc.nextFloat();
cost +=18 * temp;
}
for(i = 0; i < ne; i++) {
temp = sc.nextFloat();
cost += 12 * temp;
}
System.out.printf("Total estimated Cost : %.1f", cost);
}
}
}
Example 1:
Input Values
ca
Ca
Output Values
INVALID OUTPUT
Example 2:
Input Values
NI
HA
Output Values :23.0 INR
Note: Input and Output should be in format given in example.
Input should not be case sensitive and output should be in the format
INR
Code:
import java.util.HashMap;
import java.util.Map;
static {
for (int i = 0; i < BusStops.length; i++) {
stopIndexMap.put(BusStops[i].toUpperCase(), i);
}
}
if (!stopIndexMap.containsKey(Source) || !
stopIndexMap.containsKey(Destination)) {
return "INVALID OUTPUT";
}
int forwardDistance = 0;
int backwardDistance = 0;
// Calculate fare
double fare = Math.ceil(distance / 1000.0) * 5;
Example 1:
Input Values
20
2
3
12
12
Output Values
Number of Monkeys left on the tree:10
Note: Kindly follow the order of inputs as n,k,j,m,p as given in the above
example. And output must include the same format as in above
example(Number of Monkeys left on the Tree:)
For any wrong input display INVALID INPUT
Code:
import java.util.*;
class Monkeys
{
public static void main(String []args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int j = sc.nextInt();
int m = sc.nextInt();
int p = sc.nextInt();
int atebanana=0 ,atepeanut=0;
if( n<0 && k<0 || j<0 || m<0 || p<0)
{
System.out.println("Invalid Input");
}
else
{
if(k>0)
{
atebanana =m/k;
m=m%k;
}
if(j>0)
{
atepeanut = p/j;
p=p%j;
}
n=n-atebanana-atepeanut;
if((m!=0) || (p!=0))
n=n-1;
System.out.println("Number of Monkeys left on the Tree:
"+n);
}
}
}
15. Problem Statement:
Chain Marketing Organization has has a scheme for income generation,
through which its members generate income for themselves. The
scheme is such that suppose A joins the scheme and makes R and V to
join this scheme then A is Parent Member of R and V who are child
Members. When any member joins the scheme then the parent gets
total commission of 10% from each of its child members.
Child members receive commission of 5% respectively. If a Parent
member does not have any member joined under him, then he gets
commission of 5%.
Take name of the members joining the scheme as input.
Display how many members joined the scheme including parent
member.Calculate the Total commission gained by each members in the
scheme. The fixed amount for joining the scheme is Rs.5000 on which
commission will be generated
SchemeAmount = 5000
Code:
import java.util.*;
} else {
// Parent gets 5% if no child members
commissionMap.put(parentMember, SCHEME_AMOUNT
* 0.05);
}
Coffee
1. Espresso Coffee
2. Plain Tea
3. Cappuccino Coffee
4. Latte Coffee
Tea
1. Assam Tea
2. Ginger Tea
3. Cardamom Tea
4. Masala Tea
5. Lemon Tea
6. Green Tea
7. Organic Darjeeling Tea
Soups
Beverages
Write a program to take input for main menu & sub menu and display the
name of sub menu selected in the following format (enter the first letter
to select main menu):
Welcome to CCD
Enjoy your
Example 1:
Input:
c
1
Output
Welcome to CCD!
Enjoy your Espresso Coffee!
Example 2:
Input:
t
9
Output
INVALID OUTPUT!
Code:
import java.util.Scanner;
// Sub-menu selection
int subMenuOption = scanner.nextInt();
String selectedDrink = getSelectedDrink(mainMenuOption,
subMenuOption);
if (selectedDrink.equals("INVALID")) {
System.out.println("INVALID OUTPUT!");
} else {
System.out.println("Welcome to CCD!");
System.out.println("Enjoy your " + selectedDrink + "!");
}
}
Example 1:
Input
20 30 40 50 2 3 14
Output
Total Income 2000 INR
Note: Input and Output Format should be same as given in the above
example.For any wrong input display INVALID INPUT
Output Format
Total Income 2100 INR
Code:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
Explanation:
To check whether a year is leap or not
Step 1:
Step 2:
Code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc=new Scanner(System.in);
//input year from user
System.out.println("Enter a Year");
int year = sc.nextInt();
//condition for checking year entered by user is a leap year
or not
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
}
}
19. Problem Statement:
Code:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//input a number from user
System.out.println("Enter the number to be checked : ");
int n = sc.nextInt();
//create object of class CheckPrime
Main ob=new Main();
//calling function with value n, as parameter
ob.check(n);
}
//function for checking number is positive or negative
void check(int n)
{
if(n<0)
System.out.println("Please enter a positive integer");
else
prime(n);
}
//function for checking number is prime or not
void prime(int n)
{
int c=0;
for(int i=2;i<n;i++)
{
if(n%i==0)
++c;
}
if(c>=1)
System.out.println("Entered number is not a prime number");
else
System.out.println("Entered number is a prime number");
}
}
Code:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//input value of n
System.out.print("Enter the value of n : ");
int n = sc.nextInt();
int a = 1, b = 1;
//statement for even value of n
if(n % 2 == 0)
{
for(int i = 1 ; i <= (n-2) ; i = i+2)
{
a = a * 2;
b = b * 3;
}
System.out.print(n+" element of the series is =
"+b);
}
//statement for odd value of n
else
{
for(int i = 1 ; i < (n-2) ; i = i+2)
{
a = a * 2;
b = b * 3;
}
a = a * 2;
System.out.print(n+" element of the series is =
"+a);
}
}
}
22. Problem Statement:
Consider the below series :
This series is a mixture of 2 series all the odd terms in this series form
even numbers in ascending order and every even terms is derived from
the previous term using the formula (x/2)
Write a program to find the nth term in this series.
The value n in a positive integer that should be read from STDIN the nth
term that is calculated by the program should be written to STDOUT.
Other than the value of the nth term no other characters /strings or
message should be written to STDOUT.
Code:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 0, b = 0;
if(n % 2 == 0)
{
for(int i = 1 ; i <= (n-2) ; i = i+2)
{
a = a + 2;
b = a / 2;
}
System.out.print(b);
}
else
{
for(int i = 1 ; i < (n-2) ; i = i+2)
{
a = a + 2;
b = a / 2;
}
a = a + 2;
System.out.print(a);
}
}
}
23. Problem Statement:
The program will recieve 3 English words inputs from STDIN
1. These three words will be read one at a time, in three separate line
2. The first word should be changed like all vowels should be
replaced by %
3. The second word should be changed like all consonants should be
replaced by #
4. The third word should be changed like all char should be converted
to upper case
5. Then concatenate the three words and print them
For example if you print how are you then output should be h
%wa#eYOU.
You can assume that input of each word will not exceed more than 5
chars
Code:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter three words : ");
String s1 = sc.next();
String s2 = sc.next();
String s3 = sc.next();
int l1 = s1.length();
int l2 = s2.length();
There were a total of 4 test cases. Once you compile 3 of them will be
shown to you and 1 will be a hidden one. You have to display error
message if numbers are not numeric.
Code:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Number 1 : ");
int num1 = sc.nextInt();
System.out.print("Number 2 : ");
float num2 = sc.nextFloat();
float sum = num1 + num2;
System.out.println("Sum = "+sum);
}
}
Code:
26. Problem Statement:
Given a non-negative integer array Arr having size N. Each
element of the array will carry a different
value. This means no two elements can have the same
values.The candidate has to do this with minimal
changes in the original value of the elements, making every
element as least as much value as it originally
had.
Find the minimum sum of all elements that can be set the array
for:
Example 1:
Input
3 -> Value of N, represents size of Arr
2 -> Value of Arr[0]
2-> Value of Arr[1]
4-> Value of Arr[2]
Output
9
Explanation:
As two elements have the same value, max value for the one of
them needs to be incremented to 3.
He can set the array with 2+3+4=9
Example 2:
Input
2 -> Value of N, represents size of Arr
3 -> Value of Arr[0]
4-> Value of Arr[1]
5-> Value of Arr[2]
Output
Wrong Input
Explanation:
Here N=2, so we need to provide value of only two elements but
we are providing value of three elements
so result is “Wrong Input”
The Input format for the testing
First input line: Accept a single positive integer value for N
representing the size of Arr[]
Second input line: Accept N number of integer values separated
by a new line, representing the original
value assigned to each element.
Output Format for testing:
The output must be a non integer only (See the output format
example).
Code:
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
int sum = 0;
for (int i = 0; i < N; i++) {
// Increment the value until it's unique
while (uniqueValues.contains(Arr[i])) {
Arr[i]++;
}
uniqueValues.add(Arr[i]);
sum += Arr[i];
}
Constraints
1 <=N <=100
Example 1
Input:
10 ---> Integer
Output:
5 → result - Integer
Explanation:
Binary representation of 10 is 1010. After toggling the bits
(1010), will get 0101, which represents “5”.
Hence the output will print “5”.
Example 2
Input:
101 ---> Integer
Output:
Wrong input → result - String
Explanation:
Given integer “101” is out of range. Hence the output will print
“Wrong input”.
The input format for testing
The candidate has to write the code to accept one input(s).
1. First Input - First line contains an integer
The output format for testing
1. Print integer value based on the number got after toggling all
the bits of given input.
2. Print “Wrong input if the string length is out of the range.
3. Additional messages in output will cause the failure of test
cases.
Instructions
1. The system doesn’t allow any kind of hard-coded input value.
2. Written program code by the candidate will be verified against
all the inputs which are supplied from
the system.
Code:
import java.util.Scanner;
Code:
import java.util.Scanner;
case 1:
// Just increment mid
mid++;
break;
case 2:
// Swap arr[mid] and arr[high], decrement high
int temp2 = arr[mid];
arr[mid] = arr[high];
arr[high] = temp2;
high--;
break;
}
}
}
}
29. Problem Statement:
Given N gold wires, each wire has a length associated with it. At
a time, only two adjacent small wres
assembled at the end of a large wire and the cost of forming is
the sum of their length. Find the minimum
cost when all wires are assembled to form a single wire.
For Example:
Suppose, Arr[] = {7, 6, 8, 6, 1, 1}
{7, 6, 8, 6, 1, 1} - {7, 6, 8, 6, 2}, cost = 2
{7, 6, 8, 6, 2} - {7, 6, 8, 8}, cost = 8
{7, 6, 8, 8} - {13, 8, 8}, cost =13
{13, 8, 8} - {13, 16}, cost = 16
{13, 16} - {29}, cost = 29
2 + 8 + 13 + 16 + 29 = 68
Hence, the minimum cost to assemble all gold wires is : 68
Constraints:
1 <= N <= 30
1<= Arr[] <= 100
Example 1:
Input:
6 -> Value of N, represents size of Arr
7 -> Value of Arr[0], represents length of 1st wire
6 -> Value of Arr[1], represents length of 2nd wire
8 -> Value of Arr[2], represents length of 3rd wire
6 -> Value of Arr[3], represents length of 4th wire
-1 -> Value of Arr[4], represents length of 5th wire
1 -> Value of Arr[5], represents length of 6th wire
Output:
68
Example 2:
Input:
4 -> Value of N, represents size of Arr
12 -> Value of Arr[0], represents length of 1st wire
2 -> Value of Arr[1], represents length of 2nd wire
2 -> Value of Arr[2], represents length of 3rd wire
5 -> Value of Arr[3], represents length of 4th wire
Output:
34
Code:
import java.util.PriorityQueue;
import java.util.Scanner;
int totalCost = 0;
pq.add(combined);
}
Solution 2
Maximum number of squares/rectangles that can be obtained
with N=4 is 9(Solution 1)
Hence, output is 9
Example 2:
Input:
1 → Value of N(No. of teams)
Output:
2 → maximum number of equal squares or rectangles
Code:
import java.util.Scanner;
Constraints:
1 <= N <= 500
1 <=arr[i] <= 100
Example 1:
Input:
5 → Value of N
{8,1,7,9,2} → arr[] elements from arr[0] to arr [N-1],
Where each element is separated by new line.
Output:
14
Explanation:
21
Code:
import java.util.Arrays;
import java.util.Scanner;
int maxDifferenceSum = 0;
Code:
import java.util.Scanner;
Code:
import java.util.Scanner;
23
Constraints:
<=N<S<T
Example -1:
Input:
5. -> Number of elements i.e. T
0. -> Value of 1st element
1. -> Value of 2nd element
0. -> Value of 3rd element
1. -> Value of 4th element
1. -> Value of 5th element
Output:
5
Explanation:
The 5 pairs are (Arr[0], Arr[1]), (Arr[0], Arr[3]), (Arr[0], Arr[4]),
(Arr[2], Arr[3]) and (Arr[2], Arr[4]).
Note that in all pairs first element is 0, second element is 1 and
index of first element is smaller than index
of second element.
The Input format for testing:
First input line: Accept a single positive integer value for T
representing the size of Arr[].
Second input line:: Accept N number of integer values (0 or 1)
separated by a new line.
Output Format for Testing:
The output must be a non-negative integer number only (See the
output format in example).
Additional messages in the output will result in the failure of test
cases.
Code:
import java.util.Scanner;
For example :
3
232
1122233
22311
Sample Input :
22311
Sample Output :
Code:
import java.util.*;
int n = sc.nextInt();
arr[i] = sc.nextInt();
int left=0,right=n-1,mid,pre,nxt;
if(arr[0] != arr[1]){
System.out.print(arr[0]);
System.out.print(arr[n-1]);
}
else{
mid = ((right-left)/2)+left;
pre = mid-1;
nxt = mid+1;
System.out.print(arr[mid]);
break;
else if(mid%2==0){
if(arr[pre] == arr[mid])
right = mid - 1;
else
left = mid + 1;
}
else{
if(arr[pre] == arr[mid])
left = mid + 1;
else
right = mid - 1;
Count all the subsets of given array with sum equal to given
sum.
NOTE: Since result can be very large, print the value modulo
109+7.
Constraints :
1<=T<=100
1<=n<=103
1<=a[i]<=103
1<=sum<=103
Example :
Input :
2 3 5 6 8 10
10
5
12345
10
Output :
Explanation :
Code:
import java.io.*;
import java.util.*;
class Main {
while (n>0) {
if ((n & 1) == 1)
System.out.print(1);
else
System.out.print(0);
n >>= 1;
len--;
while (len>0) {
System.out.print(0);
len--;
System.out.println();
}
static void printSubsetsCount(int set[], int n, int val)
int sum;
int count = 0;
sum = 0;
sum += set[j];
if (sum == val) {
count++;
}
if (count == 0) {
System.out.println("No subset is
found");
else {
System.out.println(count);
int t = sc.nextInt();
int n,sum;
while(t>0)
{
n=sc.nextInt();
for(int i=0;i<n;i++)
set[i] = sc.nextInt();
sum = sc.nextInt();
printSubsetsCount(set, n, sum);
t--;
Note:
Input Format:
Output Format:
Constraints:
Sample Input 1:
ABAB
Sample Output 1:
7
Explanation:
Sample Input 2:
AAA
Sample output 2:
Explanation:
The fake palindrome for the string AAA are A, A, A, AA, AA, AAA
Code:
import java.util.*;
class Main{
int res = 0;
{
int x = 0;
x ^= temp;
res++;
System.out.print(res);
{
Scanner sc = new Scanner(System.in);
countSubString(str);
B2 B1 B4 B3
B2 B3 B4 B1
B2 B4 B1 B3
B3 B1 B4 B2
B3 B4 B1 B2
B3 B4 B2 B1
B4 B1 B2 B3
B4 B3 B1 B2
B4 B3 B2 B1
Constraints
Input Format
Output Format
Sample Input :
4
Sample Output :
Code:
import java.util.*;
long n,a=0,b=1,c=2,d=0;
n = sc.nextLong();
if(n==1 || n==2)
System.out.println(n-1);
else{
a = b;
b = d;
c++;
System.out.println(d);
The first line contains the number of test cases T, T lines follow.
Each line then contains an integer N, the total number of people
attended that meeting.
Output Format :
Constraints :
Sample Input :
Output :
Explanation :
import java.util.*;
int t,n;
long sum;
t = sc.nextInt();
n = sc.nextInt();
n--;
sum = (n*(n+1))/2;
System.out.println(sum);
t--;
}
int decimalValue = 0;
int length = base17Number.length();
Case 1:
Input: 4567
Expected Output: 2
Explantion : Odd positions are 4 and 6 as they are pos:
1 and pos: 3, both have sum 10. Similarly, 5 and 7 are at
even positions pos: 2 and pos: 4 with sum 12. Thus,
difference is 12 - 10 = 2
Code:
import java.util.Scanner;
int oddSum = 0;
int evenSum = 0;
if ((i + 1) % 2 == 0) {
// Even position (1-based index)
evenSum += digit;
} else {
// Odd position (1-based index)
oddSum += digit;
}
}
Constraints:
2<=n<=1000
Input:
Code:
import java.util.*;
import java.lang.*;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
char c = 'R';
int x = 0, y = 0;
while(n>0){
switch(c){
case 'R':
x = Math.abs(x) + 10;
y = Math.abs(y);
c ='U';
break;
case 'U':
y = y + 20;
c = 'L';
break;
case 'L':
x = -(x + 10);
c = 'D';
break;
case 'D':
y = -(y);
c = 'R';
break;
}
n--;
}
System.out.println(x+" "+y);
}
}
break, case, continue, default, defer, else, for, func, goto, if, map,
range, return, struct, type, var
Input #1:
defer
Output:
defer is a keyword
Input #2:
While
Code:
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
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");
}
}
}
Example-1
Input
5 10
Expected output
05 06 07 08 09 10
Example-2
Input
9 100
Expected output
009 010 011 012 013 014 015 016 017 018 019 020 021 022
023 024 025 026 027 028 029 030 031 032 033 034 035 036
037 038 039 040 041 042 043 044 045 046 047 048 049 050
051 052 053 054 055 056 057 058 059 060 061 062 063 064
065 067 068 069 070 071 072 073 074 075 076 077 078 079
080 081 082 083 084 085 086 087 088 089 090 091 092 093
094 095 096 097 098 099 100
Example-3
Input
19
Code:
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int low=sc.nextInt();
int up=sc.nextInt();
for(int i=low;i<=up;i++)
{
if(up>=100)
System.out.printf("%03d ",i);
else if(up>=10)
System.out.printf("%02d ",i);
else
System.out.printf("%d ",i);
}
}
}
She said that if I add all the mints of kids before her and subtract
one from it, the result equals the mints she has. It seemed to be
a uniform response from everyone. So, I went to the boy at the
head of the queue consoling myself that he would not give the
same response as others. He said, “I have four mints”.
Given the number of first kid’s mints (n) and the length (len) of
the queue as input, write a program to display the total number
of mints with all the kids.
constraints:
2<n<10
1<len<20
Input#1:
42
Output:
7
Input#2:
14 4
Code:
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
int n = sc.nextInt();
int sum=s,prev;
for(int i=1;i<n;i++)
{
prev=sum-1;
sum+=prev;
}
System.out.println(sum);
}
}
Now let’s check all the combinations and how in-efficient is all of
them.
When we make 1st, 2nd, and 3rd employee on table-1 and 4th
and 5th on table-2
Cost of 2 tables = 2*1
Number of people getting into arguments = 2 (two 3’s: 4th and
5th employee)
Total = 2 + 2 = 4
When we make 1st, 2nd, 3rd, and 4th employees on table-1 and
5th on table-2
Cost of 2 tables = 2*1
Number of people getting into arguments = 2 (two 3’s: 4th and
5th employee)
Total = 2 + 2 = 4
When we make 1st, 2nd and 3rd employee on table-1 and 4th on
table-2 and 5th on table-3
Cost of 3 tables = 3*1
Number of people getting into arguments = 0 (all 3’s are. sitting
at different tables)
Total = 3 + 0 = 3
Example 1:
Input:
5 1 -> Input Integer, N and K
{5, 1, 3, 3, 3) Input Integer, array elements a[i].
Output:
3 -> Output
Explanation:
Below is the seating for each case:
Case 1:
Table 1: 1st, 2nd, and 3rd
Table 2: 4th and 5th
Number of people getting into arguments: 2
Total in-efficiency: 2*1 + 2 = 4
Case 2:
Table 1: 1st, 2nd, 3rd, and 4th
Table 2: 5th
Number of people getting into arguments: 2
Total in-efficiency: 2*1 + 2 = 4
Case 3:
Table 1: 1st, 2nd, 3rd, 4th, and 5th
Number of people getting into arguments: 3
Total in-efficiency: 1*1 + 3 = 4
Case 4:
Table 1: 1st, 2nd, and 3rd
Table 2: 4th
Table 3: 5th
Number of people getting into arguments: 0
Total in-efficiency: 3*1 + 0 = 3
Input:
5 14 -> Input Integer, N and K.
{1, 4, 2, 4, 4} -> Input Integer, array elements a[i].
Output:
17 -> Output
Explanation:
Below is the seating for each case:
Case 1:
Table 1: 1st, 2nd, and 3rd
Table 2: 4th and 5th
Number of people getting into arguments: 2
Total in-efficiency: 2*14 + 2 = 30
Case 2:
Table 1: 1st, 2nd, 3rd, and 4th
Table 2: 5th
Number of people getting into arguments: 2
Total in-efficiency: 2*14 + 2 = 30
Case 3:
Table 1: 1st, 2nd, 3rd, 4th, and 5th
Number of people getting into arguments: 3
Total in-efficiency: 1*14+3 = 17
Case 4:
Table 1: 1st, 2nd, and 3rd
Table 2: 4th
Table 3: 5th
Number of people getting into arguments: 3
Total in-efficiency: 3*14+ 0 = 42
return minInefficiency;
}
Example 1:
Input:
tiger -> input string A
ti -> input string B
Output:
ger -> Output string C
Explanation:
After removing “t” and “i” from “tiger”, we are left with “ger”.
So, the answer is “ger”.
Example 2:
Input:
processed -> input string A
esd -> input string B
Output:
proc -> Output string C
Explanation:
After removing “e” “s” and “d” from “processed”, we are left with
“proc”.
So, the answer is “proc”.
Example 3:
Input:
talent -> input string A
tens -> input string B
Output:
al -> Output string C
Explanation:
After removing “t” “e” and “n” from “talent”, we are left with “al”.
So, the answer is “al”.
Code:
import java.util.Scanner;
if (B.indexOf(currentChar) == -1) {
result.append(currentChar);
}
}
return result.toString();
}
Input:
N and M, size of the matrix
A[i][j] for all 1<=i<=N and 1<=j<=M
If Mahesh plays first, he will try to capture most of the 1’s, he will
capture A[2][2], A[2][3], A[3][2], and A[3][3]. Now there are no
adjacent cells left. So, the chance will be given to Suresh. Now
Suresh’s turn. He can capture either A[1][1] or A[4][1] or A[4][7]
or A[4][4]. He will capture any one cell, and as there is no
adjacent deft, the chance will now be given to Mahesh. The
game proceeds and then again Suresh’s turn will come, and he
will again be able to choose only 1 cell finally Mahesh will end
the game by choosing the final cell.
Like this Mahesh has captured 6 cells and Suresh has captured
only 2 cells.
Hence 2 is the answer.
Example 1:
Input:
22 -> Input integer, N, M
11 -> Input integer, A[i]
11 -> Input integer, A[N]
Output:
0 -> Output
Explanation:
In the above scenario, it is very clear that if Mahesh plays first,
he will capture all the cells as all the cells are adjacent to each
other.
There will be nothing left for Suresh. Hence the cells captured by
Suresh will be 0.
Hence the answer is 0.
Example 2:
Input:
44 -> Input integer, N, M
1001 -> Input integer, A[i]
0110 -> Input integer, A[i+1]
0110 -> Input integer, A[i+2]
1001 -> Input integer, A[N]
Output:
2 -> Output
Explanation:
If Mahesh plays first, he will try to cover most of the 1’s, he will
cover A[2][2], A[2][3], A[3][2], and A[3][3]. Now there are no
adjacent cells left. So, the chance will be given to Suresh. Now
Suresh’s turn. He can capture either of A[1][1] or A[4][1] or A[4]
[1] or A[4][4]. He will capture any one cell, and as there is no
adjacent left, the chance will now be given to Mahesh. The game
proceeds and then again Suresh’s turn will come, and he will
again be able to choose only 1 cell, and finally, Mahesh will end
the game by choosing the final cell.
Like this Mahesh has captured 6 cells and Suresh has captured
only 2 cells.
Hence 2 is the answer.
Code:
import java.util.Scanner;
visited[row][col] = true;
if (player == 2) {
sureshCaptured[0]++;
}
return sureshCaptured[0];
}
It will be 2N:
[ ] : B([ ]) = 1
[1] : B([1]) = 2
[2] : B([2]) = 1
[1] : B([1]) = 2
[1, 2] : B([1, 2]) = 3
[2, 1] : B([2, 1]) = 3
[1, 1] : B([1, 1]) = 2
[1, 2, 1] : B([1, 2, 1]) = 3
Example 1:
Input:
2 -> input Integer, N
1 1 -> input Integer, S
Output:
7 -> Output
Explanation:
In the above scenario below are the various sub-sequence and
their respective functions of it:
[ ] : B(l) = 1
[1]: B([1])= 2
[1]: B([1]) = 2
[1,1]: B([1,1]) = 2
Total sum of all B(S) = 1+2+2+2 = 7 Hence the answer is 7.
Example 2:
Input:
3 -> Input integer, N
1 2 1 -> Input integer, S
Output:
17->Output
Explanation:
In the above scenario below are the various sub-sequences and
their respective functions of it.
[ ] : B([ ]) = 1
[1] : B([1]) = 2
[2] : B([2]) =1
[1] : B([1]) = 2
[1, 2] : B([1, 2]) = 3
[2, 1] : B([2, 1]) = 3
[1, 1] : B([1, 1]) = 2
[1, 2, 1] : B([1, 2, 1]) = 3
Total sum of all B(S) = 1 + 2 + 1 + 2 + 3 + 3 + 2 + 3 = 17.
Hence the answer is 17.
Code:
import java.util.Arrays;
import java.util.Scanner;
return missing;
}
int index = 0;
for (int j = 0; j < n; j++) {
if ((i & (1 << j)) != 0) {
subsequence[index++] = sequence[j];
}
}
return sum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Pick exactly one element from the sequence and multiply it with -
1.
Your task is to find out the number of different sequences which
can be formed by performing the above operation. If the answer
is large, print the result modulo 109 +7.
Way 2:
If we multiply -1, by two different elements just 1 time each. We
get:
Input:
3 1 -> Input integer, N, X
{1, 2, 1} -> Input integer, S
Output:
3 -> Output
Explanation:
In the given scenario, we have X =1. Hence, we can have this
multiplication of -1 only once.
So, if we multiply -1, by different elements just 1 time. We get:
Multiply -1 to S[1] & S[2] : [-1, -2, 1].
Multiply -1 to S[2] & S[3] : [1, -2, -1].
Multiply -1 to S[1] & S[3] : [-1, 2, -1].
Hence, we get a total of 3 different sequences.
So, the answer is 3.
Example 2:
Input:
3 2 -> Input integer, N, X
{1, 2, 3} -> Input integer, S
Output:
4 -> Output
Explanation:
There are 2 ways in which this operation can be performed
Way 1: Either – 1 should be multiplied to the same element 2
times, OR
Way 2: -1 should be multiplied by different elements once.
As shown in the above Demo example, there will be a total of 4
different sequences which can be achieved from this.
Hence the answer is 4.
Code:
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
return result;
}
return result;
}
Example 1:
Input:
3 -> Value of N1
3 -> Value of N2
{3,5,2} -> a[ ], Elements a[0]to a[N1-1], where each input
element is separated by new line
{1,2,3} -> b[ ], Elements b[0]to b[N2-1), where each input
element is separated by new line
Output: 2.5
Explanation:
From the inputs given above:
Number of strong ground positions on road A:3
Number of strong ground positions on road B:3
The positions of strong ground from the starting point of road A
are at a distance of 3,5,2
The positions of strong ground from the starting point of road B
are at a distance of 1,2,3
Combining the strong ground positions of both the roads and
sorting them in ascending order
1, 2, 3, 5
The Middle points are 2 and 3
2+3 = 5
5/2 = 2.5
So, the middle point from where the bridge should be
constructed is 2.5.
Hence, the output is 2.5
Example 2:
Input:
2 -> Value of N1
3 -> Value of N2
{2,3} -> all, Elements a[O]to a[N1-1), where each input element
is separated by new line
{5,6,4} -> b[ ], Elements b[O]to b[N2-1], where each input
element is separated by new line
Output: 4
Explanation:
From the inputs given above:
Number of strong ground positions on road A: 2
Number of strong ground positions on road B: 3
The positions of strong ground from the starting point of road A
are at a distance of 2, 3 The positions of strong ground from the
starting point of road B are at a distance of 5, 6, and 4
Combining the strong ground positions of both the roads and
sorting them in ascending order: 2, 3, 4, 5, 6 > Middle point is 4
So, the middle point from where the bridge should be
constructed is 4.
Hence, the output is 4.
Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
int N1 = scanner.nextInt();
int N2 = scanner.nextInt();
Collections.sort(combined);
double midpoint;
int size = combined.size();
if (size % 2 == 0) {
midpoint = (combined.get(size/2 - 1) +
combined.get(size/2)) / 2.0;
} else {
midpoint = combined.get(size/2);
}
System.out.println(midpoint);
scanner.close();
}
}
52. Problem Statement:
A chocolate factory is packing chocolates into 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:
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
Code:
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;
● 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
Valid (total weight(<C)
Invalid (total
Total Weig Total
weight>C)
Value Value ht Weight
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.
Third input – Accept N number of positive integer number for array v[0…
N-1], where each value is separated by a new line.
Constraints:
● 0<=N<=10
● 0 <= C<=500
● 1<=V[i]<=500
Code:
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));
}
}
Example 1:
5 —Value of N
{10, 20, 30, 40, 50} —Element of Arr[ ]
2 —–Value of K
Output :
40 50 10 20 30
Example 2:
4 —Value of N
{10, 20, 30, 40} —Element of Arr[]
1 —–Value of K
Output :
40 10 20 30
Code:
import java.util.*;
public class Main
{
static int[] rotate(int nums[], int n, int k) {
if (k > n)
k = k % n;
int index = 0;
for (int i = k; i < n; i++) {
ans[i] = nums[index++];
}
return ans;
}
public static void main(String[] args) {
int Array[] = { 1, 2, 3, 4, 5 };
int N = 5;
int K = 2;
For example:
There is the number 11, which has repeated digits, but 12, 13,
14 and 15 have no repeated digits. So, the output is 4.
Example1:
Input:
11 — Vlaue of n1
15 — value of n2
Output:
4
Example 2:
Input:
101 — value of n1
200 — value of n2
Output:
72
Code:
import java.util.*;
public class Main
{
static int find(int n1, int n2) {
int count = 0;
for (int i = n1 ; i <= n2 ; i++) {
int num = i;
if (num == 0)
count++;
}
return count;
}
public static void main(String[] args) {
int n1 = 101, n2 = 200;
System.out.println(find(n1, n2));
}
}
56. Problem Statement:
Identify the logic behind the series
00006
00028 00066
If input is 3, output will be
00006
00028 00066
00120 00190 00276
Formal input and output specifications are stated below
Input Format:
0 < N <= 14
Code:
import java.util.Scanner;
public class LogicPyramid
{
public static void main (String[]args)
{
int n, a = 0, b = 3, i, re, j;
Scanner sc = new Scanner (System.in);
n = sc.nextInt ();
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
a = a + 2;
if (i == 1)
b = 3;
else
b = b + 4;
re = a * b;
System.out.println (re);
}
System.out.println ();
}
}
}
Constraints:
Input Format:
Explanation:
Example 1
Input
10000
20
3
5 9.5
10 9.6
5 8.5
3
10 6.9
5 8.5
5 7.9
Output: Bank B
Example 2
Input
500000
26
3
13 9.5
3 6.9
10 5.6
3
14 8.5
6 7.4
6 9.6
Output: Bank A
Code:
import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
double p, s, mi, sum, emi, sq;
int y, n, k, yrs, l = 0;
double[] bank = new double[5];
System.out.println ("Enter the principal amount");
p = sc.nextDouble ();
System.out.println ("Enter tenature year");
y = sc.nextInt ();
for (k = 0; k < 2; k++)
{
System.out.println ("Enter the no of slabs");
n = sc.nextInt ();
sum = 0;
for (int i = 0; i < n; i++)
{
System.out.println ("Enter the period :");
yrs = sc.nextInt ();
System.out.println ("Enter the intrest :");
s = sc.nextDouble ();
mi = 0;
sq = Math.pow ((1 + s), yrs * 12);
emi = (p * (s)) / (1 - 1 / sq);
sum = sum + emi;
}
bank[l++] = sum;
}
if (bank[0] < bank[1])
System.out.println ("Bank A");
else
System.out.println ("Bank B");
}
58. Problem Statement:
In this 3 Palindrome, Given an input string word, split the string
into exactly 3 palindromic substrings. Working from left to right,
choose the smallest split for the first substring that still allows the
remaining word to be split into 2 palindromes.
1
Examples
Example 1
Input
nayannamantenet
Output
nayan
naman
tenet
Explanation
Input
aaaaa
Output
a
a
aaa
Explanation
The other ways to split the given string into 3 palindromes are as
follows –
[a, aaa, a], [aaa, a, a], [aa, aa, a], etc.
Since we want to minimize the length of the first palindromic
substring using left to right processing, the correct way to split is
[a, a, aaa].
Example 3
Input
aaaabaaaa
Output
aaabaaa
Explanation
The other ways to split the given string into 3 palindromes are as
follows –
[aaaa, b, aaaa], [aa, aabaa, aa], etc.
Since we want to minimize the length of the first palindromic
substring using left to right processing, the correct way to split is
[a, aaabaaa, a].
Code:
import java.util.*;
class Solution
{
if (s.length () == 1)
return true;
StringBuilder sb = new StringBuilder (s);
sb = sb.reverse ();
String rev = new String (sb);
return s.equals (rev);
}
Your task is that given the location of the first tree that catches
fire, determine how long would it take for the entire forest to be
on fire. You may assume that the lay out of the forest is such that
the whole forest will catch fire for sure and that there will be at
least one tree in the forest
Input Format:
Constrains:
3 ≤ M ≤ 20
3 ≤ N ≤ 20
Sample Input 1:
33
WTT
TWW
WTT
Sample Output 1:
Explanation:
In the second minute,tree at (1,2) catches fire,in the third
minute,the tree at (2,1) catches fire,fourth minute tree at (3,2)
catches fire and in the fifth minute the last tree at (3,3) catches
fire.
Sample Input 2:
66
16
WTTTTT
TWWWWW
WTTTTT
WWWWWT
TTTTTT
TWWWWW
Sample Output 2:
16
Code:
import java.util.HashMap;
import java.util.Scanner;
class Temp {
static void print(int [][] arr){
for(int i=0;i<arr.length;i++){
for (int j=0;j<arr[0].length;j++){
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int [][] array = new int[N][N];
HashMap<Integer, Integer> map = new HashMap<>();
int counter = 1;
int row = 0, col = 0; // for the current row/col that we are end
int endRow = 0, endColumn = 0; // for the row/col where we
need to stop
map.put(0,0);
for(int i=0;i<N/2;i++){
row = col = i;
endColumn = N-i-1;
if (counter % 11==0)
map.put(row,col);
counter++;
col++;
}
endRow = N-i-1;
while (row<endRow){
array[row][col] = counter;
if (counter % 11==0)
map.put(row,col);
counter++;
row++;
}
endColumn = i;
while (col>endColumn){
array[row][col] = counter;
if (counter % 11==0)
map.put(row,col);
counter++;
col--;
}
endRow = i;
while (row>endRow){
array[row][col] = counter;
if (counter % 11==0)
map.put(row,col);
counter++;
row--;
}
}
if (N%2==1)
array[N/2][N/2] = N*N;
print(array);
System.out.println("Total Power Points: " + powerpoints);
for (Integer key: map.keySet()) {
System.out.println("("+key+ ","+map.get(key)+")");