Find the largest possible value of S (S = (min2∧min)), where min2 & min is the smallest and the next smallest integer in k elements of the array

There is a programming problem I'm working on. The problem is this:

Given an array A [] of N individual elements. Let min2 and min be the smallest and the next smallest element in the interval [L, R], where 1 ≤ L <R ≤ N.

S = (min2 ∧min).

where ∧ is the bitwise XOR operator. I have to find the maximum possible value of S.

I wrote 1 solution that finds the maximum possible value for S, but its complexity is high. I thought if it could be optimized anyway. Since even the range of k elements is not fixed, I have to calculate for all ranges, for example k = 2, the length of the array. The approach that I use first takes k as 2 and starting from the 0th element, I find min and min2 in the first k elements, computing S if it is larger than the previous S, taking it as S, otherwise ignoring it. After that, starting from the 1st element, I find min in the following elements of k and so on. The same with other higher ranges as well, i.e. 3,4,5 ... Below is the code I wrote:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {

    int num = 0, S = Integer.MIN_VALUE, min = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
    int[] input = null;

    try {
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        String s = bufferRead.readLine();
        num = Integer.parseInt(s);
        s = bufferRead.readLine();
        String arr[] = s.split(" ");

        input = new int[arr.length];
        for(int i =0; i<arr.length;i++) {
            input[i] = Integer.parseInt(arr[i]);
        }
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }



    for(int k=2;k<=input.length;k++) {
         int j =0;

         while(j<input.length-k+1)
             {

             int i=j;

             for(i=j;i<(j+k);i++)
             {
             if(input[i] < min)
                 {
                 min2 = min;
                 min = input[i];
             }
                 else if(input[i] > min && input[i] < min2)
                 {
                 min2 = input[i];

             }


         }

         int st = (((min2 & min)^(min2 | min)) & (min2 ^ min));


         if(st > S)
             S = st;
            j++;
            min = Integer.MAX_VALUE;
            min2 = Integer.MAX_VALUE;
         }

        }
        System.out.println( S );

    }

}

Is there any way to choose this?

+4
source share
1

O (N) .

, , . ( , ).

(A[R]). , (A[L]), , , A[R], . A[L], A[R] - , S . .

-:

result = -infinity
for each X in array A:
    while NOT stack.empty AND stack.top > X: stack.pop
    if NOT stack.empty:
        result = max(result, X XOR stack.top)
    stack.push(X)
reverse array A and repeat

. ( , ) , () . , A[L]..A[R]. , ( ), , A[R], A[R] - , ( S). , A[R] , , A[R] , ( , , ).

+3

Source: https://habr.com/ru/post/1606040/


All Articles