- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given an integer n as input. The goal is to find the number of ways in which we can represent ‘n’ as the sum of odd integers. For example, if n is 3 it can be represented as sum ( 1+1+1 ) and (3) so total 2 ways.

**For Example**

n=6

Count of ways to express ‘n’ as sum of odd integers are: 8

The ways in which we can express ‘n’ as sum of odd integers − 1. 1+1+1+1+1+1 2. 3+1+1+1 3. 1+3+1+1 4. 1+1+3+1 5. 1+1+1+3 6. 3+3 7. 1+5 8. 5+1

n=9

Count of ways to express ‘n’ as sum of odd integers are: 34

The some of the ways in which we can express ‘n’ as sum of odd integers: 1. 1+1+1+1+1+1+1+1+1 2. 3+3+3 3. 5+3+1 4. 7+1+1 5. ….and other such combinations

**Approach used in the below program is as follows** −

In this approach we will check the ways of representing a number as a sum of odd integers from previous numbers that are n−1th and n−2th numbers. Ways will be ways(n−1) + ways(n−2).

Take an integer n as input.

Function odd_ways(int n) takes a number and returns the count of ways to express ‘n’ as sum of odd integers.

Take an array arr of length n+1 to store count ways for representing numbers as sum of odd integers.

For number 0, there is no such way so set arr[0] with 0.

For number 1 there is only one way so set arr[1] with 1.

For the rest of numbers we can set arr[i] with arr[i−1]+arr[i−2] for i between 2 to n.

At the end we have arr[n] for the number of ways in which n is represented as the sum of odd integers.

Return arr[n] as result.

#include<iostream> using namespace std; int odd_ways(int n){ int arr[n+1]; arr[0] = 0; arr[1] = 1; for(int i = 2; i <= n; i++){ arr[i] = arr[i-1] + arr[i-2]; } return arr[n]; } int main(){ int n = 6; cout<<"Count of ways to express ‘n’ as sum of odd integers are: "<<odd_ways(n); return 0; }

If we run the above code it will generate the following output −

Count of ways to express ‘n’ as sum of odd integers are: 8

- Related Questions & Answers
- Count ways to express a number as sum of powers in C++
- Count of different ways to express N as the sum of 1, 3 and 4 in C++
- Count ways to express a number as sum of consecutive numbers in C++
- Express an odd number as sum of prime numbers in C++
- Ways to write N as sum of two or more positive integers in C++
- Count ‘d’ digit positive integers with 0 as a digit in C++
- Minimum number of palindromes required to express N as a sum using C++.
- Minimum numbers needed to express every integer below N as a sum in C++
- Count ways to reach the n’th stair
- How to use ‘as’ operator in C#?
- Python program to replace first ‘K’ elements by ‘N’
- What is the purpose of ‘as’ operator in C#?
- Print string of odd length in ‘X’ format in C Program.
- Count positive integers with 0 as a digit and maximum ‘d' digits in C++
- Squared sum of n odd numbers - JavaScript

Advertisements