0% found this document useful (0 votes)
88 views7 pages

Coding Sollution (1)

The document contains coding solutions for various problems, including a Java implementation for calculating walls needed to contain a virus spread in a grid, finding the first and last position of a target in an array, and simple instructions for completing solutions for a supermarket and massage ordering tasks. Each section provides code snippets and brief explanations of the logic used. The document is structured with clear headings for each problem and includes example outputs.

Uploaded by

princekumar5252
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views7 pages

Coding Sollution (1)

The document contains coding solutions for various problems, including a Java implementation for calculating walls needed to contain a virus spread in a grid, finding the first and last position of a target in an array, and simple instructions for completing solutions for a supermarket and massage ordering tasks. Each section provides code snippets and brief explanations of the logic used. The document is structured with clear headings for each problem and includes example outputs.

Uploaded by

princekumar5252
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

🥰

Mahindra Rise Coding Solution


1. Corona Virus

2. First And Last Position

3. Super Market

4. Massage Orderaing

1.Corona Virus
import java.util.*;

public class CoronaVirus {

/* {1, 1, 1},
{1, 0, 1},
{1, 1, 1} output = 4 */

public static void main(String[] args) {


int[][] grid = {
{1, 1, 1,0,0,0,0,0,0},

Mahindra Rise Coding Solution 1


{1, 0, 1,0,1,1,1,1,1},
{1, 1, 1,0,0,0,0,0,0}
};
int m = grid.length;
int n = grid[0].length;

int wallsBuilt = coronaVirus(grid, m, n);


System.out.println("Walls built: " + wallsBuilt);
}

public static int coronaVirus(int[][] grid, int m, int n) {


int totalWalls = 0;
int[][] directions = {{0,1},{1,0},{0,-1},{-1,0}};

while (true) {
boolean[][] visited = new boolean[m][n];
List<Set<Integer>> regions = new ArrayList<>();
List<Set<Integer>> fronts = new ArrayList<>();
List<Integer> wallsNeeded = new ArrayList<>();

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


for (int j = 0; j < n; j++) {
if (grid[i][j] == 1 && !visited[i][j]) {
Set<Integer> region = new HashSet<>();
Set<Integer> front = new HashSet<>();
int[] walls = new int[1];

dfs(grid, i, j, visited, region, front, walls, directions, m, n);


regions.add(region);
fronts.add(front);
wallsNeeded.add(walls[0]);
}
}
}

if (regions.isEmpty()) break;

int idx = 0;

Mahindra Rise Coding Solution 2


for (int i = 1; i < fronts.size(); i++) {
if (fronts.get(i).size() > fronts.get(idx).size()) {
idx = i;
}
}

totalWalls += wallsNeeded.get(idx);

// Quarantine the selected region


for (int pos : regions.get(idx)) {
int r = pos / n;
int c = pos % n;
grid[r][c] = -1;
}

// Spread the virus for other regions


for (int i = 0; i < regions.size(); i++) {
if (i == idx) continue;
for (int pos : fronts.get(i)) {
int r = pos / n;
int c = pos % n;
if (grid[r][c] == 0)
grid[r][c] = 1;
}
}

boolean canSpread = false;


for (int[] row : grid)
for (int cell : row)
if (cell == 1)
canSpread = true;

if (!canSpread) break;
}

return totalWalls;
}

Mahindra Rise Coding Solution 3


private static void dfs(int[][] grid, int r, int c, boolean[][] visited,
Set<Integer> region, Set<Integer> front, int[] walls,
int[][] directions, int m, int n) {
visited[r][c] = true;
region.add(r * n + c);

for (int[] dir : directions) {


int nr = r + dir[0];
int nc = c + dir[1];
if (nr >= 0 && nc >= 0 && nr < m && nc < n) {
if (grid[nr][nc] == 0) {
walls[0]++;
front.add(nr * n + nc);
} else if (grid[nr][nc] == 1 && !visited[nr][nc]) {
dfs(grid, nr, nc, visited, region, front, walls, directions, m, n);
}
}
}
}
}

2. First And Last Position


import java.util.ArrayList;

public class FindFirstAndLast {


public static ArrayList<Integer> findFirstAndLast(int[] arr, int x) {
ArrayList<Integer> result = new ArrayList<>();
int first = -1;
int last = -1;

for (int i = 0; i < arr.length; i++) {


if (arr[i] == x) {
if (first == -1) {
first = i;
}
last = i;

Mahindra Rise Coding Solution 4


}
}

result.add(first);
result.add(last);
return result;
}

public static void main(String[] args) {


int[] arr = {1, 3, 5, 5, 5, 5, 67, 123, 125};
int target = 5;
ArrayList<Integer> result = findFirstAndLast(arr, target);
System.out.println(result); // Output: [2, 5]
}
}

3.Super Market

Mahindra Rise Coding Solution 5


Just You Have To Add 48 to 53 Line in Market.java (And Done Solution ✅)

4.Massage Ordering (Hi , Bay)

Mahindra Rise Coding Solution 6


Just Add Line 12 and 13 inside MainService.Java File (And Done Solution ✅)

Mahindra Rise Coding Solution 7

You might also like