# Costliest Data Plan | hackerearth solution

Problem

Pawan has a finite number of friends, each friend has a unique non-negative number associated with him. (0, 1, 2..)
Pawan plans to host a party and has to send notifications to his friends.
A friend attends the party if he receives at least 1 notification from Pawan.
Pawan can send notifications using data plan $�$ in the following way:
Let's say Data plan $�$ costs $�$.
Then a friend associated with the number $�$ will receive a notification if and only if the ${�}^{�\mathrm{â„Ž}}$ bit in $�$ is set.
Now Pawan wonders if he can cut costs. Let him know the maximum cost that he can cut by removing at most 1 data plan and still being able to invite all the friends he could invite earlier

Input

Each test contains multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases. Description of the test cases follows.

The first line of each test case contains one integer $�$ -  the number of data plans

The second line of each test case contains $�$ integers a1,a2,…,an — the cost of data plans array.

It is guaranteed that the sum of $�$ over all test cases does not exceed ${10}^{5}$

Output

For each test case, print a line containing a single integer – the maximum possible money that Pawan can save (print 0 if no data plan can be removed)

Constraints

$1<=�<=100$

$1<=�<={10}^{5}$

$0<={�}_{�}<={10}^{9}$

Sample Input
2
2
10 5
4
9 9 9 9

Sample Output
0
9
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In the first case, the data plans cost 10rs and 5rs, With the first data plan(10rs) we can invite 1st and the 3rd friend because in the binary representation of 10  (1010) the first and the third bit are set, and with the second data plan (5rs), we can invite 0th and the 2nd friend because in the binary representation of 5(101), the 0th and the 2nd bit is set

Now with all the data plans, we could invite the 0th, 1st,2nd, and 3rd friends, but if we remove any data plans, there will be a friend who will be left. Therefore the answer is 0rs

In the second case, the cost of all the data plans is the same, so if we remove any one data plan, we will still be able to invite all the friends we invited before. Therefore the answer is 9rs

Code(C):-

#include <stdio.h>
#define ll long long int
ll cmpfunc (const void * a, const void * b) {
return ( *(ll*)a - *(ll*)b );
}
int main() {
ll t;
scanf("%lld", &t);
while(t--){
ll n, i, j;
scanf("%lld", &n);
ll ar[n];
ll bits[100001] = {0};
for(i = 0;i<n;i++){
scanf("%lld", &ar[i]);
}
for(i = 0;i<n;i++){
for(j = 0;j<64;j++){
if((ar[i]&(1<<j))){
bits[j]++;
}
}
}
ll ans = 0;
qsort(ar, n, sizeof(ll), cmpfunc);
ll fl = 0;
for(i = n-1;i>=0;i--){
fl = 0;
for(j = 0;j<64;j++){
if((ar[i]&(1<<j)) && bits[j] == 1){
fl = 1;
break;
}
}
if(fl == 0){
ans = ar[i];
break;
}
}
printf("%lld\n", ans);
}
}

