📌  相关文章
📜  最大的子集,其中任何两个元素的绝对差为2的幂

📅  最后修改于: 2021-04-27 19:55:39             🧑  作者: Mango

给定不同的元件的阵列ARR [] -10 9≤A I≤10 9。任务是从给定的数组中找到最大的子集,以使子集中任意两个数字之间的绝对差为2的正幂。如果无法进行这样的设置,则打印-1

例子:

方法:让我们证明子集的大小不会> 3 。假设abcd是来自子集的四个元素,并且a
假设abs(a – b)= 2 k,abs(b – c)= 2 l,abs(a – c)= abs(a – b)+ abs(b – c)= 2 k + 2 l = 2 m 。这意味着k = l 。三元组(b,c,d)的条件也必须成立。现在可以很容易地看出,如果abs(a – b)= abs(b – c)= abs(c – d)= 2 k,abs(a – d)= abs(a – b)* 3不是二的幂。因此,子集的大小永远不会大于3。

  • 让我们检查答案是否为3 。在给定数组上迭代子集的中间元素以及2的幂(从1到30)(包括1和30)。令xi为子集的中间元素,令j为两者的当前幂。然后,如果数组中存在元素x i -2 j和x i +2 j ,则答案为3
  • 否则检查答案是否为2 。重复上一步,但是在这里可以得到左点x i -2 jx i +2 j
  • 如果答案既不是2也不是3,则打印-1

下面是上述方法的实现:

C++
// CPP program to find sub-set with
// maximum possible size
#include 
using namespace std;
  
// Function to find sub-set with
// maximum possible size
void PowerOfTwo(vector x, int n)
{
    // Sort the given array
    sort(x.begin(), x.end());
  
    // To store required sub-set
    vector res;
  
    for (int i = 0; i < n; ++i) {
  
        // Iterate for all powers of two
        for (int j = 1; j < 31; ++j) {
  
            // Left number
            int lx = x[i] - (1 << j);
  
            // Right number
            int rx = x[i] + (1 << j);
  
            // Predefined binary search in c++
            bool isl = binary_search(x.begin(), x.end(), lx);
            bool isr = binary_search(x.begin(), x.end(), rx);
  
            // If possible to get sub-set of size 3
            if (isl && isr && int(res.size()) < 3)
                res = { lx, x[i], rx };
  
            // If possible to get sub-set of size 2
            if (isl && int(res.size()) < 2)
                res = { lx, x[i] };
  
            // If possible to get sub-set of size 2
            if (isr && int(res.size()) < 2)
                res = { x[i], rx };
        }
    }
  
    // If not possible to get sub-set
    if (!res.size()) {
        cout << -1;
        return;
    }
  
    // Print the sub-set
    for (auto it : res)
        cout << it << " ";
}
  
// Driver Code
int main()
{
  
    vector a = { 3, 4, 5, 6, 7 };
  
    int n = a.size();
    PowerOfTwo(a, n);
  
    return 0;
}


Java
// Java program to find sub-set with
// maximum possible size
import java.util.*;
  
class GFG
{
      
// Function to find sub-set with
// maximum possible size
static void PowerOfTwo(int []x, int n)
{
    // Sort the given array
    Arrays.sort(x);
  
    // To store required sub-set
    ArrayList res = new ArrayList();
  
    for (int i = 0; i < n; ++i) 
    {
  
        // Iterate for all powers of two
        for (int j = 1; j < 31; ++j) 
        {
  
            // Left number
            int lx = x[i] - (1 << j);
  
            // Right number
            int rx = x[i] + (1 << j);
  
            // Predefined binary search in Java
            boolean isl = Arrays.binarySearch(x,lx) <
                                    0 ? false : true;
            boolean isr = Arrays.binarySearch(x,rx) < 
                                    0 ? false : true;
  
            // If possible to get sub-set of size 3
            if (isl && isr && res.size() < 3)
            {
                res.clear();
                res.add(lx);
                res.add(x[i]);
                res.add(rx);
            }
  
            // If possible to get sub-set of size 2
            if (isl && res.size() < 2)
            {
                res.clear();
                res.add(lx);
                res.add(x[i]);
            }
            // If possible to get sub-set of size 2
            if (isr && res.size() < 2)
            {
                res.clear();
                res.add(x[i]);
                res.add(rx);
            }
        }
    }
  
    // If not possible to get sub-set
    if (res.size() == 0)
    {
        System.out.println("-1");
        return;
    }
  
    // Print the sub-set
    for (int i = 0; i < res.size(); i++)
        System.out.print(res.get(i) + " ");
}
  
// Driver Code
public static void main (String[] args) 
{
    int[] a = {3, 4, 5, 6, 7};
    int n = a.length;
    PowerOfTwo(a, n);
}
}
  
// This code is Contributed by chandan_jnu


Python3
# Python3 program to find sub-set with 
# maximum possible size 
  
# Function to find sub-set with 
# maximum possible size 
def PowerOfTwo(x, n) : 
      
    # Sort the given array 
    x.sort()
  
    # To store required sub-set 
    res = [] 
  
    for i in range(n) : 
  
        # Iterate for all powers of two 
        for j in range(1, 31) :
              
            # Left number 
            lx = x[i] - (1 << j)
  
            # Right number 
            rx = x[i] + (1 << j) 
  
            if lx in x :
                isl = True
            else :
                isl = False
              
            if rx in x :
                isr = True
            else :
                isr = False
              
            # If possible to get sub-set of size 3 
            if (isl and isr and len(res) < 3) :
                res = [ lx, x[i], rx ] 
  
            # If possible to get sub-set of size 2 
            if (isl and len(res) < 2) :
                res = [ lx, x[i] ] 
  
            # If possible to get sub-set of size 2 
            if (isr and len(res) < 2) :
                res = [ x[i], rx ]
  
    # If not possible to get sub-set 
    if (not len(res)) :
        print(-1) 
        return
      
    # Print the sub-set 
    for it in res :
        print(it, end = " ") 
  
# Driver Code 
if __name__ == "__main__" :
  
    a = [ 3, 4, 5, 6, 7 ] 
  
    n = len(a)
    PowerOfTwo(a, n) 
  
# This code is contributed by Ryuga


C#
// C# program to find sub-set with
// maximum possible size
using System;
using System.Collections;
  
class GFG
{
      
// Function to find sub-set with
// maximum possible size
static void PowerOfTwo(int[] x, int n)
{
    // Sort the given array
    Array.Sort(x);
  
    // To store required sub-set
    ArrayList res = new ArrayList();
  
    for (int i = 0; i < n; ++i) 
    {
  
        // Iterate for all powers of two
        for (int j = 1; j < 31; ++j) 
        {
  
            // Left number
            int lx = x[i] - (1 << j);
  
            // Right number
            int rx = x[i] + (1 << j);
  
            // Predefined binary search in C#
            bool isl = Array.IndexOf(x, lx) < 0? false : true;
            bool isr = Array.IndexOf(x, rx) < 0? false : true;
  
            // If possible to get sub-set of size 3
            if (isl && isr && res.Count < 3)
            {
                res.Clear();
                res.Add(lx);
                res.Add(x[i]);
                res.Add(rx);
            }
  
            // If possible to get sub-set of size 2
            if (isl && res.Count < 2)
            {
                res.Clear();
                res.Add(lx);
                res.Add(x[i]);
            }
            // If possible to get sub-set of size 2
            if (isr && res.Count < 2)
            {
                res.Clear();
                res.Add(x[i]);
                res.Add(rx);
            }
        }
    }
  
    // If not possible to get sub-set
    if (res.Count == 0)
    {
        Console.Write("-1");
        return;
    }
  
    // Print the sub-set
    for (int i = 0; i < res.Count; i++)
        Console.Write(res[i] + " ");
}
  
// Driver Code
public static void Main() 
{
    int[] a = {3, 4, 5, 6, 7};
    int n = a.Length;
    PowerOfTwo(a, n);
}
}
  
// This code is Contributed by chandan_jnu


PHP


输出:
3 5 7

时间复杂度:O(N * logN)
辅助空间:O(1)