Wednesday, 1 Dec 2021
Category: Binary Search

Given an array arr[] consisting of N positive integers and a  positive integer K such that there are N countries, each country has arr[i] players, the task is to find the maximum number of teams that can be formed by forming teams of size K such that each player in the team is from a […]

Given an array arr[] of length N, the task is to find the number of Good Ranges in the array arr[].A Good Range is defined as the range of left and right indices, i.e, [L. R] in the array arr[] such that by removing all the numbers in the range [L, R] of the array arr[] and […]

#include using namespace std;  void Rearrange(int arr[], int K, int N){        int ans[N + 1];              int f = -1;      for (int i = 0; i < N; i++) {        ans[i] = -1;    }          K = find(arr, arr + N, K) – arr;                  vector smaller, greater;          for (int i = 0; i < N; i++) {                  if (arr[i] < arr[K])            smaller.push_back(arr[i]);                  else if (arr[i] > arr[K])            greater.push_back(arr[i]);    }      int low = […]

Given an array arr[] and brr[] both consisting of N integers and a positive integer K, the task is to find the minimum value of X such that the sum of the maximum of (arr[i] – X, 0) raised to the power of brr[i] for all array elements (arr[i], brr[i]) is at most K.Examples:Input: arr[] […]

Given an array arr[] of size N, and integers M and K, the task is to find the maximum possible value of the smallest array element by performing M operations. In each operation, increase the value of all elements in a contiguous subarray of length K by 1.Examples:Input: arr[ ] = {2, 2, 2, 2, […]

import java.io.*;class GFG{     static void findMinDays(int arr[], int R[], int N,                        int X, int K){                 int low = 0, high = X;    int minDays = -1;        while (low = K)            {                                                sum += temp;            }        }                        if (sum >= X)        {                                     minDays = mid;            high = mid – 1;        }                else        {            low = mid + 1;        }    }            System.out.println(minDays);}public static void main(String[] args){    int X = 100, K = 45;    int arr[] = { 2, 5, […]

#include using namespace std;  bool check(int K, int n, int m,           vector arr,           int good_share){    int candies = n, taken = 0;      while (candies > 0) {                                  taken += min(K, candies);        candies -= min(K, candies);                  for (int j = 0; j < m; j++) {                                        int consume = (arr[j] * candies) / 100;                                        candies -= consume;        }    }                return (taken >= good_share);}  void minimumK(vector &arr, int N,              int M){              int good_share […]

import java.util.*;  class GFG {                  static double probability(int[] arr1,                              int[] arr2)    {                int N = arr1.length;                  int M = arr2.length;                  double res = 0;                          Arrays.sort(arr2);                  for (int i = 0; i < N; i++) {                                                  int y = countGreater(                arr2, arr1[i]);                          res += y;        }                          res = (double)res / (double)(N * M);                  return res;    }                  static int countGreater(int[] arr,                            int k)    {        int n = arr.length;        int l = 0;        int r = n - 1;                                  int leftGreater […]

#include using namespace std;  int minAbsoluteSumDiff(vector A,                       vector B, int N){        int sum = 0;              int diff[N];      for (int i = 0; i < N; i++) {                  diff[i] = abs(A[i] - B[i]);                  sum += diff[i];    }          sort(A.begin(), A.end());              int bestDiff[N];      for (int i = 0; i < N; i++) {                          int j = lower_bound(A.begin(), A.end(), B[i])                - A.begin();                          if (j != 0 && j != N)            bestDiff[i] = min(abs(A[j] - […]

Size of smallest square that contains N non-overlapping rectangles of given dimensions Given two positive integers W and H and N rectangles of dimension W*H, the task is to find the smallest size of the square required such that all the N rectangles can be packed without overlapping. Examples:Input: N = 10, W = 2, H […]