Header Ads Widget

Max and | hackerearth solution

Problem

You are given array  each containing  inetegers. You have to create array  of  inetegers where =& (Here, & denotes the Bitwise AND operation). You can shuffle elements of    however you want. Find maximum value of 1&2&&.

 Input format

  • The first line contains  denoting the number of test cases. The description of  test cases is as follows:
  • For each test case:
    • The first line contains an integer  denotes size of array .
    • The next line contains  space-separated integers 1,2,, - denoting the elements of .
    • The next line contains  space-separated integers 1,2,, - denoting the elements of .

Output format

For each test case, print maximum value of 1&2&& in a separate line.

Constraints

110511050,109

The sum of   over all test cases does not exceed 105.

 

Sample Input
1
2
3 2
2 3
Sample Output
2
Time Limit: 1
Memory Limit: 256

Source Limit: 

Code(C++):-

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
void fastscan(int &number)
{
bool negative = false;
register int c;
number = 0;
c = getchar();
if (c == '-')
{
negative = true;
c = getchar();
}
for (; (c > 47 && c < 58); c = getchar())
number = number * 10 + c - 48;
if (negative)
number *= -1;
}
void solve()
{
int n;
fastscan(n);
int arr_a[100005];
int arr_b[100005];
int ans = 0xFFFFFFFF;
for (size_t i = 0; i < n; i++)
{
fastscan(arr_a[i]);
}
for (size_t i = 0; i < n; i++)
{
fastscan(arr_b[i]);
ans &= arr_a[i] & arr_b[i];
}
cout << ans << endl;
}
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
    #ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
    #endif
int q;
fastscan(q);
while (q--)
{
solve();
}
return 0;
}

Code(JAVA):-

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.stream.LongStream;
class TestClass {
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws IOException {
Reader reader = new Reader();
int testcase = reader.nextInt();
LongStream.range(0, testcase).map(a -> {
try {
return reader.nextLong();
} catch (IOException e) {
throw new RuntimeException(e);
}
}).map(f -> {
try {
return solution(reader, f);
} catch (IOException e) {
throw new RuntimeException(e);
}
}).forEach(System.out::println);
reader.close();
}
public static long solution(Reader reader, long limit) throws IOException {
long result = Long.MAX_VALUE;
for (int i = 0; i < 2 * limit; i++) {
result &= reader.nextLong();
}
return result;
}
}


Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-