Skip to content
Related Articles

Related Articles

Generate N sized Array with mean K and minimum difference between min and max

View Discussion
Improve Article
Save Article
  • Last Updated : 11 May, 2022
View Discussion
Improve Article
Save Article

Given two integers N and X, the task is to find an output array arr[] containing distinct integers of length N such that their average is K and the difference between the minimum and the maximum is minimum possible.

 Input: N = 4, X = 8
Output :- 6 7 9 10
Explanation: Clearly the  mean of 6, 7, 9, 10 is 8 and 
The difference between the max and the min is  (10 – 6) = 4.

Input: N = 5, X = 15
Output: 13 14 15 16 17

 

Approach: The problem can be solved based on the following mathematical observation:

  • For the difference between max and min to be the minimum, the gap between the elements in sorted order must be minimum.
  • For that every element when in sorted order should have minimum difference from the average of array.
  • So half of the elements should be in the left of K and half in right and they should have difference = 1 between them
    • If the value of N is odd, then K can be present in the array.
    • If the value of N is even, then K cannot be a part, (because then elements in left of K and in right of K will not be the same). The two middle elements will be K-1 and K+1 and all elements in the left part and right part have adjacent difference = 1.

Follow the steps below to solve this problem based on the above idea:

  • Check the size of output array (N) is even or odd
    • If even, then print all the numbers from (M-N/2  to M+N/2) except M itself.
    • Otherwise, print all the numbers from (M-N/2  to M+N/2) including M.

Below is the implementation of the above approach :

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the output array
// of length N whose mean is equal to X
void findArray(int n, int x)
{
    int p, q, l;
 
    // If array size to be constructed is odd.
    if (n % 2 != 0) {
        l = n / 2;
        p = x - l;
        q = l + x;
        for (int i = p; i <= q; i++)
            cout << i << " ";
    }
 
    // If array size to be constructed is even
    else {
        l = n / 2;
        p = x - l;
        q = x + l;
        for (int i = p; i <= q; i++) {
            if (i != x)
                cout << i << " ";
        }
    }
}
 
// Driver code
int main()
{
    int N = 4, X = 8;
 
    // Function call
    findArray(N, X);
    return 0;
}


Java




// Java code to implement the approach
import java.io.*;
 
class GFG
{
 
  // Function to find the output array
  // of length N whose mean is equal to X
  public static void findArray(int n, int x)
  {
    int p = 0, q = 0, l = 0;
 
    // If array size to be constructed is odd.
    if (n % 2 != 0) {
      l = n / 2;
      p = x - l;
      q = l + x;
      for (int i = p; i <= q; i++)
        System.out.print(i + " ");
    }
 
    // If array size to be constructed is even
    else {
      l = n / 2;
      p = x - l;
      q = x + l;
      for (int i = p; i <= q; i++) {
        if (i != x)
          System.out.print(i + " ");
      }
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 4, X = 8;
 
    // Function call
    findArray(N, X);
  }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Python3 code to implement the approach
 
# Function to find the output array
# of length N whose mean is equal to X
def findArray(n,x):
   
    # If array size to be constructed is odd.
    a=n%2
    if (a is not 0):
        l = int(n / 2)
        p = int(x - l)
        q = int(l + x)
        for i in range(p,q+1):
            print(i,"",end='')
             
    # If array size to be constructed is even
    else:
        l = int(n / 2)
        p = int(x - l)
        q = int(x + l)
        for i in range(p,q+1):
            if (i is not x):
                print(i,"",end='')
# Driver code
N = 4
X = 8
 
# Function call
findArray(N,X)
 
# This code is contributed by ashishsingh13122000.


C#




// C# program to implement
// the above approach
using System;
class GFG
{
 
  // Function to find the output array
  // of length N whose mean is equal to X
  public static void findArray(int n, int x)
  {
    int p = 0, q = 0, l = 0;
 
    // If array size to be constructed is odd.
    if (n % 2 != 0) {
      l = n / 2;
      p = x - l;
      q = l + x;
      for (int i = p; i <= q; i++)
        Console.Write(i + " ");
    }
 
    // If array size to be constructed is even
    else {
      l = n / 2;
      p = x - l;
      q = x + l;
      for (int i = p; i <= q; i++) {
        if (i != x)
          Console.Write(i + " ");
      }
    }
  }
 
// Driver Code
public static void Main()
{
    int N = 4, X = 8;
 
    // Function call
    findArray(N, X);
}
}
 
// This code is contributed by code_hunt.


Javascript




<script>
        // Javascript code to implement the approach
         
        // Function to find the output array
        // of length N whose mean is equal to X
        function findArray(n, x){
            let p;
            let q;
            let l;
         
            // If array size to be constructed is odd.
            if (n % 2 !== 0) {
                l = n / 2;
                p = x - l;
                q = l + x;
                for (let i = p; i <= q; i++)
                    document.write(i+" ");
            }
         
            // If array size to be constructed is even
            else {
                l = n / 2;
                p = x - l;
                q = x + l;
                for (let i = p; i <= q; i++) {
                    if (i !== x)
                        document.write(i+" ");
                }
            }
        }
         
        // Driver code
        let N = 4;
        let X = 8;
         
        // Function call
        findArray(N, X);
         
        // This code is contributed by ashishsingh13122000.
        </script>


Output

6 7 9 10 

Time Complexity: O(N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!