MAXIMUM PIECES OF A ROPE AND WALMART TECHNICAL QUESTION

 package com.company;


import java.util.Arrays;

public class CWR_Gfg_MaximumPiecesOfRope {

// Walmart question
public static int[] array(int [] arr, int n){

for(int i = 0; i < n; i++){
if(arr[i] != i){
arr[i] = -1;
}
}
return arr;
}
// Maximum pieces of a rope
public static int maxPieces(int n, int a, int b, int c){
if(n == 0){
return 0;
}
if( n < 0){
return -1;
}

int x = maxPieces(n-a, a, b, c);
int y = maxPieces(n-b, a, b, c);
int z = maxPieces(n-c, a, b, c);
int res = Math.max(x, y);
int res1 = Math.max(res,z);
if(res1 == -1){
return -1;
}

return res1 + 1;
}

public static void main(String[] args) {
// System.out.println(maxPieces(23,11,9,12));
int [] a = {0,1,5,4,10,9};
System.out.println(Arrays.toString(array(a, 6)));
}
}

Comments