Code(JAVA):-

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.io.Closeable;
import java.util.Arrays;
class TestClass {
static FasterIO fio = new FasterIO();
static int MAXN = (int)1e5;
static int[] arr = new int[MAXN];
static int[] prefixOr = new int[MAXN];
static int[] suffixOr = new int[MAXN];
public static void main(String args[] ) throws Exception {
int t = fio.nextInt();
while(t-- != 0) solve();
fio.close();
}
static void solve() throws Exception {
int n = fio.nextInt(),totOr=0,res=0;
for(int i=0;i<n;i++) {
arr[i] = fio.nextInt();
totOr |= arr[i];
prefixOr[i] = totOr;
}
int sufOr = 0;
for(int i=n-1;i>=0;i--) {
sufOr |= arr[i];
suffixOr[i] = sufOr;
}
if(suffixOr[0 + 1] == totOr) res = arr[0];
for(int i=1;i<n-1;i++)
if((prefixOr[i-1]|suffixOr[i+1])==totOr)
res = max(res,arr[i]);
if(n!=1&&prefixOr[n-1 -1] == totOr) res = max(res,arr[n-1]);
fio.print(res);
fio.pln();
}
static int min(int a, int b) { return a < b ? a : b; }
static int max(int a, int b) { return a > b ? a : b; }
static public class FasterIO implements Closeable {
static final int BUFFER_SIZE = 1<<18;
final byte[] inBuffer = new byte[BUFFER_SIZE];
final byte[] outBuffer = new byte[BUFFER_SIZE];
final byte[] temp = new byte[20];
InputStream in;
OutputStream out;
int inBufPtr, bytesRead, outBufPtr, tempPtr, tInt;
long tLong;
double tDbl, tDiv;
boolean neg;
byte c;
public FasterIO() { in = System.in;out = System.out; }
public FasterIO(String inFile) throws FileNotFoundException {
in = new FileInputStream(inFile);
out = System.out;
}
public int nextInt() throws IOException {
tInt = 0;
while (c <= ' ') c = read();
neg = (c == '-');
do tInt = (tInt << 3) + (tInt << 1) + (c - '0');
while ((c = read()) >= '0' && c <= '9');
return (neg) ? -tInt : tInt;
}
public long nextLong() throws IOException {
tLong = 0;
while (c <= ' ') c = read();
neg = (c == '-');
do tLong = (tLong << 3) + (tLong << 1) + (c - '0');
while ((c = read()) >= '0' && c <= '9');
return (neg)? -tLong: tLong;
}
public double nextDouble() throws IOException {
tDbl = 0;
tDiv = 1;
while (c <= ' ') c = read();
neg = (c == '-');
do tDbl = tDbl * 10 + c - '0'; while ((c = read()) >= '0' && c <= '9');
if (c == '.') while ((c = read()) >= '0' && c <= '9') tDbl += (c - '0') / (tDiv *= 10);
return (neg)? -tDbl: tDbl;
}
public String next() throws IOException {
StringBuilder sb = new StringBuilder();
for (; c > 32; c = read()) sb.append(c);
return sb.toString();
}
public String nextLine() throws IOException {
StringBuilder sb = new StringBuilder();
for (; c != '\n'; c = read()) sb.append(c);
return sb.toString();
}
public char nextChar() throws IOException { while((c=read())<=32); return (char)c; }
private byte read() throws IOException {
? -1: inBuffer[inBufPtr++];
}
public void print(int i) throws IOException {
if (i == 0) { write((byte)'0'); return; }
if (i < 0) { write((byte)'-'); i = -i; }
tempPtr = temp.length;
while (i != 0) { temp[--tempPtr] = (byte) ((i % 10) + '0'); i /= 10; }
write(temp, tempPtr, temp.length-tempPtr);
}
public void print(long i) throws IOException {
if (i == 0) { write((byte)'0'); return; }
if (i < 0) { write((byte)'-'); i = -i; }
tempPtr = temp.length;
while (i != 0) { temp[--tempPtr] = (byte) ((i % 10) + '0'); i /= 10; }
write(temp, tempPtr, temp.length-tempPtr);
}
public void print(double d) throws IOException { print(String.valueOf(d)); }
public void print(char ch) throws IOException { print((byte)ch); };
public void print(String s) throws IOException { write(s.getBytes(), 0, s.length()); }
public void ps() throws IOException { write((byte)' '); }
public void pln() throws IOException { write((byte)'\n');}
private void write(byte b) throws IOException {
if(outBufPtr == BUFFER_SIZE) flushOut();
outBuffer[outBufPtr++] = b;
}
private void write(byte[] bytes, int off, int len) throws IOException {
if(len > BUFFER_SIZE) { flushOut(); out.write(bytes, off, len); }
else {
int l = off, r = off + len, d;
while (l < r) {
d = min(BUFFER_SIZE - outBufPtr, r - l);
System.arraycopy(bytes, l, outBuffer, outBufPtr, d);
l += d; outBufPtr += d;
if (outBufPtr >= BUFFER_SIZE) flushOut();
}
}
}
private void flushOut() throws IOException {
out.write(outBuffer, 0, outBufPtr); outBufPtr = 0;
}
@Override
public void close() throws IOException {
if (in != null) in.close();
if (out != null) { flushOut(); out.close(); }
}
}
}

### Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-