- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have an array of numbers and have another number k, we have to check whether given array can be divided into pairs such that the sum of every pair is k or not.

So, if the input is like arr = [1, 2, 3, 4, 5, 6], k = 7, then the output will be True as we can take pairs like (2, 5), (1, 6) and (3, 4).

To solve this, we will follow these steps −

- n := size of arr
- if n is odd, then
- return False

- low := 0, high := n - 1
- while low < high, do
- if arr[low] + arr[high] is not same as k, then
- return False

- low := low + 1
- high := high - 1

- if arr[low] + arr[high] is not same as k, then
- return True

Let us see the following implementation to get better understanding −

def solve(arr, k): n = len(arr) if n % 2 == 1: return False low = 0 high = n - 1 while low < high: if arr[low] + arr[high] != k: return False low = low + 1 high = high - 1 return True arr = [1, 2, 3, 4, 5, 6] k = 7 print(solve(arr, k))

[1, 2, 3, 4, 5, 6], 7

True

- Related Questions & Answers
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- Count pairs in a sorted array whose product is less than k in C++
- Count pairs in array whose sum is divisible by K in C++
- Count pairs in a sorted array whose sum is less than x in C++
- Check if array can be sorted with one swap in Python
- Check if array sum can be made K by three operations on it in Python
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum in Python
- Program to check whether list can be partitioned into pairs where sum is multiple of k in python
- Find if array can be divided into two subarrays of equal sum in C++
- Check if array can be divided into two sub-arrays such that their absolute difference is Ks in Python
- Check if the array can be sorted using swaps between given indices only in Python
- Check if a large number can be divided into two or more segments of equal sum in C++
- Check if an array is sorted and rotated in Python
- Check If a Number Is Majority Element in a Sorted Array in Python
- Count pairs in array whose sum is divisible by 4 in C++

Advertisements