Problem Statement: Given an Array A of size N and P number of pairs describing groups that can be formed between this two pairs ( pairs are indices…