Count ways to express ‘n’ as sum of odd integers
Last Updated :
12 Oct, 2022
Given an positive integer n. Count total number of ways to express ‘n’ as sum of odd positive integers.
Examples:
Input: 4
Output: 3
Explanation
There are only three ways to write 4
as sum of odd integers:
1. 1 + 3
2. 3 + 1
3. 1 + 1 + 1 + 1
Input: 5
Output: 5
Simple approach is to find recursive nature of problem. The number ‘n’ can be written as sum of odd integers from either (n-1)th number or (n-2)th number. Let the total number of ways to write ‘n’ be ways(n). The value of ‘ways(n)’ can be written by recursive formula as follows:
ways(n) = ways(n-1) + ways(n-2)
The above expression is actually the expression for Fibonacci numbers. Therefore problem is reduced to find the nth fibonacci number.
ways(1) = fib(1) = 1
ways(2) = fib(2) = 1
ways(3) = fib(2) = 2
ways(4) = fib(4) = 3
C++
#include<iostream>
using namespace std;
int fib( int n)
{
int f[n+1];
int i;
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
f[i] = f[i-1] + f[i-2];
}
return f[n];
}
int countOddWays( int n)
{
return fib(n);
}
int main()
{
int n = 4;
cout << countOddWays(n) << "\n" ;
n = 5;
cout << countOddWays(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int fib( int n) {
int f[] = new int [n + 1 ];
int i;
f[ 0 ] = 0 ;
f[ 1 ] = 1 ;
for (i = 2 ; i <= n; i++) {
f[i] = f[i - 1 ] + f[i - 2 ];
}
return f[n];
}
static int countOddWays( int n)
{
return fib(n);
}
public static void main(String[] args) {
int n = 4 ;
System.out.print(countOddWays(n) + "\n" );
n = 5 ;
System.out.print(countOddWays(n));
}
}
|
Python3
def fib( n ):
f = list ()
f.append( 0 )
f.append( 1 )
i = 2
while i<n + 1 :
f.append(f[i - 1 ] + f[i - 2 ])
i + = 1
return f[n]
def countOddWays( n ):
return fib(n)
n = 4
print (countOddWays(n))
n = 5
print (countOddWays(n))
|
C#
using System;
class GFG {
static int fib( int n) {
int []f = new int [n + 1];
int i;
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
f[i] = f[i - 1] + f[i - 2];
}
return f[n];
}
static int countOddWays( int n)
{
return fib(n);
}
public static void Main()
{
int n = 4;
Console.WriteLine(countOddWays(n));
n = 5;
Console.WriteLine(countOddWays(n));
}
}
|
PHP
<?php
function fib( $n )
{
$f = array ();
$i ;
$f [0] = 0;
$f [1] = 1;
for ( $i = 2; $i <= $n ; $i ++)
{
$f [ $i ] = $f [ $i - 1] +
$f [ $i - 2];
}
return $f [ $n ];
}
function countOddWays( $n )
{
return fib( $n );
}
$n = 4;
echo countOddWays( $n ) , "\n" ;
$n = 5;
echo countOddWays( $n );
?>
|
Javascript
<script>
function fib(n) {
let f = [];
let i;
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++) {
f[i] = f[i - 1] + f[i - 2];
}
return f[n];
}
function countOddWays(n)
{
return fib(n);
}
let n = 4;
document.write(countOddWays(n) + "<br/>" );
n = 5;
document.write(countOddWays(n));
</script>
|
Output:
3
5
Note: The time complexity of the above implementation is O(n). It can be further optimized up-to O(Logn) time using Fibonacci function optimization by Matrix Exponential.
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...