Given a sorted array of integers and a number k, write a function that returns true if given array can be divided into pairs such that sum of every pair k.
Expected time complexity O(n) and extra space O(1). This problem is a variation of below problem, but has a different interesting solution that requires only O(1) space.
Check if an array can be divided into pairs whose sum is divisible by k
Examples:
Input: arr[] = {1, 3, 3, 5}, k = 6 Output: True We can divide array into (1, 5) and (3, 3). Sum of both of these pairs is 6. Input: arr[] = {2, 5, 5, 5, 5, 8}, k = 10 Output: True We can divide array into (2, 8), (5, 5) and (5, 5). Sum of all these pairs is 10.
We strongly recommend you to minimize your browser and try this yourself first.
A Simple Solution is to iterate through every element arr[i]. Find if there is another not yet visited element with value (k – arr[i]). If there is no such element, return false. If a pair is found, then mark both elements as visited. Time complexity of this solution is O(n2 and it requires O(n) extra space.
A Better Solution is to use Hashing. Solution given on this post can be easily modified to work. Time complexity of this solution is O9n) but it requires extra space for hash table.
An Efficient Solution is to use Meet in the Middle algorithm discussed in method 1 here.
1) Initialize two index variables (a) Initialize first to the leftmost index: l = 0 (b) Initialize second the rightmost index: r = n-1 2) Loop while l < r. (a) If (A[l] + A[r] != k) then return false (b) Else r--, l++
Below is the implementation of above algorithm.
C++
// A C++ program to check if arr[0..n-1] can be divided // in pairs such that every pair has sum k. #include <bits/stdc++.h> using namespace std; // Returns true if arr[0..n-1] can be divided into pairs // with sum equal to k. bool canPairsSorted( int arr[], int n, int k) { // An odd length array cannot be divided into pairs if (n & 1) return false ; // Traverse from both sides of array int l = 0, r = n-1; while (l < r) { // If array can be divided, then sum of current // elements must be sum if (arr[l] + arr[r] != k) return false ; // Move index variables l++; r--; } return true ; } /* Driver program to test above function */ int main() { int arr[] = {1, 2, 3, 3, 3, 3, 4, 5}; int n = 6; int n = sizeof (arr)/ sizeof (arr[0]); canPairs(arr, n, k)? cout << "True" : cout << "False" ; return 0; } |
Java
// Java program to check if arr[0..n-1] can be divided // in pairs such that every pair has sum k. class GFG { // Returns true if arr[0..n-1] can be divided into pairs // with sum equal to k. static boolean canPairs( int arr[], int n, int k) { // An odd length array cannot be divided into pairs if (n == 1 ) { return false ; } // Traverse from both sides of array int l = 0 , r = n - 1 ; while (l < r) { // If array can be divided, then sum of current // elements must be sum if (arr[l] + arr[r] != k) { return false ; } // Move index variables l++; r--; } return true ; } // Drivers code public static void main(String[] args) { int arr[] = { 1 , 2 , 3 , 3 , 3 , 3 , 4 , 5 }; int k = 6 ; int n = arr.length; if (canPairs(arr, n, k)) { System.out.println( "True" ); } else { System.out.println( "False" ); } } } //This code contributed by 29AjayKumar |
Python 3
# A Python3 program to check if arr[0..n-1] can be # divided in pairs such that every pair has sum k. # Returns true if arr[0..n-1] can be divided # into pairs with sum equal to k. def canPairsSorted(arr, n, k): # An odd length array cannot # be divided into pairs if (n & 1 ): return False ; # Traverse from both sides of array l = 0 ; r = n - 1 ; while (l < r): # If array can be divided, then # sum of current elements must be sum if (arr[l] + arr[r] ! = k): return False ; # Move index variables l = l + 1 ; r = r - 1 ; return True # Driver Code arr = [ 1 , 2 , 3 , 3 , 3 , 3 , 4 , 5 ] k = 6 n = len (arr) if (canPairsSorted(arr, n, k)): print ( "True" ) else : print ( "False" ); # This code is contributed # by Akanksha Rai |
C#
// C# program to check if arr[0..n-1] // can be divided in pairs such that // every pair has sum k. using System; class GFG { // Returns true if arr[0..n-1] // can be divided into pairs // with sum equal to k. static bool canPairs( int []arr, int n, int k) { // An odd length array cannot // be divided into pairs if (n == 1) { return false ; } // Traverse from both sides of array int l = 0, r = n - 1; while (l < r) { // If array can be divided, // then sum of current // elements must be sum if (arr[l] + arr[r] != k) { return false ; } // Move index variables l++; r--; } return true ; } // Driver code public static void Main() { int []arr = {1, 2, 3, 3, 3, 3, 4, 5}; int k = 6; int n = arr.Length; if (canPairs(arr, n, k)) { Console.Write( "True" ); } else { Console.Write( "False" ); } } } // This code is contributed by PrinciRaj |
PHP
<?php // A PHP program to check if arr[0..n-1] // can be divided in pairs such that // every pair has sum k. // Returns true if arr[0..n-1] can be // divided into pairs with sum equal to k. function canPairs(& $arr , $n , $k ) { // An odd length array cannot be // divided into pairs if ( $n & 1) return false; // Traverse from both sides of array $l = 0; $r = $n - 1; while ( $l < $r ) { // If array can be divided, then sum // of current elements must be sum if ( $arr [ $l ] + $arr [ $r ] != $k ) return false; // Move index variables $l ++; $r --; } return true; } // Driver Code $arr = array (1, 2, 3, 3, 3, 3, 4, 5); $n = 6; $k = 6; $n = sizeof( $arr ); if (canPairs( $arr , $n , $k )) echo ( "True" ); else echo ( "False" ); // This code is contributed // by Shivi_Aggarwal ?> |
Output:
True
Time Complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by Priyanka. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.