📌  相关文章
📜  查找两个不相交的子数组,所有子元素的和之和等于2的幂

📅  最后修改于: 2021-05-18 00:02:53             🧑  作者: Mango

给定大小为N的正整数的数组arr [] ,任务是检查arr []中是否存在两个不相交的子数组,以使所有可能2的总和(subarr [i])和所有可能2的总和(subarr2 [j])相等。

例子:

方法:由于所有2的幂的二进制表示都是唯一的,因此只有在该数组中存在任何重复元素的情况下,才能获得两个sch子数组。否则,这是不可能的。

请按照以下步骤解决问题:

  • 以升序对给定数组进行排序。
  • 遍历数组并检查相邻元素对是否相等。
  • 如果找到任何这样的对,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if two non-intersecting
// subarrays with equal sum exists or not
void findSubarrays(int arr[], int N)
{
    // Sort the given array
    sort(arr, arr + N);
    int i = 0;
 
    // Traverse the array
    for (i = 0; i < N - 1; i++) {
 
        // Check for duplicate elements
        if (arr[i] == arr[i + 1]) {
 
            cout << "YES" << endl;
            return;
        }
    }
 
    // If no duplicate element is
    // present in the array
    cout << "NO" << endl;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 4, 3, 0, 1, 2, 0 };
 
    // Size of array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    findSubarrays(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
      
// Function to check if two non-intersecting
// subarrays with equal sum exists or not
static void findSubarrays(int arr[], int N)
{
     
    // Sort the given array
    Arrays.sort(arr);
    int i = 0;
  
    // Traverse the array
    for(i = 0; i < N - 1; i++)
    {
         
        // Check for duplicate elements
        if (arr[i] == arr[i + 1])
        {
            System.out.println("YES");
            return;
        }
    }
  
    // If no duplicate element is
    // present in the array
    System.out.println("NO");
}
  
// Driver code
public static void main(String[] args)
{
     
    // Given array
    int[] arr = { 4, 3, 0, 1, 2, 0 };
  
    // Size of array
    int N = arr.length;
  
    findSubarrays(arr, N);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python program for the above approach
 
# Function to check if two non-intersecting
# subarrays with equal sum exists or not
def findSubarrays(arr, N):
   
    # Sort the given array
    arr.sort();
    i = 0;
 
    # Traverse the array
    for i in range(N - 1):
 
        # Check for duplicate elements
        if (arr[i] == arr[i + 1]):
            print("YES");
            return;
 
    # If no duplicate element is
    # present in the array
    print("NO");
 
# Driver code
if __name__ == '__main__':
   
    # Given array
    arr = [4, 3, 0, 1, 2, 0];
 
    # Size of array
    N = len(arr);
 
    findSubarrays(arr, N);
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
   
class GFG{
       
// Function to check if two non-intersecting
// subarrays with equal sum exists or not
static void findSubarrays(int[] arr, int N)
{
     
    // Sort the given array
    Array.Sort(arr);
    int i = 0;
   
    // Traverse the array
    for(i = 0; i < N - 1; i++)
    {
         
        // Check for duplicate elements
        if (arr[i] == arr[i + 1])
        {
            Console.WriteLine("YES");
            return;
        }
    }
   
    // If no duplicate element is
    // present in the array
    Console.WriteLine("NO");
}
   
// Driver code
public static void Main()
{
     
    // Given array
    int[] arr = { 4, 3, 0, 1, 2, 0 };
   
    // Size of array
    int N = arr.Length;
   
    findSubarrays(arr, N);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
YES

时间复杂度: O(NLogN)
辅助空间: O(1)