📘
Placecom Question
  • ✅ABC? (Contest)
  • ✅Activity Selection (Contest)
  • ✅Absolute value discrepancy (Contest)
  • ✅Akash's Contest (Contest)
  • ✅Alternate Matrix Addition(Contest)
  • ✅Anti clockwise(Contest)
  • ✅Arpit's toy (Contest)
  • ✅Array Frequency Paradigm (Contest)
  • ✅Array Games (Contest)
  • ✅Arranging Students (Contest)
  • ✅Array Proximity (Contest)
  • ✅ATM Machine (Contest)
  • ✅Awesome Numbers(Contest)
  • ✅Bad Dish (Contest)
  • ✅Balanced Neighborhood (Contest)
  • ✅Birthday Gift (Contest)
  • ✅Boundary Traversal of Matrix(Contest)
  • ✅Buy and Sell Stock(Contest)
  • ✅Cakewalk? (Contest)
  • ✅Can we sort? (Contest)
  • ✅Candy Love (Contest)
  • ✅Candy Shopping (Contest)
  • ✅Choose Card optimally(Contest)
  • ✅Candy Store Earnings (Contest)
  • ✅Choose points (Contest)
  • ✅Concatenate Strings (Contest)
  • ✅Common digits in two numbers (Contest)
  • ✅Count 1's in binary array(Contest)
  • ✅Count Numbers (Contest)
  • ✅Count Total Digits in a Number (Contest)
  • ✅Counting Zeroes to Ones (Contest)
  • ✅Cyclic Rotation Paradigm (Contest)
  • ✅Derrangement Exercise (Contest)
  • ✅Diagonal Sum(Contest)
  • ✅Diet planning (Contest)
  • ✅Difference Array (Contest)
  • ✅Digits Rearrangement (Contest)
  • ❌Divisible by 8 (Contest)
  • ✅Door problem (Contest)
  • ✅Duplicates at a distance k (Contest)
  • ✅Easy - Peasy (Contest)
  • ✅Eat or be Eaten(Contest)
  • ✅Factorial - Recursion(Contest)
  • ✅Find element in 2D array (Contest)
  • ✅Generate all parentheses(Contest)
  • ✅The gabba test(Contest)
  • ✅Fruit Market (Contest)
  • ✅Frequency Sort (Contest)
  • ✅Floor in a Sorted Array(Contest)
  • ✅First non-repeating character in a String(Contest)
  • ✅Find unique(Contest)
  • ✅Get the Shadow (Contest)
  • ✅Good cells (Contest)
  • ✅Good circular array (Contest)
  • ✅Grid Magic (Contest)
  • ✅Guardians of Galaxy(Contest)
  • ✅Happy Balloons (Contest)
  • ✅Help James (Contest)
  • ✅Help Samar with Chopsticks (Contest)
  • ✅Hip Hip Array (Contest)
  • ✅Increment- Decrement Philosophy (Contest)
  • ✅Infinity Stones : Form Black Order - The Army of Thanos (Contest)
  • ✅Insert Operator(Contest)
  • ✅Integer Modification (contest)
  • ✅Inversion of array(Contest)
  • ✅Iso Lexo String (Contest)
  • ✅Jumping Numbers (Contest)
  • ✅K closest points(Contest)
  • ✅Knight game(Contest)
  • ✅K-Pairs (Contest)
  • ✅Kth Row of Pascal's Triangle(Contest)
  • ✅Largest Bitonic Subarray(Contest)
  • ✅Least Subarrays(Contest)
  • ✅Lexographical Rotation (Contest)
  • ✅Lone Sum Supremacy (Contest)
  • ✅Lucky Boys(Contest)
  • ❌Majority Element(Contest)
  • ✅Matrix problem(Contest)
  • ✅Matrix ZigZag Traversal(Contest)
  • ✅Max Candies(Contest)
  • ✅Max permute (Contest)
  • ✅Max Score in Quiz (Contest)
  • ✅Max sum column (Contest)
  • ✅Max XOR(Contest)
  • ❌Maximize Strength (Contest)
  • ✅Maximizing Difference(Contest)
  • ✅Maximum Area(Contest)
  • ❌Maximizing Difference(Contest)
  • ✅Maximum Force(Contest)
  • ✅Longest subarray not having more than K distinct elements(Contest)
  • ✅Maximum value of difference of a pair of elements and their Index(Contest)
  • ✅Minimum adjacent difference in a circular array easy(Contest)
  • ✅Minimum Element in Sorted and Rotated Array(Contest)
  • ✅Minimum operation - II(Contest)
  • ✅Missing two(Contest)
  • ✅Mohit and array(Contest)
  • ✅Most occurring elements(Contest)
  • ✅Move Zeros(Contest)
  • ✅Moving right (Contest)
  • ✅Max Score in Quiz (Contest)
  • ✅N integers: Easy (Contest)
  • ✅Nth node from end of linked list(Contest)
  • ✅Number of distinct numbers(Contest)
  • ✅Odd Sum Array (Contest)
  • ✅Optimal Goodies (Contest)
  • ✅Orange or Chocolate Candy? (Contest)
  • ✅Packing Rectangles (Contest)
  • ✅Pair sum (Contest)
  • ✅Passcode (Contest)
  • ✅Permutation - 2 (Contest)
  • ✅Polynomial equation(Contest)
  • ✅Power function(Contest)
  • ✅Power of Three(Contest)
  • ✅Print Pattern (Contest)
  • ✅Reduce to 1 (Contest)
  • ✅Remove Duplicates Inplace(Contest)
  • ✅Repeating numbers (Contest)
  • ✅Replace element(Contest)
  • ✅Ropes (Contest)
  • ✅Row Index Identification (Contest)
  • ✅Row with maximum 1's(Contest)
  • ✅Sara and Monsters (Contest)
  • ✅Science Camp (contest)
  • ✅Score bar(Contest)
  • ✅Search in rotated sorted array(Contest)
  • ✅Searching an element in a sorted array(Contest)
  • ✅Separating Negative and Positive numbers(Contest)
  • ❌Sequence Formation (Contest)
  • ❌Setwet's Fish Pond(Contest)
  • ✅Shopping (Contest)
  • ✅Simple Circle (Simple Contest)
  • ✅Simple Pairs(Contest)
  • ✅Simple Transpose (Contest)
  • ✅Skit Video (Contest)
  • ✅Smaller elements (Contest)
  • ✅Sort it (Contest)
  • ✅Special digit sum (Contest)
  • ✅Spiral rotation(Contest)
  • ✅Squiggly brackets (Contest)
  • ✅Strange element (Easy-Version)
  • ✅Subarray with given sum(Contest)
  • ✅Sum of largest elements(Contest)
  • ✅Sum Up(Contest)
  • ✅Swapping Matrix (Contest)
  • ✅Sweet Bunty (Contest)
  • ✅Teacher(Contest)
  • ✅The EndGame : Concatenated Words(Contest)
  • ✅The high median paradigm (Contest)
  • ✅Tower of Hanoi(Contest)
  • ✅Transpose of a matrix(Contest)
  • ✅Triangular matrix (Contest)
  • ✅Two Sum Maximization(Contest)
  • ✅Ultron : Vibranium Quest(Contest)
  • ✅Wakandan Point in Unsorted Array(Contest)
  • ✅Yet again Partition sort problem (Contest)
  • ✅Yet Another Array Rearrangement Problem (Contest)
  • ✅Zero Padding(Contest)
  • ✅First & Last (Contest)
  • ❌Fast Search (Contest)
