You are on page 1of 2

public class IdealFlowerPotPlacement {

public static int[] findIdealPlacement(int length, int width, int[] flowerPotHeights, int
newFlowerPotHeight) {

// Create a 2D array to represent the room.

int[][] room = new int[length][width];

// Initialize the room array with the flower pot heights.

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

for (int j = 0; j < width; j++) {

room[i][j] = flowerPotHeights[i * width + j];

// Find the row and column of the last flower pot in the room.

int lastRow = length - 1;

int lastColumn = width - 1;

// Iterate over the room array, starting from the bottom row and moving up.

for (int i = lastRow; i >= 0; i--) {

// Find the column of the last flower pot in the row.

int lastColumnInRow = width - 1;

while (lastColumnInRow >= 0 && room[i][lastColumnInRow] <= newFlowerPotHeight) {

lastColumnInRow--;

// If the last column in the row is greater than or equal to zero, then the new flower pot can
be placed in that position.

if (lastColumnInRow >= 0) {

return new int[]{i, lastColumnInRow};

}
// If the new flower pot cannot be placed in any of the positions in the room, then return null.

return null;

public static void main(String[] args) {

// Read the input from the console.

Scanner scanner = new Scanner(System.in);

int length = scanner.nextInt();

int width = scanner.nextInt();

int[] flowerPotHeights = new int[length * width];

for (int i = 0; i < length * width; i++) {

flowerPotHeights[i] = scanner.nextInt();

int newFlowerPotHeight = scanner.nextInt();

// Find the ideal placement for the new flower pot.

int[] idealPlacement = findIdealPlacement(length, width, flowerPotHeights,


newFlowerPotHeight);

// Print the ideal placement to the console.

if (idealPlacement != null) {

System.out.println(idealPlacement[0] + " " + idealPlacement[1]);

} else {

System.out.println("No suitable placement found.");

You might also like