Minimum value of X such that sum of arr[i] – X raised to the power of brr[i] is less than or equal to K
Last Updated :
09 Aug, 2021
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[] = {2, 1, 4, 3, 5} brr[] = { 4, 3, 2, 3, 1}, K = 12
Output: 2
Explanation:
Consider the value of X as 2, then the value of the given expression is:
=> max(2 – 2, 0)4 + max(1 – 2, 0)3 + max(4 – 2, 0)2 + max(3 – 2, 0)3 +max(5 – 2, 0)1
=> 04 + 03 + 22 + 13 + 31 = 8 <= K(= 12).
Therefore, the resultant value of X is 2, which is minimum.
Input: arr[] = {2, 1, 4, 3, 5} brr[] = { 4, 3, 2, 3, 1}, K = 22
Output: 1
Naive Approach: The simplest approach to solve the given problem is to check for every value of X from 0 to the maximum element of the array and if there exists any value of X satisfying the given conditions, then print that value of X and break out of the loop.
Time Complexity: O(N*M), where, M is the maximum element of the array.
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using Binary Search to find the value of X and if a particular value of X satisfies the above condition, then, all the greater values will also satisfy, therefore, then try to search for lower values. Follow the steps below to solve the problem:
- Define a function check(a[], b[], k, n, x):
- Initialize the variable sum as 0 to calculate the desired sum from the array arr[] and brr[].
- Iterate over the range [0, N] using variable i and add the value of pow(max(arr[i] – x, 0), brr[i]) to the variable sum.
- If the value of sum is less than equal to K, then, return true. Otherwise, return false.
- Initialize the variables, say low as 0 and high as maximum value of the array.
- Iterate in a while loop till low is less than high and perform the following steps:
- Initialize the variable mid as the average of low and high.
- Check the value of mid to see whether it satisfies the given conditions by calling the function check(arr[], brr[], k, n, mid).
- If the function check(arr[], brr[], n, k, mid) returns true then, update the high to mid. Otherwise, update the value of low to (mid + 1).
- After completing the above steps, return the value of low as the result from the function.
- After performing the above steps, print the value of low as the desired value of X as the answer.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
bool check( int a[], int b[], int k, int n, int x)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum = sum + pow (max(a[i] - x, 0), b[i]);
}
if (sum <= k)
return true ;
else
return false ;
}
int findMin( int a[], int b[], int n, int k)
{
int l = 0, u = *max_element(a, a + n);
while (l < u) {
int m = (l + u) / 2;
if (check(a, b, k, n, m)) {
u = m;
}
else {
l = m + 1;
}
}
return l;
}
int main()
{
int arr[] = { 2, 1, 4, 3, 5 };
int brr[] = { 4, 3, 2, 3, 1 };
int K = 12;
int N = sizeof (arr) / sizeof (arr[0]);
cout << findMin(arr, brr, N, K);
return 0;
}
|
Java
import java.io.*;
class GFG{
static boolean check( int a[], int b[], int k, int x)
{
int sum = 0 ;
for ( int i = 0 ; i < a.length; i++)
{
sum = sum + ( int )Math.pow(
Math.max(a[i] - x, 0 ), b[i]);
}
if (sum <= k)
return true ;
else
return false ;
}
static int findMin( int a[], int b[], int n, int k)
{
int l = 0 , u = ( int )1e9;
while (l < u)
{
int m = (l + u) / 2 ;
if (check(a, b, k, m))
u = m;
else
l = m + 1 ;
}
return l;
}
public static void main(String[] args)
{
int n = 5 ;
int k = 12 ;
int a[] = { 2 , 1 , 4 , 3 , 5 };
int b[] = { 4 , 3 , 2 , 3 , 1 };
System.out.println(findMin(a, b, n, k));
}
}
|
Python3
def check(a, b, k, n, x):
sum = 0
for i in range (n):
sum = sum + pow ( max (a[i] - x, 0 ), b[i])
if ( sum < = k):
return True
else :
return False
def findMin(a, b, n, k):
l = 0
u = max (a)
while (l < u):
m = (l + u) / / 2
if (check(a, b, k, n, m)):
u = m
else :
l = m + 1
return l
if __name__ = = '__main__' :
arr = [ 2 , 1 , 4 , 3 , 5 ]
brr = [ 4 , 3 , 2 , 3 , 1 ]
K = 12
N = len (arr)
print (findMin(arr, brr, N, K))
|
C#
using System;
public class GFG{
static bool check( int []a, int []b, int k, int x)
{
int sum = 0;
for ( int i = 0; i < a.Length; i++)
{
sum = sum + ( int )Math.Pow(
Math.Max(a[i] - x, 0), b[i]);
}
if (sum <= k)
return true ;
else
return false ;
}
static int findMin( int []a, int []b, int n, int k)
{
int l = 0, u = ( int )1e9;
while (l < u)
{
int m = (l + u) / 2;
if (check(a, b, k, m))
u = m;
else
l = m + 1;
}
return l;
}
public static void Main(String[] args)
{
int n = 5;
int k = 12;
int []a = { 2, 1, 4, 3, 5 };
int []b = { 4, 3, 2, 3, 1 };
Console.WriteLine(findMin(a, b, n, k));
}
}
|
Javascript
<script>
function check(a, b, k, n, x) {
let sum = 0;
for (let i = 0; i < n; i++) {
sum = sum + Math.pow(Math.max(a[i] - x, 0), b[i]);
}
if (sum <= k)
return true ;
else
return false ;
}
function max_element(a) {
let maxi = Number.MIN_VALUE;
for (let i = 0; i < a.length; i++) {
if (a[i] > maxi) {
maxi = a[i];
}
}
return maxi;
}
function findMin(a, b, n, k) {
let l = 0, u = max_element(a);
while (l < u) {
let m = Math.floor((l + u) / 2);
if (check(a, b, k, n, m)) {
u = m;
}
else {
l = m + 1;
}
}
return l;
}
let arr = [2, 1, 4, 3, 5];
let brr = [4, 3, 2, 3, 1];
let K = 12;
let N = arr.length;
document.write(findMin(arr, brr, N, K));
</script>
|
Time Complexity: O(N*log M), where, M is the maximum element of the array.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...