# Maximum possible number in a Sequence of distinct elements with given Average

Given integers **N** and **K**, the task is to find the maximum possible number an **N** length sequence of distinct positive integers can have if the average of the elements of the sequence is **K**.

**Examples:**

Input: N = 4, K = 3Output: 6Explanation: The desired sequence would be – {1, 2, 3, 6}.

There could be other sequences as well satisfying the given values of N and K, such as {1, 2, 4, 5}.

But , since the maximum possible number in the sequence is needed, 6 would be the answer.

Input: N = 5, K = 4Output: 10Explanation: The desired sequence would be – {1, 2, 3, 4, 10}.

Input:N = 5, K = 2Output:-1Explanation:Forming a sequence having 5 distinct positive integer and average as 2 is not possible.

**Approach:** The solution to the problem is based on the following observation:

- From the number of integers in the sequence and the average of the sequence, the total sum of all numbers of the sequence can be easily calculated by using the following formula:
sum of all numbers in sequence = (number of terms) x (average of all terms)- Now , suppose the maximum term (or number) is
Mand sum of the sequence isS.Then, to maximizeM,(S-M)must be minimized.- Since , the terms must be distinct and positive, so the series with minimum possible sum would be
1, 2, 3 . . . (N-1).

Sum of such a sequence of natural numbers can be easily calculated for(N-1)terms , and would be:(N * (N – 1)) / 2.- So , the maximum possible integer would be:
M = sum of the given sequence – sum of first (N-1) natural numbers.

Follow the steps mentioned below:

- Calculate the sum of the given sequence.
- Find the sum of first
**(N-1)**natural numbers. - Calculate the maximum possible number from the above observation.
- If the maximum value is less than N then no such sequence is possible.
- Otherwise, the maximum value is the required answer.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to calculate the` `// maximum possible number in a sequence` `// with given average and number of terms.` `int` `maxNumber(` `int` `N, ` `int` `K)` `{` ` ` `// Sum of the sequence` ` ` `int` `sum = N * K;` ` ` `// Minimum possible sum of a sequence` ` ` `// having N-1 distinct positive integers` ` ` `int` `minSum = N * (N - 1) / 2;` ` ` `// Maximum number of the given sequence` ` ` `int` `maxNum = sum - minSum;` ` ` `// If such sequence is not possible` ` ` `if` `(maxNum < N)` ` ` `return` `-1;` ` ` ` ` `return` `maxNum;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 5, K = 4;` ` ` `int` `maximum_number = maxNumber(N, K);` ` ` `cout << maximum_number;` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `public` `class` `GFG` `{` ` ` `// Function to calculate the` `// maximum possible number in a sequence` `// with given average and number of terms.` `static` `int` `maxNumber(` `int` `N, ` `int` `K)` `{` ` ` ` ` `// Sum of the sequence` ` ` `int` `sum = N * K;` ` ` `// Minimum possible sum of a sequence` ` ` `// having N-1 distinct positive integers` ` ` `int` `minSum = N * (N - ` `1` `) / ` `2` `;` ` ` `// Maximum number of the given sequence` ` ` `int` `maxNum = sum - minSum;` ` ` `// If such sequence is not possible` ` ` `if` `(maxNum < N)` ` ` `return` `-` `1` `;` ` ` ` ` `return` `maxNum;` `}` `// Driver Code` `public` `static` `void` `main(String args[])` `{` ` ` `int` `N = ` `5` `, K = ` `4` `;` ` ` `int` `maximum_number = maxNumber(N, K);` ` ` `System.out.println(maximum_number);` `}` `}` `// This code is contributed by Samim Hossain Mondal.` |

## Python3

`# Python code for the above approach` `# Function to calculate the` `# maximum possible number in a sequence` `# with given average and number of terms.` `def` `maxNumber(N, K):` ` ` `# Sum of the sequence` ` ` `sum` `=` `N ` `*` `K;` ` ` `# Minimum possible sum of a sequence` ` ` `# having N-1 distinct positive integers` ` ` `minSum ` `=` `(N ` `*` `(N ` `-` `1` `) ` `/` `/` `2` `);` ` ` `# Maximum number of the given sequence` ` ` `maxNum ` `=` `sum` `-` `minSum;` ` ` `# If such sequence is not possible` ` ` `if` `(maxNum < N):` ` ` `return` `-` `1` `;` ` ` `return` `maxNum;` `# Driver Code` `N ` `=` `5` `K ` `=` `4` `;` `maximum_number ` `=` `maxNumber(N, K);` `print` `(maximum_number);` `# This code is contributed by gfgking` |

## C#

`using` `System;` `public` `class` `GFG{` ` ` `// Function to calculate the` ` ` `// maximum possible number in a sequence` ` ` `// with given average and number of terms.` ` ` `static` `int` `maxNumber(` `int` `N, ` `int` `K)` ` ` `{` ` ` `// Sum of the sequence` ` ` `int` `sum = N * K;` ` ` `// Minimum possible sum of a sequence` ` ` `// having N-1 distinct positive integers` ` ` `int` `minSum = N * (N - 1) / 2;` ` ` `// Maximum number of the given sequence` ` ` `int` `maxNum = sum - minSum;` ` ` `// If such sequence is not possible` ` ` `if` `(maxNum < N)` ` ` `return` `-1;` ` ` `return` `maxNum;` ` ` `}` ` ` `// Driver Code` ` ` `static` `public` `void` `Main (){` ` ` `int` `N = 5, K = 4;` ` ` `int` `maximum_number = maxNumber(N, K);` ` ` `Console.Write(maximum_number);` ` ` `}` `}` `// This code is contributed by hrithikgarg03188` |

## Javascript

`<script>` ` ` `// JavaScript code for the above approach` ` ` `// Function to calculate the` ` ` `// maximum possible number in a sequence` ` ` `// with given average and number of terms.` ` ` `function` `maxNumber(N, K)` ` ` `{` ` ` ` ` `// Sum of the sequence` ` ` `let sum = N * K;` ` ` `// Minimum possible sum of a sequence` ` ` `// having N-1 distinct positive integers` ` ` `let minSum = Math.floor(N * (N - 1) / 2);` ` ` `// Maximum number of the given sequence` ` ` `let maxNum = sum - minSum;` ` ` `// If such sequence is not possible` ` ` `if` `(maxNum < N)` ` ` `return` `-1;` ` ` `return` `maxNum;` ` ` `}` ` ` `// Driver Code` ` ` `let N = 5, K = 4;` ` ` `let maximum_number = maxNumber(N, K);` ` ` `document.write(maximum_number);` ` ` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output**

10

* Time Complexity:* O(1)

*O(1)*

**Auxiliary Space:**