Cutting Boards in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct]

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Cutting Boards in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To Algorithm

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results. 

Advantages of Algorithms:

  • It is easy to understand.
  • Algorithm is a step-wise representation of a solution to a given problem.
  • In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

Link for the ProblemCutting Boards – Hacker Rank Solution

Cutting Boards – Hacker Rank Solution


Alice gives Bob a board composed of  wooden squares and asks him to find the minimum cost of breaking the board back down into its individual squares. To break the board down, Bob must make cuts along its horizontal and vertical lines.

To reduce the board to squares, Bob makes horizontal and vertical cuts across the entire board. Each cut has a given cost,  or  for each cut along a row or column across one board, so the cost of a cut must be multiplied by the number of segments it crosses. The cost of cutting the whole board down into  squares is the sum of the costs of each successive cut.

Can you help Bob find the minimum cost? The number may be large, so print the value modulo .

For example, you start with a  board. There are two cuts to be made at a cost of  for the horizontal and  for the vertical. Your first cut is across  piece, the whole board. You choose to make the horizontal cut between rows  and  for a cost of . The second cuts are vertical through the two smaller boards created in step  between columns  and . Their cost is . The total cost is  and .

Function Description

Complete the boardCutting function in the editor below. It should return an integer.

boardCutting has the following parameter(s):

  • cost_x: an array of integers, the costs of vertical cuts
  • cost_y: an array of integers, the costs of horizontal cuts

Input Format

The first line contains an integer , the number of queries.

The following  sets of lines are as follows:

  • The first line has two positive space-separated integers  and , the number of rows and columns in the board.
  • The second line contains  space-separated integers cost_y[i], the cost of a horizontal cut between rows  and  of one board.
  • The third line contains  space-separated integers cost_x[j], the cost of a vertical cut between columns  and  of one board.


Output Format

For each of the  queries, find the minimum cost () of cutting the board into  squares and print the value of .

Sample Input 0

2 2

Sample Output 0


Explanation 0
We have a  board, with cut costs  and . Our first cut is horizontal between  and , because that is the line with the highest cost (). Our second cut is vertical, at . Our first cut has a  of  because we are making a cut with cost  across  segment, the uncut board. The second cut also has a  of  but we are making a cut of cost  across  segments. Our answer is .

Sample Input 1

6 4
2 1 3 1 4
4 1 2

Sample Output 1


Explanation 1

image 116
Cutting Boards – Hacker Rank Solution
import java.util.*;

public class Solution {
    static int N;
    static int[] array;
    static long INF = Long.MAX_VALUE;
    static long mod = 1000000007;
    public static void main(String[] args) {
        Scanner in = new Scanner(;
        int T = in.nextInt();
        while(T-- != 0) {
            int m = in.nextInt();
            int n = in.nextInt();
            int[] y = new int[m];
            int[] x = new int[n];
            for(int i=1; i<m; i++) y[i] = in.nextInt();
            for(int i=1; i<n; i++) x[i] = in.nextInt();
            int i = 1;
            int j = 1;
            long count = 0;
            while(i < n || j < m) {
                long valX = -1;
                long valY = -1;
                if(i < n) valX = x[n-i];
                if(j < m) valY = y[m-j];
                if(valX > valY) {
                    count = (count + j*valX)%mod;
                } else {
                    count = (count + i*valY)%mod;

1,681 thoughts on “Cutting Boards in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct]”

  1. can i order generic mobic price [url=]how to buy generic mobic without dr prescription[/url] how to get cheap mobic without prescription

  2. where to get cheap mobic tablets [url=]buying generic mobic pills[/url] where buy generic mobic without rx

  3. mexico drug stores pharmacies [url=]mexican mail order pharmacies[/url] mexican border pharmacies shipping to usa

  4. Anna Berezina is a honoured framer and lecturer in the field of psychology. With a training in clinical luny and far-flung study sagacity, Anna has dedicated her career to arrangement philanthropist behavior and daft health: By virtue of her between engagements, she has made significant contributions to the field and has become a respected thought leader.

    Anna’s mastery spans various areas of thinking, including cognitive psychology, positive non compos mentis, and passionate intelligence. Her voluminous education in these domains allows her to provide valuable insights and strategies in return individuals seeking offensive proliferation and well-being.

    As an author, Anna has written distinct influential books that cause garnered widespread attention and praise. Her books tender practical suggestion and evidence-based approaches to help individuals lead fulfilling lives and cultivate resilient mindsets. Away combining her clinical dexterity with her passion on helping others, Anna’s writings secure resonated with readers roughly the world.

  5. Hello! I understand this is sort of off-topic however I needed to
    ask. Does running a well-established website such as
    yours require a lot of work? I am completely new to writing a blog but I do write
    in my journal every day. I’d like to start a blog
    so I will be able to share my own experience and views online.
    Please let me know if you have any kind of recommendations or tips for new
    aspiring blog owners. Appreciate it!

  6. zithromax antibiotic [url=]can you buy zithromax over the counter in canada[/url] purchase zithromax online

  7. According to reports, the acquisition of CoinDesk, if it happens, will help strengthen DCG’s financial condition. By signing up, you will receive emails about CoinDesk products and you agree to our terms & conditions and privacy policy. By signing up, you will receive emails about CoinDesk products and you agree to our terms & conditions and privacy policy. San Francisco, Aug 14: Cryptocurrency information firm CoinDesk is reportedly slashing its workforce, as a gaggle of traders was doubtless within the last phases to amass it for about $125 million, the media reported on Monday.
    Money Reimagined By signing up, you will receive emails about CoinDesk products and you agree to our terms & conditions and privacy policy. Consensus is CoinDesk’s annual crypto summit. It started in 2015. It has been held in New York City from 2017 to 2019, virtually in 2020 and 2021, and in Austin, Texas since 2022.
    Bitcoin mining is the simplest way to acquire cryptocurrency, provided you have the right hardware and the budget to pay the electricity bills. However, as more users join the Bitcoin network, you will have to compete with them for mining tokens, making it difficult to earn Bitcoins. OTC trading is considerably faster and offers more diverse payment options. However, buying bitcoin directly from individuals can be extremely risky. Meeting a complete stranger face to face to privately exchange money for cryptocurrency doesn’t always work out for the best. Platforms like LocalBitcoins offer a much safer solution and use an in-house escrow service to ensure the exchange process runs smoothly. LocalBitcoin locates a Bitcoin seller willing to accept cash and requests the prospective buyer to place the order for the number of Bitcoins wanted who then receives the account number of the seller for cash to be deposited. Once the buyer provides proof of the cash deposit, the Bitcoins are then sent to the buyer’s LocalBitcoin wallet.

  8. comprar viagra online en andorra [url=]sildenafilo 50 mg precio sin receta[/url] viagra online cerca de zaragoza

  9. comprar sildenafilo cinfa 100 mg espaГ±a [url=]comprar viagra[/url] sildenafilo sandoz 100 mg precio

  10. farmacias baratas online envГ­o gratis [url=]farmacia online envio gratis[/url] farmacias baratas online envГ­o gratis