
- 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 distribution of apples for two mice
Suppose we have a number n and two arrays A and B of different sizes. There are two mice: m1 and m2. We put n apples in front of them. We know which apples m1 likes. Similarly, we also know which apples m2 likes. We do not want any conflict between the mice (as they may like the same apple), so we decided to distribute the apples between the mice on our own. We are going to give some apples to m1 and some apples to m2. It doesn't matter how many apples each mouse gets but it is important that each mouse gets only the apples it likes. It is also possible that somebody doesn't get any apples. A[i] holds the apples that m1 likes and B[i] are apples which are liked by m2. We have to print how we distribute them.
Problem Category
An array in the data structure is a finite collection of elements of a specific type. Arrays are used to store elements of the same type in consecutive memory locations. An array is assigned a particular name and it is referenced through that name in various programming languages. To access the elements of an array, indexing is required. We use the terminology 'name[i]' to access a particular element residing in position 'i' in the array 'name'. Various data structures such as stacks, queues, heaps, priority queues can be implemented using arrays. Operations on arrays include insertion, deletion, updating, traversal, searching, and sorting operations. Visit the link below for further reading.
https://www.tutorialspoint.com/data_structures_algorithms/array_data_structure.htm
So, if the input of our problem is like n = 4; A = [1, 2]; B = [2, 3, 4], then the output will be [1, 1, 2, 2]
Steps
To solve this, we will follow these steps −
Define an array v of size: n and fills with 0 for initialize i := 0, when i < size of A, update (increase i by 1), do: j := A[i] increase v[j - 1] by 1 for initialize i := 0, when i < n, update (increase i by 1), do: (if v[i] is same as 1, then print "1, ", otherwise print "2, ")
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(int n, vector<int> A, vector<int> B){ int v[n] = { 0 }; for (int i = 0; i < A.size(); i++){ int j = A[i]; v[j - 1]++; } for (int i = 0; i < n; i++){ (v[i] == 1) ? cout << "1, " : cout << "2, "; } } int main(){ int n = 4; vector<int> A = { 1, 2 }; vector<int> B = { 2, 3, 4 }; solve(n, A, B); }
Input
4, { 1, 2 }, { 2, 3, 4 }
Output
1, 1, 2, 2,
- Related Articles
- Program to find maximum number of eaten apples in Python
- C++ Program to find minimum k for candy distribution
- Java Program for Standard Normal Distribution (SND)
- A basket contains 260 apples . If 15% of the apples were rotten , find the number of apples that were good enough to be sold.
- Find the least number of apples so that heaps of 12,15 and 20 apples can be made with no apples left.
- How to renew distribution certificate for iOS?
- C++ Program to find array of candy distribution to m friends
- 8085 Program to check for two out of five code
- 16% of the apples in a basket go bad. If there are 42 good apples in the basket, find the total number of apples in the basket.
- Why apples are red?
- C++ Program for Common Divisors of Two Numbers?
- Python Program for Common Divisors of Two Numbers
- Java Program for Common Divisors of Two Numbers
- Program for subtracting two matrices.
- How to find confidence interval for binomial distribution in R?