Powered by GitBook
On this page

Bad Dish (Contest)

Bad Dish (Contest) easy Time Limit: 2 sec Memory Limit: 128000 kB

PreviousAwesome Numbers(Contest)NextBalanced Neighborhood (Contest)

Last updated 2 years ago

Problem Statement :

Bob ate a bad dish today which left K units of bad taste in his mouth. To counter this bad taste, he decided to buy some chocolates. There are infinite types of chocolates. The chocolate of the Mth type cost M cents and gives M units of good taste. When Bob eats a chocolate at time T, he get 1 unit of good taste per second during the next M seconds . The maximum amount of good taste Bob can get in a second is 1 unit . For example: If Bob ate two chocolates at T= 1 and T= 2 seconds and M = 2, he gets 1 unit of good taste for each T=1, 2, 3. The number of chocolates he can have is fixed to N and he can have the ith chocolate at T=A[i] seconds. Bob wants to know the minimum amount of money he needs to spend to get rid of all bad taste. 1 unit of bad taste is removed by 1 unit of good taste and Bob can have any amount of residual good taste left in his mouth. Input First line contains a single integer T — the number of test cases. First line of each test case contains two integers N and K — the number of chocolates and the units of bad taste. Second line of each test case contains N integers - denoting the time you had the ith chocolate.

Constraints 1 <= T <= 1000 1 <= N <= 100 1 <= K <= 10^18 1 <= A[i] <=10^9 A[i] < A[i+1] Output Print a single integer - the minimum amount Bob needs to spend. Example Sample Input 1: 1 2 5 1 5

Output 3

Explanation: If M=3, then Bob gets good taste at T = [1, 2, 3, 5, 6, 7] This is 6 units of good taste. So, he successfully countered his bad taste and this is the minimum M possible.

Sample Input 2: 2 3 10 2 4 10 4 1000 3 25 64 1337

Output 4 470

Explanation If M=4, then Bob gets good taste at T = [2, 3, 4, 5, 6, 7, 10, 11, 12, 13] This is 10 units of good taste. So, he successfully countered his bad taste and this is the minimum M possible.

link:

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
import java.io.*;
// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args)  throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t;
        t = Integer.parseInt(br.readLine());
        while(t>0){
            int N;
            long h;
            String line = br.readLine();
            String[] str = line.trim().split("\\s+");
            N = Integer.parseInt(str[0]);
            h = Long.parseLong(str[1]);
            long a[] = new long[N];
            line = br.readLine();
            str = line.trim().split("\\s+");
            for(int i =0;i<N;i++){
                a[i] = Long.parseLong(str[i]);
            }
            long ng = 0;
            long ok=h;
            while(ok-ng>1){
                long x =(ok+ng)/2;
                long sum =0;
                for(int n=0;n<N;n++){
                    if(n==N-1){
                        sum +=x;
                    }else{
                        sum += Math.min(x,a[n+1]-a[n]);
                    }
                }
                if(sum>=h){
                    ok =x;
                }else{
                    ng = x;
                }
            }
            System.out.print(ok+"\n");
            t--;
        }
    }
}
✅
https://my.newtonschool.co/playground/code/hkik5n3itmy1/