Break The Integer

Ninja is given an integer ‘N’ . One day Ninja decides to break the integer into K positive parts (K>=2) such that the sum of all parts is equal to ‘N’.Ninja wants to maximize the product of all the ‘K’ parts.

Help Ninja in helping what maximum possible product it can obtain by breaking the integer ‘N’ into ‘K’ positive parts where ‘K’ can be any integer greater than 2.

Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases. 

The first and only line of the test case consists of a single integer ‘N’.
Output Format:
Return the maximum possible product Ninja can obtain by breaking integer ‘N’ into ‘K’ parts.
Note:
 You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= ‘T’ <= 11
2 <= ‘N’ <= 55

Time Limit: 1 sec
CodingNinjas
author
2y

The approach would be to think the max product that we could get on breaking a number N into two factors. This product can be represented by the following function : f = x(N-x).
The maximum of this fu...read more

CodingNinjas
author
2y
Brute Force

The main idea is to break the given integer ‘N’ into all possible ways and out of all possible ways the one with the maximum product will be the answer.

Algorithm:

  • Create a recursive functi...read more
CodingNinjas
author
2y
Memoisation Approach

In Approach 1 there are many overlapping sub-cases Hence we will apply memoization in Approach 1 to avoid calculating sub-problems many times.

In the above sub-tree, we are calculat...read more

CodingNinjas
author
2y
Break in 2 or 3

The main idea is to use the observation when the given number is broken into 2 and 3’s will give a maximum answer. If n%3 == 0 it means that the given number is a multiple of 3 hence b...read more

Add answer anonymously...
Info Edge Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter