Cerinta completa
Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where .
.
where , are the bitwise operators , and respectively.
Your task is to find the maximum possible value of .
Input Format
First line contains integer .
Second line contains integers, representing elements of the array .
Constraints
Output Format
Print the value of maximum possible value of .
Sample Input
5
9 6 3 5 2
Sample Output
15
Explanation
Consider the interval the result will be maximum.
Limbajul de programare folosit: cpp14
Cod:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <climits>
#include <stack>
using namespace std;
#define MAXN 1000000
int a[MAXN+1];
void solve(int n) {
stack<int> s;
int result = INT_MIN, cur;
for (int i = 0; i < n; ++ i) {
while (!s.empty() && s.top() >= a[i]) {
cur = INT_MAX;
int tmp = s.top(); s.pop();
if (tmp < cur) {
cur = tmp;
result = max(result, cur ^ a[i]);
}
}
if (!s.empty()) result = max(result, a[i] ^ s.top());
s.push(a[i]);
}
printf("%d\n", result);
}
int main() {
int N;
scanf("%d", &N);
for (int i = 0; i < N; ++ i) scanf("%ld", &a[i]);
solve(N);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Scor obtinut: 1.0
Submission ID: 464653267
Link challenge: https://www.hackerrank.com/challenges/and-xor-or/problem
