Skip to content
Related Articles

Related Articles

Distribution of a Number in Array within a Range

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 03 Feb, 2022

Given the integers S, N, K, L, and R where S has to be distributed in an array of size N such that each element must be from the range [L, R] and the sum of K elements of the array should be greater than the sum of the remaining N – K elements whose sum is equal to Sk and these elements are in non-increasing order.

Examples: 

Input: N = 5, K = 3, L = 1, R = 3, S = 13, Sk = 9 
Output: 3 3 3 2 2
 

Input: N = 5, K = 3, L = 1, R = 3, S = 15, Sk = 9 
Output: 3 3 3 3 3

Approach: If Sk can be distributed into k elements equally, then store Sk/k into all the first k elements of the array, otherwise the first element of the array will be (Sk/k) + (Sk % k), and the remaining k – 1 element will be (Sk – Sk % k) % k – 1. Similarly, distribute the S-Sk into n-k elements. 
Below is the implementation of the above approach:

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function for the distribution of the number
void distribution(int n, int k, int l,
                  int r, int S, int Sk)
{
    int a[n];
    int len = k, temp, rem, s;
    int diff = S - Sk;
    for (int i = 0; i < len; i++) {
        // Distribute the number
        // among k elements
        temp = Sk / k;
        rem = Sk % k;
        if (temp + rem >= l && temp + rem <= r) {
            a[i] = temp;
        }
        else if (temp + rem > r) {
            a[i] = r;
        }
        else if (temp + rem < r) {
            cout << "-1";
            return;
        }
        Sk = Sk - a[i];
        k = k - 1;
    }
 
    // If there is some remaining
    // sum to distribute
    if (Sk > 0) {
        cout << "-1";
        return;
    }
 
    // If there are elements remaining
    // to distribute i.e. (n - k)
    if (len) {
        k = n - len;
        for (int i = len; i < n; i++) {
            // Divide the remaining sum into
            // n-k elements
            temp = diff / k;
            rem = diff % k;
            if (temp + rem >= l && temp + rem <= r) {
                a[i] = temp;
            }
            else if (temp + rem > r) {
                a[i] = r;
            }
            else if (temp + rem < r) {
                cout << "-1";
                return;
            }
            diff = diff - a[i];
            k = k - 1;
        }
        if (diff) {
            cout << "-1";
            return;
        }
    }
 
    // Print the distribution
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
}
 
// Driver code
int main()
{
    int n = 5, k = 3, l = 1,
        r = 5, S = 13, Sk = 9;
 
    distribution(n, k, l, r, S, Sk);
 
    return 0;
}

Java




// Java implementation of the approach
class GFG
{
     
    // Function for the distribution of the number
    static void distribution(int n, int k, int l,
                            int r, int S, int Sk)
    {
        int a[] = new int[n];
        int len = k, temp, rem, s;
        int diff = S - Sk;
         
        for (int i = 0; i < len; i++)
        {
             
            // Distribute the number
            // among k elements
            temp = Sk / k;
            rem = Sk % k;
            if (temp + rem >= l && temp + rem <= r)
            {
                a[i] = temp;
            }
            else if (temp + rem > r)
            {
                a[i] = r;
            }
            else if (temp + rem < r)
            {
                System.out.print("-1");
                return;
            }
            Sk = Sk - a[i];
            k = k - 1;
        }
     
        // If there is some remaining
        // sum to distribute
        if (Sk > 0)
        {
            System.out.print("-1");
            return;
        }
     
        // If there are elements remaining
        // to distribute i.e. (n - k)
        if (len != 0)
        {
            k = n - len;
            for (int i = len; i < n; i++)
            {
                 
                // Divide the remaining sum into
                // n-k elements
                temp = diff / k;
                rem = diff % k;
                if (temp + rem >= l && temp + rem <= r)
                {
                    a[i] = temp;
                }
                else if (temp + rem > r)
                {
                    a[i] = r;
                }
                else if (temp + rem < r)
                {
                    System.out.print("-1");
                    return;
                }
                diff = diff - a[i];
                k = k - 1;
            }
            if (diff != 0)
            {
                System.out.print("-1");
                return;
            }
        }
     
        // Print the distribution
        for (int i = 0; i < n; i++)
        {
            System.out.print(a[i] + " ");
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 5, k = 3, l = 1,
            r = 5, S = 13, Sk = 9;
     
        distribution(n, k, l, r, S, Sk);
    }
}
 
// This code is contributed by AnkitRai01

Python3




# Python implementation of the approach
 
# Function for the distribution of the number
def distribution(n, k, l, r, S, Sk):
    a = [0] * n;
    len = k;
    temp, rem, s = 0, 0, 0;
    diff = S - Sk;
 
    for i in range(len):
 
        # Distribute the number
        # among k elements
        temp = Sk / k;
        rem = Sk % k;
        if (temp + rem >= l and temp + rem <= r):
            a[i] = temp;
        elif(temp + rem > r):
            a[i] = r;
        elif(temp + rem < r):
            print("-1");
            return;
         
