Maximum possible number in a Sequence of distinct elements with given Average
Last Updated :
21 Feb, 2022
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 = 3
Output: 6
Explanation: 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 = 4
Output: 10
Explanation: The desired sequence would be – {1, 2, 3, 4, 10}.
Input: N = 5, K = 2
Output: -1
Explanation: 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 M and sum of the sequence is S. Then, to maximize M, (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++
#include <bits/stdc++.h>
using namespace std;
int maxNumber( int N, int K)
{
int sum = N * K;
int minSum = N * (N - 1) / 2;
int maxNum = sum - minSum;
if (maxNum < N)
return -1;
return maxNum;
}
int main()
{
int N = 5, K = 4;
int maximum_number = maxNumber(N, K);
cout << maximum_number;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int maxNumber( int N, int K)
{
int sum = N * K;
int minSum = N * (N - 1 ) / 2 ;
int maxNum = sum - minSum;
if (maxNum < N)
return - 1 ;
return maxNum;
}
public static void main(String args[])
{
int N = 5 , K = 4 ;
int maximum_number = maxNumber(N, K);
System.out.println(maximum_number);
}
}
|
Python3
def maxNumber(N, K):
sum = N * K;
minSum = (N * (N - 1 ) / / 2 );
maxNum = sum - minSum;
if (maxNum < N):
return - 1 ;
return maxNum;
N = 5
K = 4 ;
maximum_number = maxNumber(N, K);
print (maximum_number);
|
C#
using System;
public class GFG{
static int maxNumber( int N, int K)
{
int sum = N * K;
int minSum = N * (N - 1) / 2;
int maxNum = sum - minSum;
if (maxNum < N)
return -1;
return maxNum;
}
static public void Main (){
int N = 5, K = 4;
int maximum_number = maxNumber(N, K);
Console.Write(maximum_number);
}
}
|
Javascript
<script>
function maxNumber(N, K)
{
let sum = N * K;
let minSum = Math.floor(N * (N - 1) / 2);
let maxNum = sum - minSum;
if (maxNum < N)
return -1;
return maxNum;
}
let N = 5, K = 4;
let maximum_number = maxNumber(N, K);
document.write(maximum_number);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...