mercredi 5 juin 2019

Always '-1 Output' ,In HackerRank "Electronics Shop" Question

This is the link to the question https://www.hackerrank.com/challenges/electronics-shop/problem

This is my Code, it is passing some cases but few outputs are wrong because it is returning '-1' as output for every input.

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        //Getting Input from the User
        int budget = scan.nextInt();
        int key_no = scan.nextInt();
        int usb_no = scan.nextInt();

        // Assigning some random value
        int maxkey = 0;
        int usbmax = 0;

        // Storing Cost of Keyboards in array
        int[] key_array = new int[key_no];
        for(int i=0;i<key_no;i++){
            key_array[i] = scan.nextInt();

            if(key_array[i] >= maxkey){
                maxkey = key_array[i];
            }
        }

        // Storing Cost of USB in array
        int[] usb_array = new int[usb_no];
        for(int j=0;j<usb_no;j++){
            usb_array[j] = scan.nextInt();

            if(usb_array[j] > maxkey){
                usbmax = usb_array[j];
            }
        }

        if(budget >= (maxkey+usbmax)){
            System.out.println(maxkey+usbmax);
        }
        else{
            System.out.println("-1");
        }

    }
}

10 2 3 (budget,key_no,usb_no)

3 1 (KeyBoard Costs)

5 2 8 (USB Costs)

Expected Ans: '9'. But Output: '-1'

Aucun commentaire:

Enregistrer un commentaire