Knowee
Questions
Features
Study Tools

import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-- > 0) { String[] np = br.readLine().split(" "); int n = Integer.parseInt(np[0]); int p = Integer.parseInt(np[1]); String[] arr = br.readLine().split(" "); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = Integer.parseInt(arr[i]); } Arrays.sort(a); int[] prefix = new int[n]; prefix[0] = a[0]; for(int i = 1; i < n; i++) { prefix[i] = a[i] + prefix[i - 1]; } int ans = a[p - 1] * p - prefix[p - 1]; for(int i = p; i < n; i++) { ans = Math.min(ans, a[i] * p - (prefix[i] - prefix[i - p])); } System.out.println(ans); } } }

Question

import java.util.; import java.io.;

public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); while(t-- > 0) { String[] np = br.readLine().split(" "); int n = Integer.parseInt(np[0]); int p = Integer.parseInt(np[1]); String[] arr = br.readLine().split(" "); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = Integer.parseInt(arr[i]); } Arrays.sort(a); int[] prefix = new int[n]; prefix[0] = a[0]; for(int i = 1; i < n; i++) { prefix[i] = a[i] + prefix[i - 1]; } int ans = a[p - 1] * p - prefix[p - 1]; for(int i = p; i < n; i++) { ans = Math.min(ans, a[i] * p - (prefix[i] - prefix[i - p])); } System.out.println(ans); } } }

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This Java program reads an integer t from the input, which represents the number of test cases. For each test case, it reads two integers n and p, and an array of n integers. It then sorts the array in ascending order and calculates the prefix sum of the array. The prefix sum of an array is an array where each element is the sum of all the elements before it in the original array, including the element itself.

The program then calculates an initial answer as the product of the p-th smallest number (0-indexed) and p, minus the sum of the first p numbers. It then iterates over the rest of the array, updating the answer to be the minimum of the current answer and the product of the current number and p, minus the sum of the previous p numbers.

Finally, the program prints the answer for each test case. The answer represents the minimum total cost to buy p items, where the cost of each item is equal to the maximum price of any item bought so far.

This problem has been solved

Similar Questions

import java.io.*;import java.util.*;public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int Myint = scanner.nextInt(); double d = scanner.nextDouble(); scanner.nextLine(); String mystring = scanner.nextLine(); System.out.println(mystring); System.out.println(d); System.out.println(Myint); /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ }}

public class Hello{ public static void main(String args[]) { int num = 190; String rev_num = " " ; while (num!=0){ int digit = num%10; rev_num = rev_num+digit; num = num /= 10 ; } System.out.println((Integer.parseInt(rev_num)));} } Exception in thread "main" java.lang.NumberFormatException: For input string: " 091" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:654) at java.base/java.lang.Integer.parseInt(Integer.java:786)

import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number of elements in the array"); int n = sc.nextInt(); //Declare the array int arr[ ]=new int[n]; //Get the elements from user System.out.println("Enter the elements of the array"); for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } //Code to find the sum of odd elements in the array int sum=findSumOddElements();   // invoke the method System.out.println("The sum is "+);        } public int findSumOddElements() { int sum=0; for(int i=0;i<;i++) { if(()!=0) { sum=sum+arr[i]; } }   ;  //return statement }}

rect answerWhat will be the output of the following Java code?    class codetantra     {        public static void main(String args[])         {            double m = 295.30;            int  n = 330;            byte o = (byte) m;            byte p = (byte) n;            System.out.println(o + " "  + p);        }     }

n this challenge, you must read an integer, a double, and a String from stdin, then print the values according to the instructions in the Output Format section below. To make the problem a little easier, a portion of the code is provided for you in the editor.Note: We recommend completing Java Stdin and Stdout I before attempting this challenge.Input FormatThere are three lines of input:The first line contains an integer.The second line contains a double.The third line contains a String.Output FormatThere are three lines of output:On the first line, print String: followed by the unaltered String read from stdin.On the second line, print Double: followed by the unaltered double read from stdin.On the third line, print Int: followed by the unaltered integer read from stdin.To make the problem easier, a portion of the code is already provided in the editor.Note: If you use the nextLine() method immediately following the nextInt() method, recall that nextInt() reads integer tokens; because of this, the last newline character for that line of integer input is still queued in the input buffer and the next nextLine() will be reading the remainder of the integer line (which is empty).Sample Input423.1415Welcome to HackerRank's Java tutorials!Sample OutputString: Welcome to HackerRank's Java tutorials!Double: 3.1415Int: 42

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.