In this HackerRank AND xor OR problem solution, we have given an array of distinct elements. and we need to find the smallest and next to smallest element in the bitwise operation of both elements are maximum.
Problem solution in Python programming.
class Stack: def __init__(self): self.items = [] self.size = 0 def push(self, elem): self.items.append(elem) self.size += 1 def pop(self): self.size -= 1 return self.items.pop() def peek(self): return self.items[-1] def isEmpty(self): return self.size == 0 N = int(input()) A = [int(x) for x in input().split(" ")] mx = A[0]^A[1] stack = Stack() for i in A: popped = True while not stack.isEmpty(): top = stack.peek() Si = i^top if Si > mx: # yield mx = Si if i < top: stack.pop() else: break stack.push(i) print(mx)
Problem solution in Java Programming.
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] arr = new int[N]; for(int i = 0; i < N; i++){ arr[i] = sc.nextInt(); } int max = 0; Stack<Integer> stack = new Stack(); for(int i = 0; i < N; i++){ while(!stack.isEmpty()){ int temp = stack.peek() ^ arr[i]; max = Math.max(temp,max); if(arr[i]<stack.peek()) { stack.pop(); }else{ break; } } stack.push(arr[i]); } System.out.println(max); } }
Problem solution in C++ programming.
#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("%dn", 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; }
Problem solution in C programming.
#include <stdio.h> int arr[1000010]; int stack[1000010]; #define max(a, b) ((a) > (b) ? (a) : (b)) int main() { int n, i, top = 0, r = 0; scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%d", arr+i); } for (i = 0; i < n; ++i) { while (top) { r = max(r, arr[i] ^ stack[top-1]); if (arr[i] < stack[top-1]) { --top; } else { break; } } stack[top++] = arr[i]; } printf("%d", r); return 0; }
Problem solution in JavaScript programming.
'use strict'; const fs = require('fs'); process.stdin.resume(); process.stdin.setEncoding('utf-8'); let inputString = ''; let currentLine = 0; process.stdin.on('data', inputStdin => { inputString += inputStdin; }); process.stdin.on('end', _ => { inputString = inputString.trim().split('n').map(str => str.trim()); main(); }); function readLine() { return inputString[currentLine++]; } /* * Complete the andXorOr function below. */ function andXorOr(a) { /* * Write your code here. */ const stack = []; let result = 0; for (let i = 0; i < a.length; i++){ while (stack.length > 0) { const top = stack[stack.length - 1]; const current = a[i] ^ top; result = Math.max(result, current); if (top > a[i]) { stack.pop(); } else { break; } } stack.push(a[i]); } return result; } function main() { const ws = fs.createWriteStream(process.env.OUTPUT_PATH); const aCount = parseInt(readLine(), 10); const a = readLine().split(' ').map(aTemp => parseInt(aTemp, 10)); let result = andXorOr(a); ws.write(result + "n"); ws.end(); }