Open In App

Distribute M objects starting from Sth person such that every ith person gets arr[i] objects

Last Updated : 26 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] consisting of N integers (1-based indexing) and two integers M and S, the task is to distribute M objects among N persons, starting from the position S, such that the ith person gets at most arr[i] objects each time.

Examples:

Input: arr[] = {2, 3, 2, 1, 4}, M = 11, S = 2
Output: 1, 3, 2, 1, 4
Explanation: The distribution of M (= 11) objects starting from Sth(= 2) person is as follows:

  • For arr[2](= 3): Give 3 objects to the 2nd person. Now, the total number of objects reduces to (11 – 3) = 8.
  • For arr[3] (= 2): Give 2 objects to the 3rd person. Now, the total number of objects reduces to (8 – 2) = 6.
  • For arr[4] (= 1): Give 1 object to the 4th person. Now, the total number of objects reduces to (6 – 1) = 5.
  • For arr[5] (= 4): Give 4 objects to the 5th person. Now, the total number of objects reduces to (5 – 4) = 1.
  • For arr[1] (= 1): Give 1 object to the 1st person. Now, the total number of objects reduced to (1 – 1) = 0.

Therefore, the distribution of objects is {1, 3, 2, 1, 4}.

Input: arr[] = {2, 3, 2, 1, 4}, M = 3, S = 4
Output: 0 0 0 1 2

Approach: The given problem can be solved by traversing the array from the given starting index S and distribute the maximum objects to each array element. Follow the steps below to solve the given problem:

  • Initialize an auxiliary array, say distribution[] with all elements as 0 to store the distribution of M objects.
  • Initialize two variables, say ptr and rem as S and M respectively, to store the starting index and remaining M objects.
  • Iterate until rem is positive, and perform the following steps:
    • If the value of rem is at least the element at index ptr i.e., arr[ptr], then increment the value of distribution[ptr] by arr[ptr] and decrement the value of rem by arr[ptr].
    • Otherwise, increment the distribution[ptr] by rem and update rem equal to 0.
    • Update ptr equal to (ptr + 1) % N to iterate the given array arr[] in a cyclic manner.
  • After completing the above steps, print the distribution[] as the resultant distribution of objects.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find distribution of
