Skip to content
Related Articles

Related Articles

Improve Article

Find two non-intersecting subarrays having equal sum of all elements raised to the power of 2

  • Last Updated : 12 Apr, 2021

Given an array arr[] of positive integers of size N, the task is to check if there exists two non-intersecting subarrays in arr[] such that sum of all possible 2(subarr[i]) and the sum of all possible 2(subarr2[j]) are equal.

Examples:

Input: arr[] = {4, 3, 0, 1, 2, 0}
Output: YES
Explanation: Expressing every array element in the form of 2arr[i], the array is modified to { 16, 8, 1, 2, 4, 1 }.
Therefore, two valid subarrays are { 16 } and { 8, 1, 2, 4, 1 } whose sum are equal.

Input: arr[]={ 3, 4 }
Output: NO

Approach: Since binary representation of all powers of 2 is unique, two sch subarrays can only be obtained if any repeating element is present in that array. Otherwise, it is not possible.



Follow the steps below to solve the problem: 

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
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




<script>
// javascript program for the above approach
 
    // Function to check if two non-intersecting
    // subarrays with equal sum exists or not
    function findSubarrays(arr , N) {
 
        // Sort the given array
        arr.sort();
        var i = 0;
 
        // Traverse the array
        for (i = 0; i < N - 1; i++) {
 
            // Check for duplicate elements
            if (arr[i] == arr[i + 1]) {
                document.write("YES");
                return;
            }
        }
 
        // If no duplicate element is
        // present in the array
        document.write("NO");
    }
 
    // Driver code
     
 
        // Given array
        var arr = [ 4, 3, 0, 1, 2, 0 ];
 
        // Size of array
        var N = arr.length;
 
        findSubarrays(arr, N);
 
// This code is contributed by gauravrajput1
</script>
Output: 
YES

 

Time Complexity: O(NLogN)
Auxiliary Space: O(1)

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :