Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active May 27, 2018 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/ec2f8018ec82d6dbc3c9b625ee4d9654 to your computer and use it in GitHub Desktop.
Save thmain/ec2f8018ec82d6dbc3c9b625ee4d9654 to your computer and use it in GitHub Desktop.
public class MaximumProductCutting {
public int maxProdutRecursion(int n) {
// base case
if (n == 0 || n == 1) {
return 0;
}
// make all possible cuts and get the maximum
int max = 0;
for (int i = 1; i < n; i++) {
// Either this cut will produce the max product OR
// we need to make further cuts
max = Math.max(max,
Math.max(i * (n - i), i * maxProdutRecursion(n - i)));
}
//return the max of all
return max;
}
public static void main(String[] args) throws java.lang.Exception {
MaximumProductCutting i = new MaximumProductCutting();
System.out.println("Maximum Product: "+i.maxProdutRecursion(10));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment