Breaking

Post Top Ad

Your Ad Spot

Saturday, 24 October 2020

Cost of Ballons Problem Solution in Java || HackerEarth Basics Programming || Basics of Input/Output problems|| Practise Problems

Cost of Ballons Problem Solution

Problem Statement: 

You are conducting a contest .... while purchasing balloons.


Cost of ballons problem hackerrank solution
Cost of Ballons problem


Detailed Problem Statement: Cost of Ballons

Solution Code in Java :

import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
  public static void main(String args[]) throws Exception {

    //testCases t
    BufferedReader br = new BufferedReader(new InputStreamReader(System. in ));
    int t = Integer.parseInt(br.readLine());
    if (t >= 1 && t <= 10) {
      for (int q = 0; q < t; q++) {
        int x1 = 0,
        x2 = 0,
        n = 0,
        counterCol0 = 0,
        counterCol1 = 0,
        result = 0;
        int[] a = new int[2];
        int[][] arr = new int[10][2];

        String lines = br.readLine();

        String[] strs = lines.trim().split("\\s+");

        for (int i = 0; i < strs.length; i++) {
          a[i] = Integer.parseInt(strs[i]);
        }
        x1 = a[0]; //ballon numbers
        x2 = a[1]; // ballons numbers

        //n number of participants
        n = Integer.parseInt(br.readLine());
        if (n >= 1 && n <= 10) {

          for (int i = 0; i < n; i++) {
            String line2 = br.readLine();

            String[] str2 = line2.trim().split("\\s+");
            for (int y = 0; y < 2; y++) {
              arr[i][y] = Integer.parseInt(str2[y]);
              //storing input as 1 1
              //                  1 0
              //now logic comes
            }
          }
        }
        //here reading the participant answer from arr error
        for (int i = 0; i < n; i++) {
          for (int y = 0; y < 2; y++) {
            if (y == 0) {
              if (arr[i][y] == 1) counterCol0++;
              //System.out.println(counterCol0 + ""+arr[i][y]);
            }
            if (y == 1) {
              if (arr[i][y] == 1) counterCol1++;
              //    System.out.println(counterCol1 + ""+arr[i][y]);
            }

          }
        }
        //
        if (counterCol0 >= counterCol1) {
          //col0 is max
          if (x1 <= x2) {
            result = (x2 * counterCol1) + (x1 * counterCol0);

          }
          else {
            //x1 is max
            result = (x1 * counterCol1) + (x2 * counterCol0);
          }
        }
        else if (counterCol0 < counterCol1) {
          if (x1 <= x2) {
            //x2 is max
            result = (x2 * counterCol0) + (x1 * counterCol1);

          }
          else {
            //x1 is max
            result = (x1 * counterCol0) + (x2 * counterCol1);
          }
        }
        System.out.println(result);
      }
    }

  }

}

Hello Everyone i hope your Code works,i had test it on my PC using open JDK 1.8 and on HackerEarth open JDK 1.7 version. Comment down for any query, errors and new Problems.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages