Largest subset where absolute difference of any two element is a power of 2
Given an array arr[] of distinct elements -109 ? ai ? 109. The task is to find the largest sub-set from the given array such that the absolute difference between any two numbers in the sub-set is a positive power of two. If it is not possible to make such sub-set then print -1.
Examples:
Input: arr[] = {3, 4, 5, 6, 7}
Output: 3 5 7
|3 – 5| = 21, |5 – 7| = 21 and |3 – 7| = 22.
Input: arr[] = {2, 5, 8}
Output: -1
Approach: Let’s prove that the size of the sub-set will not be > 3. Suppose a, b, c and d are four elements from a sub-set and a < b < c < d.
Let abs(a – b) = 2k and abs(b – c) = 2l then abs(a – c) = abs(a – b) + abs(b – c) = 2k + 2l = 2m. It means that k = l. Conditions must hold for the triple (b, c, d) too. Now it is easy to see that if abs(a – b) = abs(b – c) = abs(c – d) = 2k then abs(a – d) = abs(a – b) * 3 which is not a power of two. So the size of the sub-set will never be greater than 3.
- Let’s check if the answer is 3. Iterate over the given array for middle elements of the sub-set and for powers of two from 1 to 30 inclusively. Let xi be the middle element of the sub-set and j the current power of two. Then if there are elements xi-2j and xi+2j in the array then the answer is 3.
- Else check if the answer is 2. repeat the previous step but here one can get either left point xi-2j or xi+2j.
- If the answer is neither 2 nor 3 then print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void PowerOfTwo(vector< int > x, int n)
{
sort(x.begin(), x.end());
vector< int > res;
for ( int i = 0; i < n; ++i) {
for ( int j = 1; j < 31; ++j) {
int lx = x[i] - (1 << j);
int rx = x[i] + (1 << j);
bool isl = binary_search(x.begin(), x.end(), lx);
bool isr = binary_search(x.begin(), x.end(), rx);
if (isl && isr && int (res.size()) < 3)
res = { lx, x[i], rx };
if (isl && int (res.size()) < 2)
res = { lx, x[i] };
if (isr && int (res.size()) < 2)
res = { x[i], rx };
}
}
if (!res.size()) {
cout << -1;
return ;
}
for ( auto it : res)
cout << it << " " ;
}
int main()
{
vector< int > a = { 3, 4, 5, 6, 7 };
int n = a.size();
PowerOfTwo(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void PowerOfTwo( int []x, int n)
{
Arrays.sort(x);
ArrayList<Integer> res = new ArrayList<Integer>();
for ( int i = 0 ; i < n; ++i)
{
for ( int j = 1 ; j < 31 ; ++j)
{
int lx = x[i] - ( 1 << j);
int rx = x[i] + ( 1 << j);
boolean isl = Arrays.binarySearch(x,lx) <
0 ? false : true ;
boolean isr = Arrays.binarySearch(x,rx) <
0 ? false : true ;
if (isl && isr && res.size() < 3 )
{
res.clear();
res.add(lx);
res.add(x[i]);
res.add(rx);
}
if (isl && res.size() < 2 )
{
res.clear();
res.add(lx);
res.add(x[i]);
}
if (isr && res.size() < 2 )
{
res.clear();
res.add(x[i]);
res.add(rx);
}
}
}
if (res.size() == 0 )
{
System.out.println( "-1" );
return ;
}
for ( int i = 0 ; i < res.size(); i++)
System.out.print(res.get(i) + " " );
}
public static void main (String[] args)
{
int [] a = { 3 , 4 , 5 , 6 , 7 };
int n = a.length;
PowerOfTwo(a, n);
}
}
|
Python3
def PowerOfTwo(x, n) :
x.sort()
res = []
for i in range (n) :
for j in range ( 1 , 31 ) :
lx = x[i] - ( 1 << j)
rx = x[i] + ( 1 << j)
if lx in x :
isl = True
else :
isl = False
if rx in x :
isr = True
else :
isr = False
if (isl and isr and len (res) < 3 ) :
res = [ lx, x[i], rx ]
if (isl and len (res) < 2 ) :
res = [ lx, x[i] ]
if (isr and len (res) < 2 ) :
res = [ x[i], rx ]
if ( not len (res)) :
print ( - 1 )
return
for it in res :
print (it, end = " " )
if __name__ = = "__main__" :
a = [ 3 , 4 , 5 , 6 , 7 ]
n = len (a)
PowerOfTwo(a, n)
|
C#
using System;
using System.Collections;
class GFG
{
static void PowerOfTwo( int [] x, int n)
{
Array.Sort(x);
ArrayList res = new ArrayList();
for ( int i = 0; i < n; ++i)
{
for ( int j = 1; j < 31; ++j)
{
int lx = x[i] - (1 << j);
int rx = x[i] + (1 << j);
bool isl = Array.IndexOf(x, lx) < 0? false : true ;
bool isr = Array.IndexOf(x, rx) < 0? false : true ;
if (isl && isr && res.Count < 3)
{
res.Clear();
res.Add(lx);
res.Add(x[i]);
res.Add(rx);
}
if (isl && res.Count < 2)
{
res.Clear();
res.Add(lx);
res.Add(x[i]);
}
if (isr && res.Count < 2)
{
res.Clear();
res.Add(x[i]);
res.Add(rx);
}
}
}
if (res.Count == 0)
{
Console.Write( "-1" );
return ;
}
for ( int i = 0; i < res.Count; i++)
Console.Write(res[i] + " " );
}
public static void Main()
{
int [] a = {3, 4, 5, 6, 7};
int n = a.Length;
PowerOfTwo(a, n);
}
}
|
PHP
<?php
function PowerOfTwo( $x , $n )
{
sort( $x );
$res = array ();
for ( $i = 0; $i < $n ; ++ $i )
{
for ( $j = 1; $j < 31; ++ $j )
{
$lx = $x [ $i ] - (1 << $j );
$rx = $x [ $i ] + (1 << $j );
$isl = in_array( $lx , $x );
$isr = in_array( $rx , $x );
if ( $isl && $isr && count ( $res ) < 3)
{
unset( $res );
$res = array ();
array_push ( $res , $lx );
array_push ( $res , $x [ $i ]);
array_push ( $res , $rx );
}
if ( $isl && count ( $res ) < 2)
{
unset( $res );
$res = array ();
array_push ( $res , $lx );
array_push ( $res , $x [ $i ]);
}
if ( $isr && count ( $res ) < 2)
{
unset( $res );
$res = array ();
array_push ( $res , $x [ $i ]);
array_push ( $res , $rx );
}
}
}
if (! count ( $res ))
{
echo "-1" ;
return ;
}
for ( $i = 0; $i < count ( $res ); $i ++)
echo $res [ $i ] . " " ;
}
$a = array ( 3, 4, 5, 6, 7 );
$n = count ( $a );
PowerOfTwo( $a , $n );
?>
|
Javascript
<script>
function PowerOfTwo(x, n)
{
x.sort();
let res = [];
for (let i = 0; i < n; ++i)
{
for (let j = 1; j < 31; ++j)
{
let lx = x[i] - (1 << j);
let rx = x[i] + (1 << j);
let isl = x.indexOf(lx) <
0 ? false : true ;
let isr = x.indexOf(rx) <
0 ? false : true ;
if (isl && isr && res.length < 3)
{
res = [];
res.push(lx);
res.push(x[i]);
res.push(rx);
}
if (isl && res.length < 2)
{
res = [];
res.push(lx);
res.push(x[i]);
}
if (isr && res.length < 2)
{
res = [];
res.push(x[i]);
res.push(rx);
}
}
}
if (res.length == 0)
{
document.write( "-1" + "</br>" );
return ;
}
for (let i = 0; i < res.length; i++)
document.write(res[i] + " " );
}
let a = [3, 4, 5, 6, 7];
let n = a.length;
PowerOfTwo(a, n);
</script>
|
Time Complexity : O(N*logN)
Auxiliary Space : O(1), since no extra space has been taken.
Last Updated :
10 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...