Number of ways in which N can be represented as the sum of two positive integers
Given a number N, the task is to find the number of unique ways in which N can be represented as a sum of two positive integers.
Input: N = 7
(1 + 6), (2 + 5) and (3 + 4).
Input: N = 200
Approach: The number of ways in which the number can be expressed as the sum of two positive integers are 1 + (N – 1), 2 + (N – 2), …, (N – 1) + 1 and (N – 2) + 2. There are N – 1 terms in the series and they appear in identical pairs i.e. (X + Y, Y + X). So the required count will be N / 2.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)