# 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 ` `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

 ``

Output

`2`

Time Complexity: O(N2)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :