
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to find array of candy distribution to m friends
Suppose we have two numbers n and m. Amal has n candies and m friends. He wants to make a present with candies to each friend. Amal is planning to present all candies and he wants to do this in the most equal manner. He wants to choose such A[i], where A[i] is the number of candies in the i-th friend's present, the maximum A[i] differs from the least A[i] as little as possible. We have to find the array A.
Problem Category
Various problems in programming can be solved through different techniques. To solve a problem, we have to devise an algorithm first and to do that we have to study the particular problem in detail. A recursive approach can be used if there is a recurring appearance of the same problem over and over again; alternatively, we can use iterative structures also. Control statements such as if-else and switch cases can be used to control the flow of logic in the program. Efficient usage of variables and data structures provides an easier solution and a lightweight, low-memory-requiring program. We have to look at the existing programming techniques, such as Divide-and-conquer, Greedy Programming, Dynamic Programming, and find out if they can. This problem we can solve by some basic logics or brute-force approach. Follow the following contents to understand the approach better.
So, if the input of our problem is like n = 15; m = 4, then the output will be [3, 4, 4, 4]
Steps
To solve this, we will follow these steps −
for initialize j := m, when j > 0, update (decrease j by 1), do: z := n / j n := n - z print z
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(int n, int m){ int z; for (int j = m; j > 0; j--){ z = n / j; n = n - z; cout << z << ", "; } } int main(){ int n = 15; int m = 4; solve(n, m); }
Input
15, 4
Output
3, 4, 4, 4,
- Related Articles
- C++ Program to find minimum k for candy distribution
- Program to count number of unhappy friends in Python
- Program to find number of friend groups in a set of friends connections in Python
- C++ program to distribution of apples for two mice
- Program to find latest group of size M using Python
- Python Program to find the sum of array
- Program to find minimum number of days to make m bouquets using Python
- Python Program to find out the size of the bus that can contain all the friends in the group
- C++ Program to Find Largest Element of an Array
- Program to find array of doubled pairs using Python
- Swift Program to Find Mean of an Unsorted Array
- Swift Program to Find Median of an Unsorted Array
- Golang Program To Find Common Array Elements
- Swift Program to Find Common Array Elements
- Candy in C++
