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 .
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 - denoting the elements of .
The next line contains space-separated integers - denoting the elements of .
Output format
For each test case, print maximum value of in a separate line.
Constraints
The sum of over all test cases does not exceed .
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:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-
- Swap the adjacent characters of the string
- Double the vowel characters in the string
- Character with their frequency
- Program to find the closest value
Must check this:-
Companies interview:-
- Swap adjacent characters
- Double the vowel characters
- Check valid parenthesis
- Print the characters with their frequencies
- Find closest value
- Word Count
- Program of CaesarCipher
- Program to find the perfect city
- Annual Day | Tech Mahindra coding question
- Find the number of pairs in the array whose sum is equal to a given target.
Full C course:-
Key points:-
- How to set limit in the floating value in python
- What is boolean data type
- How to print any character without using format specifier
- How to check that given number is power of 2 or not
- How to fix limit in double and floating numbers after dot (.) in c++
- How to print a double or floating point number in scientific notation and fixed notation
- How to take input a string in c
- How to reduce the execution time of program in c++.
Cracking the coding interview:-
Array and string:-
Tree and graph:-
Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Hackerrank Problems:-
- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Sum of Digits of a Five Digit Number | hackerrank practice problem solution
- 1D Arrays in C | hackerrank practice problem solution
- Array Reversal | hackerrank practice problem solution
- Printing Tokens | hackerrank practice problem solution
- Digit Frequency | hackerrank practice problem solution
- Calculate the Nth term | hackerrank practice problem solution
Data structure:-
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
MCQs:-
0 Comments