Given an integer N, find the number of ways we can choose 3 numbers from {1, 2, 3 …, N} such that their sum is even.

Examples:

Input : N = 3 Output : 1 Explanation: Select 1, 2 and 3 Input : N = 4 Output : 2 Either select (1, 2, 3) or (1, 3, 4)

# Recommended: Please solve it on “*PRACTICE*” first, before moving on to the solution.

*PRACTICE*

To get sum even there can be only 2 cases:

- Take 2 odd numbers and 1 even.
- Take all even numbers.

If n is even, Count of odd numbers = n/2 and even = n/2. Else Count odd numbers = n/2 +1 and evne = n/2.

Case 1 – No. of ways will be : ^{odd}C_{2} * even.

Case 2 – No. of ways will be : ^{even}C_{3}.

So, total ways will be Case_1_result + Case_2_result.

## C++

`// C++ program for above implementation ` `#include <bits/stdc++.h> ` `#define MOD 1000000007 ` `using` `namespace` `std; ` ` ` `// Function to count number of ways ` `int` `countWays(` `int` `N) ` `{ ` ` ` `long` `long` `int` `count, odd = N / 2, even; ` ` ` `if` `(N & 1) ` ` ` `odd = N / 2 + 1; ` ` ` ` ` `even = N / 2; ` ` ` ` ` `// Case 1: 2 odds and 1 even ` ` ` `count = (((odd * (odd - 1)) / 2) * even) % MOD; ` ` ` ` ` `// Case 2: 3 evens ` ` ` `count = (count + ((even * (even - 1) * ` ` ` `(even - 2)) / 6)) % MOD; ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` `cout << countWays(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java program for above implementation ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `long` `MOD = ` `1000000007` `; ` ` ` ` ` `// Function to count number of ways ` ` ` `static` `long` `countWays(` `int` `N) ` ` ` `{ ` ` ` `long` `count, odd = N / ` `2` `, even; ` ` ` ` ` `if` `((N & ` `1` `) > ` `0` `) ` ` ` `odd = N / ` `2` `+ ` `1` `; ` ` ` ` ` `even = N / ` `2` `; ` ` ` ` ` `// Case 1: 2 odds and 1 even ` ` ` `count = (((odd * (odd - ` `1` `)) / ` `2` `) ` ` ` `* even) % MOD; ` ` ` ` ` `// Case 2: 3 evens ` ` ` `count = (count + ((even * (even ` ` ` `- ` `1` `) * (even - ` `2` `)) / ` `6` `)) ` ` ` `% MOD; ` ` ` ` ` `return` `(` `long` `)count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` ` ` `System.out.println(countWays(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code for above implementation ` ` ` `MOD ` `=` `1000000007` ` ` `# Function to count number of ways ` `def` `countWays( N ): ` ` ` `odd ` `=` `N ` `/` `2` ` ` `if` `N & ` `1` `: ` ` ` `odd ` `=` `N ` `/` `2` `+` `1` ` ` `even ` `=` `N ` `/` `2` ` ` ` ` `# Case 1: 2 odds and 1 even ` ` ` `count ` `=` `(((odd ` `*` `(odd ` `-` `1` `)) ` `/` `2` `) ` `*` `even) ` `%` `MOD ` ` ` ` ` `# Case 2: 3 evens ` ` ` `count ` `=` `(count ` `+` `((even ` `*` `(even ` `-` `1` `) ` `*` ` ` `(even ` `-` `2` `)) ` `/` `6` `)) ` `%` `MOD ` ` ` `return` `count ` ` ` `# Driver code ` `n ` `=` `10` `print` `(` `int` `(countWays(n))) ` ` ` `# This code is contributed by "Sharad_Bhardwaj" ` |

*chevron_right*

*filter_none*

## C#

`// C# program for above implementation ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `static` `long` `MOD = 1000000007; ` ` ` ` ` `// Function to count number of ways ` ` ` `static` `long` `countWays(` `int` `N) ` ` ` `{ ` ` ` `long` `count, odd = N / 2, even; ` ` ` ` ` `if` `((N & 1) > 0) ` ` ` `odd = N / 2 + 1; ` ` ` ` ` `even = N / 2; ` ` ` ` ` `// Case 1: 2 odds and 1 even ` ` ` `count = (((odd * (odd - 1)) / 2) ` ` ` `* even) % MOD; ` ` ` ` ` `// Case 2: 3 evens ` ` ` `count = (count + ((even * (even ` ` ` `- 1) * (even - 2)) / 6)) ` ` ` `% MOD; ` ` ` ` ` `return` `(` `long` `)count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 10; ` ` ` ` ` `Console.WriteLine(countWays(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for ` `// above implementation ` ` ` `$MOD` `= 1000000007; ` ` ` `// Function to count ` `// number of ways ` `function` `countWays(` `$N` `) ` `{ ` ` ` `global` `$MOD` `; ` ` ` ` ` `$count` `; ` ` ` `$odd` `=` `$N` `/ 2; ` ` ` `$even` `; ` ` ` `if` `(` `$N` `& 1) ` ` ` `$odd` `= ` `$N` `/ 2 + 1; ` ` ` ` ` `$even` `= ` `$N` `/ 2; ` ` ` ` ` `// Case 1: 2 odds ` ` ` `// and 1 even ` ` ` `$count` `= (((` `$odd` `* (` `$odd` `- 1)) / 2) * ` ` ` `$even` `) % ` `$MOD` `; ` ` ` ` ` `// Case 2: 3 evens ` ` ` `$count` `= (` `$count` `+ ((` `$even` `* (` `$even` `- 1) * ` ` ` `(` `$even` `- 2)) / 6)) % ` `$MOD` `; ` ` ` ` ` `return` `$count` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 10; ` ` ` `echo` `countWays(` `$n` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

60

This article is contributed by **Sahil Chhabra**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.

## Recommended Posts:

- Number of ways of choosing K equal substrings of any length for every query
- Total ways of choosing X men and Y women from a total of M men and W women
- Count ways to express even number ‘n’ as sum of even integers
- Number of ways to select exactly K even numbers from given Array
- Ways to form a group from three groups with given constraints
- Probability for three randomly chosen numbers to be in AP
- Different ways to sum n using numbers greater than or equal to m
- Count of ways in which N can be represented as sum of Fibonacci numbers without repetition
- Maximum sum from three arrays such that picking elements consecutively from same is not allowed
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Number of triangles formed from a set of points on three lines
- Ways to represent a number as a sum of 1's and 2's
- Number of ways to write N as a sum of K non-negative integers
- Number of ways to sum up a total of N from limited denominations
- Number of permutations such that sum of elements at odd index and even index are equal
- Number of Subsequences with Even and Odd Sum | Set 2
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Ways to sum to N using array elements with repetition allowed
- Count ways to express 'n' as sum of odd integers