Given a number N, the task is to find the number of pairs containing an even and an odd number from numbers between 1 and N inclusive.
Note: The order of numbers in the pair does not matter. That is (1, 2) and (2, 1) are the same.
Input: N = 3 Output: 2 The pairs are (1, 2) and (2, 3). Input: N = 6 Output: 9 The pairs are (1, 2), (1, 4), (1, 6), (2, 3), (2, 5), (3, 4), (3, 6), (4, 5), (5, 6).
Approach: The number of ways to form the pairs is (Total number of Even numbers*Total number of Odd numbers).
- if N is an even number of even numbers = number of odd numbers = N/2
- if N is an odd number of even numbers = N/2 and the number of odd numbers = N/2+1
Below is the implementation of the above approach:
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes