Maximize the value of expression [i.j - K.(Ai | Aj)] over all pairs (i, j) in given Array
Last Updated :
21 Mar, 2022
Given an array A[] of length N and an integer K, the task is to maximize the value of expression [i.j - K.(Ai | Aj)] over all pairs (i, j) in given Array, where (1 ≤ i < j ≤ N) and | denotes Bitwise OR operator.
Examples:
Input: A[] = {5, 20, 1, 0, 8, 11}, K = 10
Output: 2
Explanation: The maximum value of the expression f(i, j) = i.j - K.(A[i] | A[j]) can be found for below pair:
f(3, 4) = 3.4 - 10.(1 | 0) = 2
Input: A[] = {1, 5, 6, 7, 8, 19}, K = 3
Output: -9
Approach: The following observations have to be made:
Let f(i, j) = i.j - K.(Ai | Aj).
=> Notice that for f(i, j) to be maximum, i.j should be maximum and K.(Ai | Aj) should be minimum.
=> Thus, in the best case, f(i, j) will be maximum if
=> K.(Ai | Aj) is equal to 0
=> i = (N-1) and j = N.
=> So, the maximum value of the expression can be f(i, j) = (N-1)*N.
=> Also, the minimum value will be obtained by subtracting the maximum value of K.(Ai | Aj) from (N-1)*N.
=> It is known that
a | b < 2*max(a, b) where | is the bitwise OR operator
From the above property and the given constraints, it can be inferred:
- The maximum value of (Ai | Aj) can be 2*N. because (0 ≤ Ai ≤ N)
- The maximum value of K can be 100 because (1 ≤ K ≤ min(N, 100)).
- So, the minimum value of f(i, j) will be:
f(i, j) = (N-1)*N - K*(Ai | Aj)
= (N-1)*N - 100*2*N
= N*(N - 201)
- It can be easily observed that the resultant answer will always lie between:
N*(N-201) <= ans <= (N-1)*N
- Also, notice that for i = N - 201 and j = N,
- the maximum value of f(i, j) will be N*(N - 201),
- which in turn is the minimum value of f(i, j) for i = N - 1 and j = N.
- Thus, the maximum value of the expression has to be checked from i = N - 201 to i = N and j = i+1 to j = N.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum
// value of the given expression
long long int maxValue(int N, int K,
long long int A[])
{
// Stores the maximum value of
// the given expression
long long int ans = LLONG_MIN;
// Nested loops to find the maximum
// value of the given expression
for (long long int i = max(0, N - 201);
i < N; ++i) {
for (long long int j = i + 1;
j < N; ++j) {
ans = max(ans, (i + 1) * (j + 1)
- K * (A[i] | A[j]));
}
}
// Return the answer
return ans;
}
// Driver Code
int main()
{
// Given input
int N = 6, K = 10;
long long int A[N]
= { 5, 20, 1, 0, 8, 11 };
// Function Call
cout << maxValue(N, K, A);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find the maximum
// value of the given expression
static int maxValue(int N, int K,
int A[])
{
// Stores the maximum value of
// the given expression
int ans = Integer.MIN_VALUE;
// Nested loops to find the maximum
// value of the given expression
for (int i = Math.max(0, N - 201);
i < N; ++i) {
for (int j = i + 1;
j < N; ++j) {
ans = Math.max(ans, (i + 1) * (j + 1)
- K * (A[i] | A[j]));
}
}
// Return the answer
return ans;
}
// Driver Code
public static void main (String[] args)
{
// Given input
int N = 6, K = 10;
int A[] = { 5, 20, 1, 0, 8, 11 };
// Function Call
System.out.println( maxValue(N, K, A));
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python3 program for the above approach
LLONG_MIN = -9223372036854775808
# Function to find the maximum
# value of the given expression
def maxValue(N, K, A):
# Stores the maximum value of
# the given expression
ans = LLONG_MIN
# Nested loops to find the maximum
# value of the given expression
for i in range(max(0, N - 201), N):
for j in range(i + 1, N):
ans = max(ans, (i + 1) * (j + 1)
- K * (A[i] | A[j]))
# Return the answer
return ans
# Driver Code
if __name__ == "__main__":
# Given input
N, K = 6, 10
A = [5, 20, 1, 0, 8, 11]
# Function Call
print(maxValue(N, K, A))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the maximum
// value of the given expression
static int maxValue(int N, int K,
int []A)
{
// Stores the maximum value of
// the given expression
int ans = Int32.MinValue;
// Nested loops to find the maximum
// value of the given expression
for (int i = Math.Max(0, N - 201);
i < N; ++i) {
for (int j = i + 1;
j < N; ++j) {
ans = Math.Max(ans, (i + 1) * (j + 1)
- K * (A[i] | A[j]));
}
}
// Return the answer
return ans;
}
// Driver Code
public static void Main ()
{
// Given input
int N = 6, K = 10;
int []A = { 5, 20, 1, 0, 8, 11 };
// Function Call
Console.WriteLine(maxValue(N, K, A));
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code for the above approach
// Function to find the maximum
// value of the given expression
function maxValue(N, K, A)
{
// Stores the maximum value of
// the given expression
let ans = Number.MIN_VALUE;
// Nested loops to find the maximum
// value of the given expression
for (let i = Math.max(0, N - 201);
i < N; ++i) {
for (let j = i + 1;
j < N; ++j) {
ans = Math.max(ans, (i + 1) * (j + 1)
- K * (A[i] | A[j]));
}
}
// Return the answer
return ans;
}
// Driver Code
// Given input
let N = 6, K = 10;
let A
= [5, 20, 1, 0, 8, 11];
// Function Call
document.write(maxValue(N, K, A));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
Maximize the Absolute Value of Expression |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j| Given two integer arrays arr1[] and arr2[] of equal length, the task is to find the maximum value of the expression: |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|, where the maximum is taken over all valid indices i and j satisfying 0 <= i, j < arr1.length. Return the maximum value of th
7 min read
Find the maximum possible value of a[i] % a[j] over all pairs of i and j Given an array arr[] of N positive integers. The task is to find the maximum possible value of a[i] % a[j] over all pairs of i and j. Examples: Input: arr[] = {4, 5, 1, 8} Output: 5 If we choose the pair (5, 8), then 5 % 8 gives us 5 which is the maximum possible. Input: arr[] = {7, 7, 8, 8, 1} Outp
6 min read
Maximize value of (a[i]+i)*(a[j]+j) in an array Given an array with input size n, find the maximum value of (a[i] + i) * (a[j] + j) where i is not equal to j. Note that i and j vary from 0 to n-1 . Examples: Input : a[] = [4,5,3,1,10] Output : 84 Explanation: We get the maximum value for i = 4 and j = 1 (10 + 4) * (5 + 1) = 84 Input : a[] = [10,0
12 min read
Maximize value of (arr[i] - i) - (arr[j] - j) in an array Given an array, arr[] find the maximum value of (arr[i] - i) - (arr[j] - j) where i is not equal to j. Where i and j vary from 0 to n-1 and n is size of input array arr[]. Examples: Input : arr[] = {9, 15, 4, 12, 13} Output : 12 We get the maximum value for i = 1 and j = 2 (15 - 1) - (4 - 2) = 12 In
10 min read
Maximum value of expression (arr[i] + arr[j] * arr[k]) formed from a valid Triplet Given an array arr[] of N integers. The task is to find the maximum value of (arr[i] + arr[j] * arr[k]) among every triplet (i, j, k) such that arr[i] < arr[j] < arr[k] and i < j < k. If there doesn't exist any such triplets then print â-1". Examples: Input: arr[]={7, 9, 3, 8, 11, 10}Out
14 min read