Table of contents
1.
Introduction
2.
Brute Force Approach   
3.
Algorithm 
4.
C++ code
5.
Algorithm Complexity
6.
Efficient Approach  
7.
Algorithm 
8.
C++ code
9.
Algorithm Complexity
10.
FAQs
11.
Key Takeaways
Last Updated: Mar 27, 2024

Designing Competition

Author Riya
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This section will discuss the problem “Designing Competition”. First, let’s see the problem statement.


Problem statement-

Ninja has to participate in a designing competition. In this competition, he will be given a sheet of paper of length ‘l’ and breadth ‘b’. He has to cover this entire area of sheet with square colourful papers of equal dimensions. Each colourful square paper is of unit length and breadth, so we need exactly ‘l * b’ square papers to cover the entire sheet. Each square paper is diagonally split and colored in two colours - red and black as shown below:

                                      

 

There is only one rule of the competition that the participant should place colourful papers such that two adjacent square papers must not share the same colour on the edge. As shown below, design 1 is acceptable but design 2 is not acceptable as the last two squares share edges with the same colour.

There can be multiple design patterns that can be generated by placing these colourful square papers on the given sheet of dimension ‘l x b’ as each square can be rotated and placed in four ways. Ninja has yet not figured out which design he will submit for the competition and is wondering exactly how many total number of designs are possible. You have to help him to find out the total number of designs that are possible for a given pair of values of ‘l’ and ‘b’.

You will be given the dimension of sheet i.e. length and breadth of the sheet and you have to print the total number of designs possible for the given dimension. As this number may be large, print the total number of designs modulo 109 + 7.

Now that we understand the problem let's discuss the approach to solve it in the next section.

Also see, Euclid GCD Algorithm

Brute Force Approach   

This section will discuss the brute force approach to solve the problem described in the previous section. We have to find the total number of designs possible. Imagine the sheet as coordinates. We can observe that if we fix the square paper of coordinates (i-1,j)  and (i, j-1), then there will be only one possible orientation for coordinate (i,j) that will satisfy the rule of the competition. So, if we arrange the square papers in the first row and first column then the rest of the orientation will be uniquely determined.


There can be following four ways of placing square paper at (1, 1):

Now, each of  the remaining squares of the first row and first column has two possible orientations that will satisfy the rule of the competition.

So, the total number of designs possible for a sheet of dimension ‘l x b’ is 2(l + b).

Algorithm 

Step 1. First, the main function. Inside the main function, create variables ‘l’ and ‘b’ to store the given length and breadth of the sheet respectively, then call the function “numberOfDesigns()” to find the total number of designs possible.

Step 2. Next, create the function “numberOfDesigns()” to find the total number of designs possible for a given dimension of sheet. It takes the following input parameters:

  1. l: Length of the given sheet in the designing competition
  2. arr: Breadth of the given sheet in the designing competition
     

Step 3.  Inside the function “numberOfDesignsBinaryExp()”, first initialise a variable ‘ans’ and calculate 2(l + b) using a ‘for loop’

Step 4. Finally, print the calculated total number of designs possible for the given dimension of sheet.

C++ code

// C++ program to count total number of designs possible
#include <bits/stdc++.h>
using namespace std;


#define mod 1000000007


// Function to find the total number of designs possible for a given dimension
int numberOfDesigns(int l,int b)
{
	int ans = 1;

	for(int i=0; i < (l + b); i++)
	{
		ans = (ans * 2) % mod;
	}
  	
	return ans;
}


