1.
Inheritance 2
//java Solution
import [Link].*;
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) {
[Link] = registrationNo;
[Link] = modelNo;
/*------------------------------------------------------------------------------
// create getter for field getter name must be
// getRegistrationNo
// getModelNo
----------------------------------------------------------------------------*/
public String getRegistrationNo() {
return [Link];
}
public int getModelNo() {
return [Link];
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);
[Link] = name;
[Link] = color;
/*--------------------------------------------------------------------------
// create getters for all field getter name must be
// getName
// getColor
---------------------------------------------------------------------------*/
public String getName() {
return [Link];
}
public String getColor() {
return [Link];
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner([Link]);
String registrationNo = [Link]();
int modelNo = [Link]();
[Link]();
String name = [Link]();
String color = [Link]();
Car car = new Car( name, color,registrationNo, modelNo);
[Link]("Registration no : " + [Link]());
[Link]("Model no : " + [Link]());
[Link]("Car Name : " +[Link]());
[Link]("Color : " + [Link]());
2. Student Evaluation Criteria
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
String firstName = [Link]();
String lastName = [Link]();
int empId = [Link]();
int numScores = [Link]();
int[] testScores = new int[numScores];
for(int i = 0; i < numScores; i++){
testScores[i] = [Link]();
[Link]();
RDStudent s = new RDStudent(firstName, lastName, empId, testScores);
[Link]();
[Link]("Grade: " + [Link]());
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 < [Link]; i++) {
sum += testScores[i];
}
double average = sum * 1.0 / [Link];
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){
[Link] = firstName;
[Link] = lastName;
[Link] = identification;
// Print person data
public void printPerson(){
[Link](
"Name: " + lastName + ", " + firstName
+ "\nEMP ID: " + idNumber);
3. OOPs : custom sort Object List
import [Link].*;
<showDefaultCode>
class Candidate implements Comparable<Candidate> {
private String name;
private int marks;
public Candidate(String name, int marks) {
// write your code here ...
[Link] = name;
[Link] = 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 [Link]- [Link];
}
// create getter for both fields name, marks
// getter method name must be getName, getMarks
// write your code here...
public String getName() {
return [Link];
}
public int getMarks() {
return [Link];
}
</showDefaultCode>
class Main {
public static void main (String[] args) {
Scanner sc = new Scanner([Link]);
int t = [Link]();
ArrayList<Candidate> arr = new ArrayList<Candidate>();
[Link]();
while(t > 0) {
String name = [Link]();
int marks = [Link]();
Candidate candidate = new Candidate(name, marks);
[Link](candidate);
t--;
[Link](arr);
for(Candidate c : arr ){
[Link]([Link]() + " " + [Link]() );
4. Apartments With Flower Garden View
import [Link].*;
import [Link].*;
public class Main
public void solve(int[] heights) {
// Write your logic here
int targetBuildingCount=0;
int maxHeight = 0;
for(int i = [Link]-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 < [Link]; i++) {
if(heights[i] < 0) {
heights[i] = -heights[i];
buildings[buildingIndex++] = i;
for (int i=0; i<[Link]; i++)
[Link](buildings[i]);
//Print result here;
public static void main(String[] args) {
int n;
Scanner sc=new Scanner([Link]);
n=[Link]();
int[] array = new int[n];
for(int i=0; i<n; i++)
array[i]=[Link]();
[Link]();
Main obj = new Main();
[Link](array);
}
5. EMPLOYEE TAX REGIME
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link].*;
public class Main {
public static Scanner sc = new Scanner([Link]);
public static int rebate = 0;
public static void main(String[] args) {
[Link]([Link]("(\r\n|\r|\n)"));
String slabRate = [Link]();
String slabRates[] = [Link](" ");
String taxPercent = [Link]();
String taxPercents[] = [Link](" ");
rebate = [Link]();
String empTaxPaid = [Link]();
String empsTaxPaid[] = [Link](" ");
EmployeeSalary employeeSalary = new EmployeeSalary();
int totalSalary= [Link](slabRates,taxPercents,empsTaxPaid,rebate);
[Link](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(){
[Link](0);
for(int i=1;i<[Link]();i++){
int diff = [Link](i)-[Link](i-1);
diff *= [Link](i-1);
diff /= 100;
[Link](diff);
}
}
public static Integer getSalary(Integer tax,int rebate){
Integer salary = 0;
int i=1;
while(i<[Link]() && tax>=[Link](i)){
tax -= [Link](i);
salary+=[Link](i-1);
i++;
}
if(i==[Link]())
i--;
int temp= tax*100;
temp/=[Link](i);
salary += temp+rebate;
//[Link](salary);
return salary;
}
public static int calculateSalary(String[] slabint,String[] perint,String[] empTaxint,int rebate){
//write you logic here
if([Link]!=[Link])
[Link](1);
//[Link]("Success");
for(int i=0;i<[Link];i++){
[Link]([Link](slabint[i]));
[Link]([Link](perint[i]));
//[Link]("slab : "+[Link](i)+" perc :
"+[Link](i));
}
for(int i=0;i<[Link];i++){
[Link]([Link](empTaxint[i]));
//[Link]([Link](i));
}
populateTax();
for(int i=0;i<[Link]();i++){
totalSalary += getSalary([Link](i),rebate);
}
return totalSalary;
6. The shortest distance between all structures
import [Link].*;
class Main {
int[][] dist = null;
int[][] reach = null;
public int shortestDistance(int[][] grid) {
// Write your logic here
dist = new int[[Link]][grid[0].length];
reach = new int[[Link]][grid[0].length];
int buildings = 0;
for (int r=0;r<[Link];r++) {
for (int c=0;c<grid[0].length;c++) {
if (grid[r][c] == 1) {
buildings++;
bfs(grid, r, c);
}
}
if (buildings == [Link]*grid[0].length)
return -1;
int shortestDistance = Integer.MAX_VALUE;
for (int i=0;i<[Link];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[[Link]][grid[0].length];
Queue<int[]> queue = new LinkedList<>();
int[] start = {r, c, 0};
[Link](start);
int[] nx = {1, -1, 0, 0};
int[] ny = {0, 0, -1, 1};
while (![Link]()) {
int[] cc = [Link]();
for(int i=0;i<[Link];i++) {
int nextX = cc[0]+nx[i];
int nextY = cc[1]+ny[i];
int nextD = cc[2]+1;
if ( nextX<[Link] && nextX >=0
&& nextY<grid[0].length && nextY >=0
&& (grid[nextX][nextY] == 0)
&& !visited[nextX][nextY]) {
int[] nextC = {nextX, nextY, nextD};
[Link](nextC);
dist[nextX][nextY] += nextD;
visited[nextX][nextY] = true;
reach[nextX][nextY]++;
public static void main(String args[])
Scanner sc = new Scanner([Link]);
int M = [Link]([Link]());
int N = [Link]([Link]());
int[][] matrix = new int[M][N];
for (int row = 0; row < M; row++) {
for (int col = 0; col < N; col++) {
matrix [row][col] = [Link]([Link]());
[Link]();
Main obj = new Main();
[Link]([Link](matrix));
7. Real estate Business startup
import [Link];
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
int n = [Link]();
int m = [Link]();
int[][] clients = new int[n][2];
for(int clienti = 0; clienti < n; clienti++){
clients[clienti][0] = [Link]();
clients[clienti][1] = [Link]();
int[][] houses = new int[m][2];
for(int housei=0; housei < m; housei++){
houses[housei][0] = [Link]();
houses[housei][1] = [Link]();
RealEstate realEstate = new RealEstate();
[Link]([Link](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;
}