// M objects among all array elements
void distribute(int N, int K,
                int M, int arr[])
{
    // Stores the distribution
    // of M objects
    int distribution[N] = { 0 };
 
    // Stores the indices
    // of distribution
    int ptr = K - 1;
 
    // Stores the remaining objects
    int rem = M;
 
    // Iterate until rem is positive
    while (rem > 0) {
 
        // If the number of remaining
        // objects exceeds required
        // the number of objects
        if (rem >= arr[ptr]) {
 
            // Increase the number of objects
            // for the index ptr by arr[ptr]
            distribution[ptr] += arr[ptr];
 
            // Decrease remaining
            // objects by arr[ptr]
            rem -= arr[ptr];
        }
        else {
 
            // Increase the number of objects
            // for the index ptr by rem
            distribution[ptr] += rem;
 
            // Decrease remaining
            // objects to 0
            rem = 0;
        }
 
        // Increase ptr by 1
        ptr = (ptr + 1) % N;
    }
 
    // Print the final distribution
    for (int i = 0; i < N; i++) {
        cout << distribution[i]
             << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 2, 1, 4 };
    int M = 11, S = 2;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    distribute(N, S, M, arr);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find distribution of
  // M objects among all array elements
  static void distribute(int N, int K, int M, int arr[])
  {
    // Stores the distribution
    // of M objects
    int distribution[] = new int[N];
 
    // Stores the indices
    // of distribution
    int ptr = K - 1;
 
    // Stores the remaining objects
    int rem = M;
 
    // Iterate until rem is positive
    while (rem > 0) {
 
      // If the number of remaining
      // objects exceeds required
      // the number of objects
      if (rem >= arr[ptr]) {
 
        // Increase the number of objects
        // for the index ptr by arr[ptr]
        distribution[ptr] += arr[ptr];
 
        // Decrease remaining
        // objects by arr[ptr]
        rem -= arr[ptr];
      }
      else {
 
        // Increase the number of objects
        // for the index ptr by rem
        distribution[ptr] += rem;
 
        // Decrease remaining
        // objects to 0
        rem = 0;
      }
 
      // Increase ptr by 1
      ptr = (ptr + 1) % N;
    }
 
    // Print the final distribution
    for (int i = 0; i < N; i++) {
      System.out.print(distribution[i] + " ");
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    int arr[] = { 2, 3, 2, 1, 4 };
    int M = 11, S = 2;
    int N = arr.length;
 
    distribute(N, S, M, arr);
  }
}
 
// This code is contributed by Kingash.


Python3




# Python3 program for the above approach
 
# Function to find distribution of
# M objects among all array elements
def distribute(N, K, M, arr):
 
    # Stores the distribution
    # of M objects
    distribution = [0] * N
 
    # Stores the indices
    # of distribution
    ptr = K - 1
 
    # Stores the remaining objects
    rem = M
 
    # Iterate until rem is positive
    while (rem > 0):
 
        # If the number of remaining
        # objects exceeds required
        # the number of objects
        if (rem >= arr[ptr]):
 
            # Increase the number of objects
            # for the index ptr by arr[ptr]
            distribution[ptr] += arr[ptr]
 
            # Decrease remaining
            # objects by arr[ptr]
            rem -= arr[ptr]
         
        else:
 
            # Increase the number of objects
            # for the index ptr by rem
            distribution[ptr] += rem
 
            # Decrease remaining
            # objects to 0
            rem = 0
         
        # Increase ptr by 1
        ptr = (ptr + 1) % N
     
    # Print the final distribution
    for i in range(N):
        print(distribution[i], end = " ")
     
# Driver Code
arr = [ 2, 3, 2, 1, 4 ]
M = 11
S = 2
N = len(arr)
 
distribute(N, S, M, arr)
 
# This code is contributed by sanjoy_62


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find distribution of
// M objects among all array elements
static void distribute(int N, int K,
                       int M, int []arr)
{
     
    // Stores the distribution
    // of M objects
    int []distribution = new int[N];
     
    // Stores the indices
    // of distribution
    int ptr = K - 1;
     
    // Stores the remaining objects
    int rem = M;
     
    // Iterate until rem is positive
    while (rem > 0)
    {
     
        // If the number of remaining
        // objects exceeds required
        // the number of objects
        if (rem >= arr[ptr])
        {
             
            // Increase the number of objects
            // for the index ptr by arr[ptr]
            distribution[ptr] += arr[ptr];
             
            // Decrease remaining
            // objects by arr[ptr]
            rem -= arr[ptr];
        }
        else
        {
         
            // Increase the number of objects
            // for the index ptr by rem
            distribution[ptr] += rem;
             
            // Decrease remaining
            // objects to 0
            rem = 0;
        }
         
        // Increase ptr by 1
        ptr = (ptr + 1) % N;
    }
     
    // Print the final distribution
    for(int i = 0; i < N; i++)
    {
        Console.Write(distribution[i] + " ");
    }
}
 
// Driver Code
public static void Main(string[] args)
{
    int []arr = { 2, 3, 2, 1, 4 };
    int M = 11, S = 2;
    int N = arr.Length;
     
    distribute(N, S, M, arr);
}
}
 
// This code is contributed by AnkThon


Javascript




<script>
        // Javascript program for the above approach
 
        // Function to find distribution of
        // M objects among all array elements
        function distribute( N,  K,
             M,  arr)
        {
            // Stores the distribution
            // of M objects
            let distribution = new Array(N)
             
            for (let i = 0; i < N; i++) {
                distribution[i]=0
            }
            // Stores the indices
            // of distribution
            let ptr = K - 1;
 
            // Stores the remaining objects
            let rem = M;
 
            // Iterate until rem is positive
            while (rem > 0) {
 
                // If the number of remaining
                // objects exceeds required
                // the number of objects
                if (rem >= arr[ptr]) {
 
                    // Increase the number of objects
                    // for the index ptr by arr[ptr]
                    distribution[ptr] += arr[ptr];
 
                    // Decrease remaining
                    // objects by arr[ptr]
                    rem -= arr[ptr];
                }
                else {
 
                    // Increase the number of objects
                    // for the index ptr by rem
                    distribution[ptr] += rem;
 
                    // Decrease remaining
                    // objects to 0
                    rem = 0;
                }
 
                // Increase ptr by 1
                ptr = (ptr + 1) % N;
            }
 
            // Print the final distribution
            for (let i = 0; i < N; i++) {
                document.write(distribution[i]+" ")
            }
        }
 
        // Driver Code
      
        let arr = [ 2, 3, 2, 1, 4 ];
        let M = 11, S = 2;
        let N = arr.length
        distribute(N, S, M, arr);
 
        // This code is contributed by Hritik
    </script>


Output: 

1 3 2 1 4

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads