Open In App

Number of ways to choose a pair containing an even and an odd number from 1 to N

Last Updated : 27 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Examples

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).
Thus  

  1. if N is an even number of even numbers = number of odd numbers = N/2
  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: 

C++




// C++ implementation of the above approach
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    int N = 6;
 
    int Even = N / 2;
 
    int Odd = N - Even;
 
    cout << Even * Odd;
 
    return 0;
    // This code is contributed
    // by ANKITRAI1
}


Java




// Java implementation of the above approach
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
 
// Driver code
public static void main(String args[])
{
  int N = 6;
  
  int Even = N / 2 ;
  
  int Odd = N - Even ;
    
  System.out.println( Even * Odd );
    
}
}


Python3




# Python implementation of the above approach
N = 6
 
 # number of even numbers
Even = N//2
 
# number of odd numbers
Odd = N-Even
print(Even * Odd)


C#




// C# implementation of the
// above approach
using System;
 
class GFG
{
 
// Driver code
public static void Main()
{
    int N = 6;
     
    int Even = N / 2 ;
     
    int Odd = N - Even ;
         
    Console.WriteLine(Even * Odd);
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP




<?php
// PHP implementation of the
// above approach
 
// Driver code
$N = 6;
 
$Even = $N / 2 ;
 
$Odd = $N - $Even ;
     
echo $Even * $Odd ;
     
// This code is contributed
// by ChitraNayal
?>


Javascript




<script>
// Javascript implementation of the above approach   
     
    // Driver code
    let N = 6;
    
      let Even = Math.floor(N / 2) ;
    
      let Odd = N - Even ;
      
     document.write( Even * Odd );
     
 
// This code is contributed by avanitrachhadiya2155
</script>


Output: 

9

 

Time Complexity: O(1)

Space Complexity: O(1)



Similar Reads

Count of ways to choose N people containing at least 4 boys and 1 girl from P boys and Q girls
Given integers N, P, and Q the task is to find the number of ways to form a group of N people having at least 4 boys and 1 girls from P boys and Q girls. Examples: Input: P = 5, Q = 2, N = 5Output: 10Explanation: Suppose given pool is {m1, m2, m3, m4, m5} and {w1, w2}. Then possible combinations are: m1 m2 m3 m4 w1m2 m3 m4 m5 w1m1 m3 m4 m5 w1m1 m2
6 min read
Count of Ways to Choose N People Containing at Least 4 Boys and 1 Girl from P Boys and Q Girls | Set 2
Given integers N, P, and Q the task is to find the number of ways to form a group of N people having at least 4 boys and 1 girl from P boys and Q girls. Examples: Input: P = 5, Q = 2, N = 5Output: 10Explanation: Suppose given pool is {m1, m2, m3, m4, m5} and {w1, w2}. Then possible combinations are:m1 m2 m3 m4 w1m2 m3 m4 m5 w1m1 m3 m4 m5 w1m1 m2 m4
7 min read
Count of integers in a range which have even number of odd digits and odd number of even digits
Given a range [L, R], the task is to count the numbers which have even number of odd digits and odd number of even digits. For example, 8 has 1 even digit and 0 odd digit - Satisfies the condition since 1 is odd and 0 is even.545 has 1 even digit and 2 odd digits - Satisfies the condition since 1 is odd and 2 is even.4834 has 3 even digits and 1 od
11 min read
Check if a number has an odd count of odd divisors and even count of even divisors
Given an integer N, the task is to check if N has an odd number of odd divisors and even number of even divisors. Examples: Input: N = 36Output: YesExplanation:Divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36Count of Odd Divisors(1, 3, 9) = 3 [Odd]Count of Even Divisors(2, 4, 6, 12, 18, 36) = 6 [Even] Input: N = 28Output: No Naive Approach: The idea i
9 min read
Find the nearest odd and even perfect squares of odd and even array elements respectively
Given an array arr[ ] of size N, the task for each array element is to print the nearest perfect square having same parity. Examples: Input: arr[ ] = {6, 3, 2, 15}Output: 4 1 4 9Explanation:The nearest even perfect square of arr[0] (= 6) is 4.The nearest odd perfect square of arr[1] (= 3) is 1.The nearest even perfect square of arr[2] (= 2) is 4The
5 min read
Count numbers from given range having odd digits at odd places and even digits at even places
Given two integers L and R, the task is to count numbers from the range [L, R] having odd digits at odd positions and even digits at even positions respectively. Examples: Input: L = 3, R = 25Output: 9Explanation: The numbers satisfying the conditions are 3, 5, 7, 9, 10, 12, 14, 16 and 18. Input: L = 128, R = 162Output: 7Explanation: The numbers sa
29 min read
Modify Binary Tree by replacing all nodes at even and odd levels by their nearest even or odd perfect squares respectively
Given a Binary Tree consisting of N nodes, the task is to replace all the nodes that are present at even-numbered levels in a Binary Tree with their nearest even perfect square and replace nodes at odd-numbered levels with their nearest odd perfect square. Examples: Input: 5 / \ 3 2 / \ 16 19 Output: 9 / \ 4 4 / \ 9 25 Explanation: Level 1: Nearest
13 min read
Even numbers at even index and odd numbers at odd index
Given an array of size n containing equal number of odd and even numbers. The problem is to arrange the numbers in such a way that all the even numbers get the even index and odd numbers get the odd index. Required auxiliary space is O(1).Examples : Input : arr[] = {3, 6, 12, 1, 5, 8} Output : 6 3 12 1 8 5 Input : arr[] = {10, 9, 7, 18, 13, 19, 4,
11 min read
Absolute difference between sum of even elements at even indices & odd elements at odd indices in given Array
Given an array arr[] containing N elements, the task is to find the absolute difference between the sum of even elements at even indices &amp; the count of odd elements at odd indices. Consider 1-based indexing Examples: Input: arr[] = {3, 4, 1, 5}Output: 0Explanation: Sum of even elements at even indices: 4 {4}Sum of odd elements at odd indices: 4
5 min read
Find Kth element in an array containing odd elements first and then even elements
Given the length of an array of integers N and an integer K. The task is to modify the array in such a way that the array contains first all odd integers from 1 to N in ascending order, then all even integers from 1 to N in ascending order and then print the Kth element in the modified array.Examples: Input: N = 8, K = 5 Output: 2 The array will be
8 min read
Article Tags :
Practice Tags :