Find two non-intersecting subarrays having equal sum of all elements raised to the power of 2
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 such 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:
- Sort the given array in ascending order.
- Traverse the array and check if any pair of adjacent elements is equal or not.
- If any such pair is found, print “YES”. Otherwise, print “NO”.
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> |
YES
Time Complexity: O(NLogN)
Auxiliary Space: O(1)
Please Login to comment...