Category: Binary Search

Rearrange Array to find K using Binary Search algorithm without sorting

#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 = […]

Continue Reading

Minimum days to make Array elements with value at least K sum at least X

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, […]

Continue Reading

Minimize K to let Person A consume at least ceil(N/(M + 1)) candies based on given rules

  #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 […]

Continue Reading

Probability of obtaining pairs from two arrays such that element from the first array is smaller than that of the second array

  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 […]

Continue Reading

Minimize sum of absolute differences of same-indexed elements of two given arrays by at most one replacement

#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] - […]

Continue Reading