0

I have participated in coding contest, where I have attempt the given problem, so I am supposed to come up with solution which should not only passing all the test cases, also well optimized in terms of time and space complexity, I have passed 3 out of 7 but I am still not able to identify the rest of the test cases, it is possible that I might be missing something, please help me to rectify the below problem statement.

PROBLEM STATEMENT:

The Powerpuff Girls (100 Marks)

Professor Utonium is restless because of the increasing crime in the world. The number of villains and their activities has increased to a great extent. The current trio of Powerpuff Girls is not well to fight the evils of the whole world. Professor has decided to create the maximum number of Powerpuff Girls with the ingredients he has.

There are N ingredients required in a certain quantity to create a Powerpuff Girl. Professor has all the N ingredients in his laboratory and knows the quantity of each available ingredient. He also knows the quantity of a particular ingredient required to create a Powerpuff Girl. Professor is busy with the preparations and wants to start asap.

The villains, on the other hand, want to destroy the laboratory and stop Professor Utonium from creating more Powerpuff girls. Mojo Jojo is coming prepared with ammunition and Him is leading other villains like Princess, Amoeba Boys, Sedusa, Gangreen Gang etc.

Professor does not have much time as villains will reach the laboratory soon. He is starting the process but does not know the number of Powerpuff Girls which will be created. He needs your help in determining the maximum number of Powerpuff Girls which will be created with the current quantity of ingredients.

Example:

Professor Utonium requires 3 ingredients to make Powerpuff Girls. The 3 ingredients are present in the laboratory in the given quantity:

To make a Powerpuff Girl, Professor Utonium requires:

3 units of Ingredient A

6 units of Ingredient B

10 units of Ingredient C

The maximum number of Powerpuff Girls that can be created is 3 as, after 3, Professor will run out of Ingredient C.

Can you determine the maximum number?

Input Format

The first line of input consists of the number of ingredients, N

The second line of input consists of the N space-separated integers representing the quantity of each ingredient required to create a Powerpuff Girl.

The third line of input consists of the N space-separated integers representing the quantity of each ingredient present in the laboratory.

Constraints

1<= N <=10000000 (1e7)

0<= Quantity_of_ingredient <= LLONG_MAX 
Output Format

Print the required output in a separate line.

Sample TestCase 1

Input
4
2 5 6 3 
20 40 90 50 
Output
8

SOLUTION CODE

package com.mzk.poi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class PowerPuffGirls {

    private static final String SPACE = " ";
    private static final Integer INITAL_IDX = 0;
    private static final Integer LOWER_IDX = 1;
    private static final Integer SECOND_IDX = 2;
    private static final Integer MAX_LINES = 3;
    private static final Integer UPPER_IDX = 1000000;
    private static String[] UnitsArr = null;
    private static String[] totIngrdientsArr = null;
    private static int size = 0;

    public static void main(String[] args) {
        List<Integer> maxPowerPuffGirlsCreationList = new ArrayList<Integer>();
        Scanner stdin = new Scanner(System.in);
        String[] input = new String[MAX_LINES];
        try {
            for (int i = 0; i < input.length; i++) {
                input[i] = stdin.nextLine();
                if (validateIngredienInput(input[INITAL_IDX])) {
                    System.exit(INITAL_IDX);
                }
            }
        } finally {
            stdin.close();

        }
        int numOfIngredients = Integer.parseInt(input[INITAL_IDX]);
        String units = input[LOWER_IDX];
        UnitsArr = units.split(SPACE);
        String ingredients = input[SECOND_IDX];
        totIngrdientsArr = ingredients.split(SPACE);

        size = UnitsArr.length;
        int[] specifiedArrayOfUnits = convertToIntegerArray(UnitsArr);
        int[] totIngredientInLabArray = convertToIntegerArray(totIngrdientsArr);
        for (int i = 0; i < size; i++) {
            totIngredientInLabArray[i] = Integer.parseInt(totIngrdientsArr[i]);
        }

        for (int i = 0; i < numOfIngredients; i++) {
            maxPowerPuffGirlsCreationList.add(totIngredientInLabArray[i] / specifiedArrayOfUnits[i]);
        }
        System.out.println(Collections.min(maxPowerPuffGirlsCreationList));

    }
    /**
     * This method validates the first input 
     * @param noOfIngredients
     * @return boolean
     */
    private static boolean validateIngredienInput(String noOfIngredients) {
        int numOfIngredients = Integer.parseInt(noOfIngredients);
        boolean result = false;
        if (numOfIngredients <= LOWER_IDX && numOfIngredients <= UPPER_IDX) {
            result = true;
            return result;
        }
        return result;
    }

    /**
     * This utility method convert the String array to Integer array
     * @param size
     * @param specifiedArrayOfUnits
     * @return int[]
     */
    private static int[] convertToIntegerArray(String[] arrayToBeParsed) {
        int array[] = new int[size];
        for (int i = INITAL_IDX; i < size; i++) {
            array[i] = Integer.parseInt(arrayToBeParsed[i]);
        }
        return array;
    }
}

