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++
#include <bits/stdc++.h>
using namespace std;
long long int maxValue( int N, int K,
long long int A[])
{
long long int ans = LLONG_MIN;
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 ans;
}
int main()
{
int N = 6, K = 10;
long long int A[N]
= { 5, 20, 1, 0, 8, 11 };
cout << maxValue(N, K, A);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static int maxValue( int N, int K,
int A[])
{
int ans = Integer.MIN_VALUE;
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 ans;
}
public static void main (String[] args)
{
int N = 6 , K = 10 ;
int A[] = { 5 , 20 , 1 , 0 , 8 , 11 };
System.out.println( maxValue(N, K, A));
}
}
|
Python3
LLONG_MIN = - 9223372036854775808
def maxValue(N, K, A):
ans = LLONG_MIN
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 ans
if __name__ = = "__main__" :
N, K = 6 , 10
A = [ 5 , 20 , 1 , 0 , 8 , 11 ]
print (maxValue(N, K, A))
|
C#
using System;
class GFG {
static int maxValue( int N, int K,
int []A)
{
int ans = Int32.MinValue;
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 ans;
}
public static void Main ()
{
int N = 6, K = 10;
int []A = { 5, 20, 1, 0, 8, 11 };
Console.WriteLine(maxValue(N, K, A));
}
}
|
Javascript
<script>
function maxValue(N, K, A)
{
let ans = Number.MIN_VALUE;
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 ans;
}
let N = 6, K = 10;
let A
= [5, 20, 1, 0, 8, 11];
document.write(maxValue(N, K, A));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Mar, 2022
Like Article
Save Article