
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ code to count number of weight splits of a number n
Suppose we have a number n. We can split n as a nonincreasing sequence of positive integers, whose sum is n. The weight of a split is the number of elements in the split that are equal to the first element. So, the weight of the split [1,1,1,1,1] is 5, the weight of the split [5,5,3,3,3] is 2 and the weight of the split [9] equals 1. We have to find out the number of different weights of n's splits.
So, if the input is like n = 7, then the output will be 4, because possible weights are [7], [3, 3, 1], [2, 2, 2, 1], [1, 1, 1, 1, 1, 1, 1]
Steps
To solve this, we will follow these steps −
return (n / 2 + 1)
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int n){ return (n / 2 + 1); } int main(){ int n = 7; cout << solve(n) << endl; }
Input
7
Output
4
- Related Articles
- C++ code to count number of notebooks to make n origamis
- C++ code to count number of unread chapters
- C++ code to count number of even substrings of numeric string
- C++ code to count number of packs of sheets to be bought
- C++ code to count number of lucky numbers with k digits
- C++ code to count number of times stones can be given
- C++ code to count number of operations to make two arrays same
- C++ code to count number of dice rolls to get target x
- C++ program to count minimum number of operations needed to make number n to 1
- Count total number of digits from 1 to N in C++
- C++ code to find minimum number starting from n in a game
- Count number of ways to divide a number in parts in C++
- Write a program in Python to count the number of digits in a given number N
- Count number of paths whose weight is exactly X and has at-least one edge of weight M in C++
- Program to count the number of ways to distribute n number of candies in k number of bags in Python

Advertisements