Menu Close

Function that returns true if given array can be divided into pairs.

Given an array of integers and numbers k and m, write a function that returns true if given array can be divided into pairs such that the sum of every pair gives remainder m when divided by k.
Input: N= 4
arr=2 1 5 7
K=9
M=3
Output: true

def rem(L,N,M,K):
    if len(L)==0:
        return True
    if N>0 and N%2==0:
        for i in range(1,N):
            sum=(L[0]+L[i])%K
            if sum == M:
                L.pop(i)
                L.pop(0)
                return rem(L,N-2,M,K)
        return False
N=int(input('Enter the N number: '))
L=list(map(int,input('Enter an array of integers: ').split(' ')))
K=int(input('Enter the K value: '))
M=int(input('Enter the M value: '))
print(rem(L,N,M,K))

Input_1:
Enter the N number: 4
Enter an array of integers: 2 1 5 7
Enter the K value: 9
Enter the M value: 3
Output:
True


Input_2:
Enter the N number: 10
Enter an array of integers: 1 2 3 4 5 10 6 7 8 9
Enter the K value: 5
Enter the M value: 0
Output:
True


Morae Q!