int main()
{

	// Given length of the sheet
	int l = 3;
   	
	// Given breadth of the sheet
	int b = 2;
    	
	// Call the function to find the total number of designs possible for the given dimension
	int ans = numberOfDesigns(l,b);
    	
	// Print the answer
	cout <<"The total number of designs possible for given dimension is: "<< ans << endl;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output
The total number of designs possible for given dimension is: 32

Algorithm Complexity

Time Complexity: O( l + b)

In the function “numberOfDesigns()” to find the number of designs possible for a given value of ‘l’ and ‘b’, we have run a ‘for loop’ to calculate 2(l+b). So, the time complexity is O(l + b), where ‘l’ and ‘b’ are length and breadth of the given sheet respectively.

Space Complexity: O(1) 

In the function “numberOfDesigns()” to find the number of designs possible for a given value of ‘l’ and ‘b’, , constant space is used. So, the space complexity is O(1).

Efficient Approach  

This approach of counting the total number of designs is the same as the previous one. But, here we will use binary exponentiation for calculating 2(l + b), which will be in logarithmic time complexity.

Algorithm 

Step 1. First, the main function. Inside the main function, create variables ‘l’ and ‘b’ to store the given length and breadth of the sheet respectively, then call the function “numberOfDesignsBinaryExp()” to find the total number of designs possible.

Step 2. Next, create the function “numberOfDesignsBinaryExp()” to find the total number of designs possible for a given dimension of sheet. It takes the following input parameters:

  1. l: Length of the given sheet in the designing competition
  2. arr: Breadth of the given sheet in the designing competition

Step 3.  Inside the function “numberOfDesignsBinaryExp()”, first initialise a variable ‘ans’ and calculate 2(l + b) using binary exponentiation. Create a ‘while loop’ that will run while ‘y’ is greater than zero. In each iteration, if y is odd, multiply it with ‘ans’ and take modulo. If y is even, then update the value of x. Finally after the completion of the while loop, return the answer.

Step 4. Finally, print the calculated total number of designs possible for the given dimension of sheet.
 

C++ code

// C++ program to count total number of designs possible
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007


// Function to find the total number of designs possible for a given dimension
int numberOfDesignsBinaryExp(int l,int b)
{
	int ans = 1;

	int x = 2;
	int y = l + b;

	x = x % mod;

	if (x == 0) return 0;
    	
	while (y > 0)
	{

		// If y is odd, multiply x with result
		if (y & 1) {
			ans = (ans * x) % mod;
		}
		
		// y must be even now
		y = y/2;
		x = (x*x) % mod;
	}
	return ans;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output
The total number of designs possible for given dimension is: 32

Algorithm Complexity

Time Complexity: O(log(l + b))

In the function “numberOfDesignsBinaryExp()” to find the number of designs possible for a given value of ‘l’ and ‘b’, we have used binary exponentiation to calculate 2(l+b)  in which ‘y’ is getting halved at  each iteration . So, the time complexity is O(log(l + b)), where ‘l’ and ‘b’ are length and breadth of the given sheet respectively.

Space Complexity: O(1) 

In the function “numberOfDesignsBinaryExp()” to find the number of designs possible for a given value of ‘l’ and ‘b’, , constant space is used. So, the space complexity is O(1).

FAQs

  1. What is Binary Exponentiation?
    Ans: Binary exponentiation is a technique to calculate the value of ‘xy’ in ‘log(y)’ number of  multiplications only instead of ‘y’ number of multiplications.
  2. How is the efficient approach discussed here better as compared to the brute force approach?
    Ans: The problem discussed above is reduced to calculate 2(l + b). In the brute force approach, 2(l + b) is calculated using a ‘for loop’ which takes O(l + b) time but in the efficient approach, 2(l + b) is calculated using binary exponentiation technique which takes O(log(l + b)) time.
     

Key Takeaways

This article discussed the problem “Designing Competition'', a brute force approach to the problem and an efficient approach based on binary exponentiation, their C++ implementation, and its time and space complexity. If you want to solve more problems for practice, you can visit Coding Ninjas Studio.

We hope that this blog helped you enhance your knowledge regarding the use of binary exponentiation in problems and its C++ implementation. 

If you think that this blog helped you, then share it with your friends!. Refer to the DSA C++ course for more information.

Do upvote our blog to help other ninjas grow. Happy Coding!

Until then, All the best for your future endeavours, and Keep Coding.

Live masterclass