Maximum area that can be saved in the given Matrix
Last Updated : 9 Dec, 2023
Consider a matrix of N rows and M columns. Initially, there is a cell at co-ordinate (X, Y), which is blue. Then the task is to place the Yellow color in an optimal cell such that the area with yellow color after ending below simulation is the maximum possible. The simulation takes place as follows (after putting a yellow color in a cell):
The cells, that are adjacent to blue cells and not colored yellow becomes blue.
The cells, that are adjacent to yellow cells and not colored blue becomes yellow.
Explanation of simulation
Examples:
Input: N = 3, M = 2, X = 1, Y = 2
Output: 4
Explanation: The blue colored cell is placed at (1, 2) initially. It will be optimal to place yellow cell at (2, 2). By doing so we will get yellow colored area 4 sq. units, which is maximum possible.
Explanation of test case 1
Input: N = 3, M = 4, X = 2, Y = 2
Output: 6
Explanation: It can be verified that placing yellow color at optimal cell will give us the maximum yellow colored area as 6 sq. units.
Approach: Implement the idea below to solve the problem
This problem is an observation based. Let us the observations:
The optimal way to choose the starting cell of yellow color is the adjacent cell of the first blue cell. We can place the first yellow color in a cell just above, just below, just left or just right of the initial blue cell.
Out of the above four choices, wherever you place the first yellow color cell, a part of the matrix will be blue.
Say, If you place the yellow cell just below the blue cell, all the rows above the blue will be colored with blue with the simulation for sure.
If we place the yellow cell above the blue cell, the lower part will get blue with the simulation.
Similarly for right and left positions.
Steps were taken to solve the problem:
Create a variable let say Total to store the total possible area of matrix and initialize it with N*M
Create six variables let say, A, B, C, D, C1, C2 and C3
Set all the above variables as follows:
A = X*M
B = (N-X+1)*M
C = Y*N
D = (M-Y+1)*N
C1 = min (A, B)
C2 = min (C, D)
C3 = min (C1, C2)
Output (Total - C3)
Code to implement the approach:
C++
//code by flutterfly#include<iostream>// Function declarationvoidMax_Area(intN,intM,intX,intY);// Driver Functionintmain(){// InputsintN=3,M=2,X=1,Y=1;// Function callMax_Area(N,M,X,Y);return0;}voidMax_Area(intN,intM,intX,intY){// Total area of the matrixinttotal=N*M;// a,b,c,d are no.of blue cells according// to the position of the yellow cellintA,B,C,D,C1,C2,C3;A=X*M;// The above grids are blueB=(N-X+1)*M;// The below grids are blueC=Y*N;// The left grids are blueD=(M-Y+1)*N;// The right grids are blueC1=std::min(A,B);C2=std::min(C,D);C3=std::min(C1,C2);// c3 has minimum of a,b,c,d// total-c3 gives you the number// of yellow gridsstd::cout<<total-C3<<std::endl;}
Java
// Java code to implement the approach// Driver ClasspublicclassMain{// Driver Functionpublicstaticvoidmain(String[]args){// InputsintN=3,M=2,X=1,Y=1;// Function callMax_Area(N,M,X,Y);}publicstaticvoidMax_Area(intN,intM,intX,intY){// Total area of the matrixinttotal=N*M;// a,b,c,d are no.of blue cells according// to the position of the yellow cellintA,B,C,D,C1,C2,C3;A=X*M;// The above grids are blueB=(N-X+1)*M;// The below grids are blueC=Y*N;// The left grids are blueD=(M-Y+1)*N;// The right grids are blueC1=Math.min(A,B);C2=Math.min(C,D);C3=Math.min(C1,C2);// c3 has minimum of a,b,c,d// total-c3 gives you number// of yellow gridsSystem.out.println(total-C3);}}
Python
# code by flutterflydefMax_Area(N,M,X,Y):# Total area of the matrixtotal=N*M# a,b,c,d are no. of blue cells according# to the position of the yellow cellA=X*M# The above grids are blueB=(N-X+1)*M# The below grids are blueC=Y*N# The left grids are blueD=(M-Y+1)*N# The right grids are blueC1=min(A,B)C2=min(C,D)C3=min(C1,C2)# C3 has the minimum of a, b, c, d# total - C3 gives you the number# of yellow gridsprint(total-C3)# InputsN,M,X,Y=3,2,1,1# Function callMax_Area(N,M,X,Y)
C#
//code by flutterflyusingSystem;publicclassMainClass{// Function declarationpublicstaticvoidMax_Area(intN,intM,intX,intY){// Total area of the matrixinttotal=N*M;// a,b,c,d are no. of blue cells according// to the position of the yellow cellintA=X*M;// The above grids are blueintB=(N-X+1)*M;// The below grids are blueintC=Y*N;// The left grids are blueintD=(M-Y+1)*N;// The right grids are blueintC1=Math.Min(A,B);intC2=Math.Min(C,D);intC3=Math.Min(C1,C2);// C3 has the minimum of a, b, c, d// total - C3 gives you the number// of yellow gridsConsole.WriteLine(total-C3);}// Driver FunctionpublicstaticvoidMain(string[]args){// InputsintN=3,M=2,X=1,Y=1;// Function callMax_Area(N,M,X,Y);}}
JavaScript
// Function declarationfunctionMax_Area(N,M,X,Y){// Total area of the matrixconsttotal=N*M;// a, b, c, d are no. of blue cells according// to the position of the yellow cellconstA=X*M;// The above grids are blueconstB=(N-X+1)*M;// The below grids are blueconstC=Y*N;// The left grids are blueconstD=(M-Y+1)*N;// The right grids are blueconstC1=Math.min(A,B);constC2=Math.min(C,D);constC3=Math.min(C1,C2);// C3 has the minimum of a, b, c, d// total - C3 gives you the number// of yellow gridsconsole.log(total-C3);}// InputsconstN=3,M=2,X=1,Y=1;// Function callMax_Area(N,M,X,Y);