        Sk = Sk - a[i];
        k = k - 1;
     
    # If there is some remaining
    # sum to distribute
    if (Sk > 0):
        print("-1");
        return;
     
    # If there are elements remaining
    # to distribute i.e. (n - k)
    if (len != 0):
        k = n - len;
        for i in range(len, n):
 
            # Divide the remaining sum into
            # n-k elements
            temp = diff / k;
            rem = diff % k;
            if (temp + rem >= l and temp + rem <= r):
                a[i] = temp;
            elif(temp + rem > r):
                a[i] = r;
            elif(temp + rem < r):
                print("-1");
                return;
             
            diff = diff - a[i];
            k = k - 1;
         
        if (diff != 0):
            print("-1");
            return;
         
    # Print the distribution
    for i in range(n):
        print(int(a[i]), end=" ");
     
# Driver code
if __name__ == '__main__':
    n, k, l, r, S, Sk = 5, 3, 1, 5, 13, 9;
 
    distribution(n, k, l, r, S, Sk);
     
# This code is contributed by PrinciRaj1992

C#




// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function for the distribution of the number
    static void distribution(int n, int k, int l,
                            int r, int S, int Sk)
    {
        int []a = new int[n];
        int len = k, temp, rem;
        int diff = S - Sk;
         
        for (int i = 0; i < len; i++)
        {
             
            // Distribute the number
            // among k elements
            temp = Sk / k;
            rem = Sk % k;
            if (temp + rem >= l && temp + rem <= r)
            {
                a[i] = temp;
            }
            else if (temp + rem > r)
            {
                a[i] = r;
            }
            else if (temp + rem < r)
            {
                Console.Write("-1");
                return;
            }
            Sk = Sk - a[i];
            k = k - 1;
        }
     
        // If there is some remaining
        // sum to distribute
        if (Sk > 0)
        {
            Console.Write("-1");
            return;
        }
     
        // If there are elements remaining
        // to distribute i.e. (n - k)
        if (len != 0)
        {
            k = n - len;
            for (int i = len; i < n; i++)
            {
                 
                // Divide the remaining sum into
                // n-k elements
                temp = diff / k;
                rem = diff % k;
                if (temp + rem >= l && temp + rem <= r)
                {
                    a[i] = temp;
                }
                else if (temp + rem > r)
                {
                    a[i] = r;
                }
                else if (temp + rem < r)
                {
                    Console.Write("-1");
                    return;
                }
                diff = diff - a[i];
                k = k - 1;
            }
            if (diff != 0)
            {
                Console.Write("-1");
                return;
            }
        }
     
        // Print the distribution
        for (int i = 0; i < n; i++)
        {
            Console.Write(a[i] + " ");
        }
    }
     
    // Driver code
    public static void Main(String[] args)
    {
        int n = 5, k = 3, l = 1,
            r = 5, S = 13, Sk = 9;
     
        distribution(n, k, l, r, S, Sk);
    }
}
 
// This code is contributed by PrinciRaj1992

Javascript




<script>
// Javascript implementation of the approach
 
// Function for the distribution of the number
function distribution(n, k, l, r, S, Sk)
{
    let a = new Array(n);
    let len = k, temp, rem, s;
    let diff = S - Sk;
    for (let i = 0; i < len; i++) {
        // Distribute the number
        // among k elements
        temp = Sk / k;
        rem = Sk % k;
        if (temp + rem >= l && temp + rem <= r) {
            a[i] = temp;
        }
        else if (temp + rem > r) {
            a[i] = r;
        }
        else if (temp + rem < r) {
            document.write("-1");
            return;
        }
        Sk = Sk - a[i];
        k = k - 1;
    }
 
    // If there is some remaining
    // sum to distribute
    if (Sk > 0) {
        document.write("-1");
        return;
    }
 
    // If there are elements remaining
    // to distribute i.e. (n - k)
    if (len) {
        k = n - len;
        for (let i = len; i < n; i++) {
            // Divide the remaining sum into
            // n-k elements
            temp = diff / k;
            rem = diff % k;
            if (temp + rem >= l && temp + rem <= r) {
                a[i] = temp;
            }
            else if (temp + rem > r) {
                a[i] = r;
            }
            else if (temp + rem < r) {
                document.write("-1");
                return;
            }
            diff = diff - a[i];
            k = k - 1;
        }
        if (diff) {
            document.write("-1");
            return;
        }
    }
 
    // Print the distribution
    for (let i = 0; i < n; i++) {
        document.write(a[i] + " ");
    }
}
 
// Driver code
 
let n = 5, k = 3, l = 1, r = 5, S = 13, Sk = 9;
 
distribution(n, k, l, r, S, Sk);
</script>

Output: 

3 3 3 2 2

 

Time Complexity: O(n)

Auxiliary Space: O(n)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!