The above solution has passed 3 test cases, remaining 7 are un identfied,Please help me to rectify or improve this code.

  • In constraints it is mentioned that- "0<= Quantity_of_ingredient <= LLONG_MAX". You are using integer array to store both. use array of longs to represent Quantity_of_ingredient and retry, – Mohd Waseem May 09 '20 at 10:48
  • Please [edit] your question to include a description of what you mean by "identify" the failed test. Depending on how you run your tests, the test framework should tell you which tests failed and which passed. Then you can easily point/see the test which fails. – Progman May 09 '20 at 14:50
  • I have run this code on techgig's environment, where it runs the 10 test cases among which my code has passed only three, I want the help from you guys to identify the test cases can be possible –  May 09 '20 at 16:34

1 Answers1

2

There are several issues with your code:

try {
    for (int i = 0; i < input.length; i++) {
        input[i] = stdin.nextLine();
        if (validateIngredienInput(input[INITAL_IDX])) {
            System.exit(INITAL_IDX);
        }
    }
} finally {
    stdin.close();
}

You don't need to check the first row each time you read one line from the input. Instead you run the test only once. You can run it once you read the first line or after you read the whole input. The code can be look like this:

for (int i = 0; i < input.length; i++) {
    input[i] = stdin.nextLine();
}
if (!validateIngredienInput(input[INITAL_IDX])) {
    System.exit(...);
}

Also, it is not clear what the return value of validateIngredienInput() means. Is true a correct input or is false? Your if() statements checks for the return value being true and quits if it is.

private static boolean validateIngredienInput(String noOfIngredients)
{
    int numOfIngredients = Integer.parseInt(noOfIngredients);
    boolean result = false;
    if (numOfIngredients <= LOWER_IDX && numOfIngredients <= UPPER_IDX) {
        result = true;
        return result;
    }
    return result;
}

Your if() condition doesn't make any sense. You are checking if the number of different ingredients is <= 0 and <= 1. This also means that you could rewrite is as just if (numOfIngredients <= 0). But with the description above it is unclear if the validation should return true or false. Depending on what this method should do it can be written as:

private static boolean validateIngredienInput(String noOfIngredients)
{
    int numOfIngredients = Integer.parseInt(noOfIngredients);
    return numOfIngredients > 0;
}

This will return true if the value is greater than 0.

UnitsArr = units.split(SPACE);

Variable and field names should begin in lowercase, so they don't get confused with class names, which start in uppercase. It looks like UnitsArr is a class, but it is actually a field. So you should rename your field to unitsArr:

unitsArr = units.split(SPACE);

Also check the requirement/format of the input and the limits:

Constraints
1<= N <=10000000 (1e7)
0<= Quantity_of_ingredient <= LLONG_MAX 

As you see the quantity of ingredient can be a long value, but you are using Integer.parseInt() and int[] arrays for storing the quantity. You must change this to long to read long values from the input.

int[] totIngredientInLabArray = convertToIntegerArray(totIngrdientsArr);
for (int i = 0; i < size; i++) {
   totIngredientInLabArray[i] = Integer.parseInt(totIngrdientsArr[i]);
}

You are converting the String array to an int array with your helper method convertToIntegerArray(), but then you are doing it again with the for loop. You can skip one of the two.

You might also want to check at: What is a debugger and how can it help me diagnose problems?

Progman
  • 16,827
  • 6
  • 33
  • 48
  • I need to accept multi line input and i need to check the very first integer that is ingredient, that's why I introduced the method `validateIngredienInput` –  May 09 '20 at 14:33
  • BTW i am really thankful for all your suggestions, could you please address the subject of the posts. –  May 09 '20 at 14:37