Number of ways to get even sum by choosing three numbers from 1 to N
Last Updated :
18 Sep, 2023
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.
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 even = n/2.
Case 1 – No. of ways will be : oddC2 * even.
Case 2 – No. of ways will be : evenC3.
So, total ways will be Case_1_result + Case_2_result.
C++
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
int countWays( int N)
{
long long int count, odd = N / 2, even;
if (N & 1)
odd = N / 2 + 1;
even = N / 2;
count = (((odd * (odd - 1)) / 2) * even) % MOD;
count = (count + ((even * (even - 1) *
(even - 2)) / 6)) % MOD;
return count;
}
int main()
{
int n = 10;
cout << countWays(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static long MOD = 1000000007 ;
static long countWays( int N)
{
long count, odd = N / 2 , even;
if ((N & 1 ) > 0 )
odd = N / 2 + 1 ;
even = N / 2 ;
count = (((odd * (odd - 1 )) / 2 )
* even) % MOD;
count = (count + ((even * (even
- 1 ) * (even - 2 )) / 6 ))
% MOD;
return ( long )count;
}
static public void main (String[] args)
{
int n = 10 ;
System.out.println(countWays(n));
}
}
|
Python3
MOD = 1000000007
def countWays( N ):
odd = N / 2
if N & 1 :
odd = N / 2 + 1
even = N / 2
count = (((odd * (odd - 1 )) / 2 ) * even) % MOD
count = (count + ((even * (even - 1 ) *
(even - 2 )) / 6 )) % MOD
return count
n = 10
print ( int (countWays(n)))
|
C#
using System;
public class GFG {
static long MOD = 1000000007;
static long countWays( int N)
{
long count, odd = N / 2, even;
if ((N & 1) > 0)
odd = N / 2 + 1;
even = N / 2;
count = (((odd * (odd - 1)) / 2)
* even) % MOD;
count = (count + ((even * (even
- 1) * (even - 2)) / 6))
% MOD;
return ( long )count;
}
static public void Main ()
{
int n = 10;
Console.WriteLine(countWays(n));
}
}
|
PHP
<?php
$MOD = 1000000007;
function countWays( $N )
{
global $MOD ;
$count ;
$odd = $N / 2;
$even ;
if ( $N & 1)
$odd = $N / 2 + 1;
$even = $N / 2;
$count = ((( $odd * ( $odd - 1)) / 2) *
$even ) % $MOD ;
$count = ( $count + (( $even * ( $even - 1) *
( $even - 2)) / 6)) % $MOD ;
return $count ;
}
$n = 10;
echo countWays( $n );
?>
|
Javascript
<script>
let MOD = 1000000007;
function countWays(N)
{
let count, odd = N / 2, even;
if ((N & 1) > 0)
odd = N / 2 + 1;
even = N / 2;
count = (((odd * (odd - 1)) / 2)
* even) % MOD;
count = (count + ((even * (even
- 1) * (even - 2)) / 6))
% MOD;
return count;
}
let n = 10;
document.write(countWays(n));
</script>
|
Output:
60
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...