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

📅  最后修改于: 2021-09-07 05:09:58             🧑  作者: Mango

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

例子:

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

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

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

下面是上述方法的实现:

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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live