1.
Inheritance 2
//java Solution
import java.util.*;
class Vehicle {
private String registrationNo;
private int modelNo;
/*-----------------------------------------------------------------------------
// create parametrize contructor to initialize the all field i.e registrationNo, modelNo
Vehicle(String registrationNo, int modelNo)
-----------------------------------------------------------------------------*/
public Vehicle(String registrationNo, int modelNo) {
this.registrationNo = registrationNo;
this.modelNo = modelNo;
/*------------------------------------------------------------------------------
// create getter for field getter name must be
// getRegistrationNo
// getModelNo
----------------------------------------------------------------------------*/
public String getRegistrationNo() {
return this.registrationNo;
}
public int getModelNo() {
return this.modelNo;
class Car extends Vehicle {
private String name;
private String color;
/*------------------------------------------------------------------------
// create parametrize constructor to initialize the all field i.e name , color, registrationNo,
modelNo use super keyword initialize the parent class name fields from this constructor
Car(String name, String color, String registrationNo, int modelNo)
--------------------------------------------------------------------------*/
public Car(String name, String color, String registrationNo, int modelNo) {
super(registrationNo, modelNo);
this.name = name;
this.color = color;
/*--------------------------------------------------------------------------
// create getters for all field getter name must be
// getName
// getColor
---------------------------------------------------------------------------*/
public String getName() {
return this.name;
}
public String getColor() {
return this.color;
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String registrationNo = sc.nextLine();
int modelNo = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();
String color = sc.nextLine();
Car car = new Car( name, color,registrationNo, modelNo);
System.out.println("Registration no : " + car.getRegistrationNo());
System.out.println("Model no : " + car.getModelNo());
System.out.println("Car Name : " +car.getName());
System.out.println("Color : " + car.getColor());
2. Student Evaluation Criteria
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String firstName = scan.next();
String lastName = scan.next();
int empId = scan.nextInt();
int numScores = scan.nextInt();
int[] testScores = new int[numScores];
for(int i = 0; i < numScores; i++){
testScores[i] = scan.nextInt();
scan.close();
RDStudent s = new RDStudent(firstName, lastName, empId, testScores);
s.printPerson();
System.out.println("Grade: " + s.calculateGrade());
class RDStudent extends Person {
private int[] testScores;
//write your logic here
public RDStudent(String firstName, String lastName, int id, int[] scores) {
super(firstName, lastName, id);
testScores = scores;
}
public char calculateGrade() {
int sum = 0;
for (int i = 0; i < testScores.length; i++) {
sum += testScores[i];
}
double average = sum * 1.0 / testScores.length;
if (average >= 90 && average <= 100) {
return 'O';
} else if (average >= 80 && average < 90) {
return 'E';
} else if (average >= 70 && average < 80) {
return 'A';
} else if (average >= 55 && average < 70) {
return 'P';
} else if (average >= 40 && average < 55) {
return 'D';
} else {
return 'T';
}
class Person {
protected String firstName;
protected String lastName;
protected int idNumber;
// Constructor
Person(String firstName, String lastName, int identification){
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = identification;
// Print person data
public void printPerson(){
System.out.println(
"Name: " + lastName + ", " + firstName
+ "\nEMP ID: " + idNumber);
3. OOPs : custom sort Object List
import java.util.*;
<showDefaultCode>
class Candidate implements Comparable<Candidate> {
private String name;
private int marks;
public Candidate(String name, int marks) {
// write your code here ...
this.name = name;
this.marks = marks;
/* Override the compareTo method to get the object of list sorted in ascending order */
@Override
public int compareTo(Candidate candidate) {
// write your code here ...
return this.marks- candidate.marks;
}
// create getter for both fields name, marks
// getter method name must be getName, getMarks
// write your code here...
public String getName() {
return this.name;
}
public int getMarks() {
return this.marks;
}
</showDefaultCode>
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
ArrayList<Candidate> arr = new ArrayList<Candidate>();
sc.nextLine();
while(t > 0) {
String name = sc.next();
int marks = sc.nextInt();
Candidate candidate = new Candidate(name, marks);
arr.add(candidate);
t--;
Collections.sort(arr);
for(Candidate c : arr ){
System.out.println(c.getName() + " " + c.getMarks() );
4. Apartments With Flower Garden View
import java.io.*;
import java.util.*;
public class Main
public void solve(int[] heights) {
// Write your logic here
int targetBuildingCount=0;
int maxHeight = 0;
for(int i = heights.length-1; i >= 0; i--) {
if(heights[i]>maxHeight) {
targetBuildingCount++;
maxHeight = heights[i];
heights[i]=-heights[i];
int[] buildings = new int[targetBuildingCount];
int buildingIndex=0;
for(int i = 0; i < heights.length; i++) {
if(heights[i] < 0) {
heights[i] = -heights[i];
buildings[buildingIndex++] = i;
for (int i=0; i<buildings.length; i++)
System.out.println(buildings[i]);
//Print result here;
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
int[] array = new int[n];
for(int i=0; i<n; i++)
array[i]=sc.nextInt();
sc.close();
Main obj = new Main();
obj.solve(array);
}
5. EMPLOYEE TAX REGIME
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.io.*;
import java.util.*;
public class Main {
public static Scanner sc = new Scanner(System.in);
public static int rebate = 0;
public static void main(String[] args) {
sc.useDelimiter(Pattern.compile("(\r\n|\r|\n)"));
String slabRate = sc.next();
String slabRates[] = slabRate.split(" ");
String taxPercent = sc.next();
String taxPercents[] = taxPercent.split(" ");
rebate = sc.nextInt();
String empTaxPaid = sc.next();
String empsTaxPaid[] = empTaxPaid.split(" ");
EmployeeSalary employeeSalary = new EmployeeSalary();
int totalSalary= employeeSalary.calculateSalary(slabRates,taxPercents,empsTaxPaid,rebate);
System.out.print(totalSalary);
}
class EmployeeSalary{
public static List<Integer> taxPerSlab = new ArrayList();
public static List<Integer> empTax = new ArrayList();
public static List<Integer> slab = new ArrayList();
public static List<Integer> perc = new ArrayList();
public static Integer totalSalary=0;
public static void populateTax(){
taxPerSlab.add(0);
for(int i=1;i<slab.size();i++){
int diff = slab.get(i)-slab.get(i-1);
diff *= perc.get(i-1);
diff /= 100;
taxPerSlab.add(diff);
}
}
public static Integer getSalary(Integer tax,int rebate){
Integer salary = 0;
int i=1;
while(i<taxPerSlab.size() && tax>=taxPerSlab.get(i)){
tax -= taxPerSlab.get(i);
salary+=slab.get(i-1);
i++;
}
if(i==taxPerSlab.size())
i--;
int temp= tax*100;
temp/=perc.get(i);
salary += temp+rebate;
//System.out.println(salary);
return salary;
}
public static int calculateSalary(String[] slabint,String[] perint,String[] empTaxint,int rebate){
//write you logic here
if(slabint.length!=perint.length)
System.exit(1);
//System.out.println("Success");
for(int i=0;i<slabint.length;i++){
slab.add(Integer.parseInt(slabint[i]));
perc.add(Integer.parseInt(perint[i]));
//System.out.println("slab : "+slab.get(i)+" perc :
"+perc.get(i));
}
for(int i=0;i<empTaxint.length;i++){
empTax.add(Integer.parseInt(empTaxint[i]));
//System.out.println(empTax.get(i));
}
populateTax();
for(int i=0;i<empTax.size();i++){
totalSalary += getSalary(empTax.get(i),rebate);
}
return totalSalary;
6. The shortest distance between all structures
import java.util.*;
class Main {
int[][] dist = null;
int[][] reach = null;
public int shortestDistance(int[][] grid) {
// Write your logic here
dist = new int[grid.length][grid[0].length];
reach = new int[grid.length][grid[0].length];
int buildings = 0;
for (int r=0;r<grid.length;r++) {
for (int c=0;c<grid[0].length;c++) {
if (grid[r][c] == 1) {
buildings++;
bfs(grid, r, c);
}
}
if (buildings == grid.length*grid[0].length)
return -1;
int shortestDistance = Integer.MAX_VALUE;
for (int i=0;i<grid.length;i++) {
for (int j=0;j<grid[0].length;j++) {
if (grid[i][j] == 0 && reach[i][j] == buildings && dist[i][j]!= 0) {
if (dist[i][j] < shortestDistance)
shortestDistance = dist[i][j];
return (shortestDistance != Integer.MAX_VALUE) ? shortestDistance : -1;
private void bfs(int[][] grid, int r, int c) {
boolean[][] visited = new boolean[grid.length][grid[0].length];
Queue<int[]> queue = new LinkedList<>();
int[] start = {r, c, 0};
queue.add(start);
int[] nx = {1, -1, 0, 0};
int[] ny = {0, 0, -1, 1};
while (!queue.isEmpty()) {
int[] cc = queue.poll();
for(int i=0;i<nx.length;i++) {
int nextX = cc[0]+nx[i];
int nextY = cc[1]+ny[i];
int nextD = cc[2]+1;
if ( nextX<grid.length && nextX >=0
&& nextY<grid[0].length && nextY >=0
&& (grid[nextX][nextY] == 0)
&& !visited[nextX][nextY]) {
int[] nextC = {nextX, nextY, nextD};
queue.add(nextC);
dist[nextX][nextY] += nextD;
visited[nextX][nextY] = true;
reach[nextX][nextY]++;
public static void main(String args[])
Scanner sc = new Scanner(System.in);
int M = Integer.parseInt(sc.nextLine());
int N = Integer.parseInt(sc.nextLine());
int[][] matrix = new int[M][N];
for (int row = 0; row < M; row++) {
for (int col = 0; col < N; col++) {
matrix [row][col] = Integer.parseInt(sc.nextLine());
sc.close();
Main obj = new Main();
System.out.println(obj.shortestDistance(matrix));
7. Real estate Business startup
import java.util.Scanner;
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[][] clients = new int[n][2];
for(int clienti = 0; clienti < n; clienti++){
clients[clienti][0] = in.nextInt();
clients[clienti][1] = in.nextInt();
int[][] houses = new int[m][2];
for(int housei=0; housei < m; housei++){
houses[housei][0] = in.nextInt();
houses[housei][1] = in.nextInt();
RealEstate realEstate = new RealEstate();
System.out.println(realEstate.realEstateBroker(n,m, clients, houses));
class RealEstate{
public int realEstateBroker(int n, int m, int[][] clients, int[][] houses) {
//write you logic here
boolean[][] graph = new boolean[n][m];
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++) {
if (houses[j][0] > clients[i][0] && houses[j][1] <= clients[i][1]) {
graph[i][j] = true;
} else {
graph[i][j] = false;
return maxBPM(graph, m, n);
// A DFS based recursive function that returns true if a
// matching for vertex u is possible
boolean bpm(boolean bpGraph[][], int u, boolean seen[],
int matchR[], int N, int M) {
// Try every job one by one
for (int v = 0; v < N; v++) {
// If applicant u is interested in job v and v
// is not visited
if (bpGraph[u][v] && !seen[v])
{
seen[v] = true; // Mark v as visited
// If job 'v' is not assigned to an applicant OR
// previously assigned applicant for job v (which
// is matchR[v]) has an alternate job available.
// Since v is marked as visited in the above line,
// matchR[v] in the following recursive call will
// not get job 'v' again
if (matchR[v] < 0 || bpm(bpGraph, matchR[v], seen, matchR, N, M)) {
matchR[v] = u;
return true;
}
}
}
return false;
}
// Returns maximum number of matching from M to N
int maxBPM(boolean bpGraph[][], int N, int M) {
// An array to keep track of the applicants assigned to
// jobs. The value of matchR[i] is the applicant number
// assigned to job i, the value -1 indicates nobody is
// assigned.
int matchR[] = new int[N];
// Initially all jobs are available
for(int i=0; i<N; ++i) {
matchR[i] = -1;
}
int result = 0; // Count of jobs assigned to applicants
for (int u = 0; u < M; u++) {
// Mark all jobs as not seen for next applicant.
boolean seen[] =new boolean[N];
for(int i=0; i<N; ++i) {
seen[i] = false;
}
// Find if the applicant 'u' can get a job
if (bpm(bpGraph, u, seen, matchR, N, M)) {
result++;
}
}
return result;
}