[Solved] Decorative Tiles with Java, C++

Decorative Tiles: Carlton Inn is planning to organize a Food Festival bringing together at one place, a wide variety of cuisines from across the world on account of Christmas. The Hotel Management has rented out a hall of an indoor Auditorium for this extravaganza.

The Event hall was of area N*M and the coordinators planned to decorate the hall’s flooringwith carpet tiles embellishing the opulence of the event. They have infinite number of tiles of size 2i X 2i, where i = 0, 1, 2,… so on. Their task is to find minimum number of tiles required to fill the given area with tiles.

Help them accomplish their task by writing a recursive method that finds the minimum number of tiles required to fill the given area with tiles.

Hence create a class named Tiles with the following method.

Method NameDescription
int minimumTiles(int,int)                                             This recursive method should return the minimum count of tiles required.

Create a driver class called Main. In the Main method, obtain input from the user in the console and display the minimum count of tiles required by calling the minimumTiles method present in the Tiles class.

[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.

All class names, attribute names and method names should be the same as specified in the problem statement.]
Input Format:

First line of the input is an integer ‘N’.
Second line of the input is an integer ‘M’.

Output Format:
Output an integer in a single line that gives the minimum count of tiles required.
Refer sample input and output for formatting specifications.

Sample Input 1:
5
6

Sample Output 1:
9

Sample Input 2:
4
4

Sample Output 2:
1

Solution

import java.util.Scanner;
public class Main {
     //sauravhathi
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        System.out.println(minimumTiles(n, m));
    }

    public static int minimumTiles(int n, int m) {
        if (n == 0 || m == 0)
         //sauravhathi
            return 0;
        else if (n % 2 == 0 && m % 2 == 0)
            return minimumTiles(n / 2, m / 2);
        else if (n % 2 == 0 && m % 2 == 1)
            return (n + minimumTiles(n / 2, m / 2));
        else if (n % 2 == 1 && m % 2 == 0)
            return (m + minimumTiles(n / 2, m / 2));
        else
         //sauravhathi
            return (n + m - 1 + minimumTiles(n / 2, m / 2));
    }
}
#include <iostream>
using namespace std;

int minimumTiles(int n, int m)
{

    if (n == 0 || m == 0)
        return 0;

    else if (n % 2 == 0 && m % 2 == 0)
        // sauravhathi
        return minimumTiles(n / 2, m / 2);

    else if (n % 2 == 0 && m % 2 == 1)
        // sauravhathi
        return (n + minimumTiles(n / 2, m / 2));

    else if (n % 2 == 1 && m % 2 == 0)
        // sauravhathi
        return (m + minimumTiles(n / 2, m / 2));

    else
        return (n + m - 1 + minimumTiles(n / 2, m / 2));
}

int main()
{
    int n, m;

    cin >> n;
    //sauravhathi
    cin >> m;

    cout << minimumTiles(n, m) << endl;
    return 0;
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *