How to useBitwise Operators in Javascript
In this approach, we are using the bitwise operation of the right shift. Here, we have to use the bitwise right shift (>>) and simple arithmetic operations to find the maximum number of pieces in N cuts.
Syntax:
operand1 >> operand2
Example: In this example, we will be printing the Maximum number of pieces in N cuts using bitwise operations.
Javascript
let input = 7; let hCuts = input >> 1; let vCuts = input - hCuts; let result = (hCuts + 1) * (vCuts + 1); console.log( "Max pieces for n = " + input + " is " + result); |
Max pieces for n = 7 is 20
JavaScript Program to Find Maximum Number of Pieces in N Cuts
We have given a square piece and the total number of cuts available (n), we need to write a JavaScript program to print the maximum number of pieces of equal size that can be obtained with the n cuts. Horizontal and Vertical cuts are allowed. We have given the example for better understanding.
Example:
Input : n = 7
Output : 20
Table of Content
- Using parseInt() Method
- Using Bitwise Operators
- Using Looping
- Using Recursive Function
So, let’s see each of the approaches with its implementation.