Distribution of a Number in Array within a Range

Given 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 first element of the array will be (Sk/k) + (Sk % k) and remaining k – 1 elements 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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;
          
    # Prthe 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

3 3 3 2 2



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, princiraj1992

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.