📌  相关文章
📜  所有子数组的第一个和第二个最大值的 XOR 的最大值

📅  最后修改于: 2021-09-07 05:12:07             🧑  作者: Mango

给定一个由不同元素组成的数组arr[] ,任务是找到每个可能子数组的第一个和第二个最大元素的 XOR 值的最大值。
注意:数组的长度大于 1。
例子:

朴素方法:生成所有可能的长度大于 1 的子数组,并为每个可能的子数组找到子数组的第一个和第二个最大元素的 XOR 值,并从中找出最大值。
有效的方法:对于这个问题,维护一个堆栈并遵循给定的步骤——

  • 从左到右遍历给定的数组,然后对于每个元素 arr[i] –
    1. 如果栈顶小于 arr[i],则从栈中弹出元素,直到栈顶小于 arr[i]。
    2. 将 arr[i] 压入堆栈。
    3. 查找堆栈顶部两个元素的 XOR 值,如果当前 XOR 值大于找到的最大值,则更新最大值。

下面是上述方法的实现:

C++
// C++ implementation of the above approach.
 
#include 
 
using namespace std;
 
// Function to find the maximum XOR value
int findMaxXOR(vector arr, int n){
     
    vector stack;
    int res = 0, l = 0, i;
 
    // Traversing given array
    for (i = 0; i < n; i++) {
 
        // If there are elements in stack
        // and top of stack is less than
        // current element then pop the elements
        while (!stack.empty() &&
                stack.back() < arr[i]) {
            stack.pop_back();
            l--;
        }
 
        // Push current element
        stack.push_back(arr[i]);
         
        // Increasing length of stack
        l++;
        if (l > 1) {
            // Updating the maximum result
            res = max(res,
             stack[l - 1] ^ stack[l - 2]);
        }
    }
 
 
    return res;
}
 
// Driver Code
int main()
{
    // Initializing array
    vector arr{ 9, 8, 3, 5, 7 };
    int result1 = findMaxXOR(arr, 5);
     
    // Reversing the array(vector)
    reverse(arr.begin(), arr.end());
     
    int result2 = findMaxXOR(arr, 5);
     
    cout << max(result1, result2);
     
    return 0;
}


Java
// Java implementation of the above approach.
import java.util.*;
 
class GFG{
 
// Function to find the maximum XOR value
static int findMaxXOR(Vector arr, int n){
     
    Vector stack = new Vector();
    int res = 0, l = 0, i;
 
    // Traversing given array
    for (i = 0; i < n; i++) {
 
        // If there are elements in stack
        // and top of stack is less than
        // current element then pop the elements
        while (!stack.isEmpty() &&
                stack.get(stack.size()-1) < arr.get(i)) {
            stack.remove(stack.size()-1);
            l--;
        }
 
        // Push current element
        stack.add(arr.get(i));
         
        // Increasing length of stack
        l++;
        if (l > 1) {
             
            // Updating the maximum result
            res = Math.max(res,
            stack.get(l - 1) ^ stack.get(l - 2));
        }
    }
 
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    // Initializing array
    Integer []temp = { 9, 8, 3, 5, 7 };
    Vector arr = new Vector<>(Arrays.asList(temp));
    int result1 = findMaxXOR(arr, 5);
     
    // Reversing the array(vector)
    Collections.reverse(arr);
     
    int result2 = findMaxXOR(arr, 5);
     
    System.out.print(Math.max(result1, result2));
}
}
 
// This code is contributed by sapnasingh4991


Python 3
# Python implementation of the approach
 
from collections import deque
 
 
def maxXOR(arr):
    # Declaring stack
    stack = deque()
     
    # Initializing the length of stack
    l = 0
     
    # Initializing res1 for array
    # traversal of left to right
    res1 = 0
     
    # Traversing the array
    for i in arr:
         
        # If there are elements in stack
        # And top of stack is less than
        # current element then pop the stack
        while stack and stack[-1]1:
            res1 = max(res1, stack[-1]^stack[-2])
     
     
    # Similar to the above method,
    # we calculate the xor for reversed array
    res2 = 0
     
    # Clear the whole stack
    stack.clear()
    l = 0
     
    # Reversing the array
    arr.reverse()
    for i in arr:
        while stack and stack[-1]1:
            res2 = max(res2, stack[-1]^stack[-2])
             
    # Printing the maximum of res1, res2
    return max(res1, res2)
 
# Driver Code
if __name__ == "__main__":
    # Initializing the array
    arr = [9, 8, 3, 5, 7]
    print(maxXOR(arr))


C#
// C# implementation of the above approach.
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to find the maximum XOR value
static int findMaxXOR(List arr, int n){
      
    List stack = new List();
    int res = 0, l = 0, i;
  
    // Traversing given array
    for (i = 0; i < n; i++) {
  
        // If there are elements in stack
        // and top of stack is less than
        // current element then pop the elements
        while (stack.Count!=0 &&
                stack[stack.Count-1] < arr[i]) {
            stack.RemoveAt(stack.Count-1);
            l--;
        }
  
        // Push current element
        stack.Add(arr[i]);
          
        // Increasing length of stack
        l++;
        if (l > 1) {
              
            // Updating the maximum result
            res = Math.Max(res,
            stack[l - 1] ^ stack[l - 2]);
        }
    }
  
    return res;
}
  
// Driver Code
public static void Main(String[] args)
{
    // Initializing array
    int []temp = { 9, 8, 3, 5, 7 };
    List arr = new List(temp);
    int result1 = findMaxXOR(arr, 5);
      
    // Reversing the array(vector)
    arr.Reverse();
      
    int result2 = findMaxXOR(arr, 5);
      
    Console.Write(Math.Max(result1, result2));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
15

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live