I am trying to find a more efficient solution to this question: https://practice.geeksforgeeks.org/problems/array-pair-sum-divisibility-problem/0
The problem statement is:
Given an 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 is divisible by k.
My solution(just the main logic):
for i in range(0,n):
for j in range(i+1,n):
if((a[i]+a[j])%k==0 and j not in res):
res[i]=a[i]
res[j]=a[j]
if(len(res)==n):
print("True")
else:
print("False")`
As evident, its a brute force solution that will work in O(n^2). I am basically adding the visited pairs to a list.
But is there a more efficient solution than this? Thanks for your time!
hashing
. geeksforgeeks.org/… – MPops