Given **N** items of type-1 and **M** items of type-2. An object can be created from **2 items of type-1** and **1 item of type-2** or **2 items of type-2** and **1 item of type-1** pieces. The task is to find the maximum number of objects that can be created from given number of items of each type.**Examples:**

Input:N = 8, M = 7Output:5Explanation:

3 pairs of 2 type-1 and 1 type-2 objects.

2 pairs of 1 type-1 and 2 type-2 objects.Input:N = 20, M = 3Output:3Explanation:

3 pairs of 2 type-1 and 1 type-2 objects.

**Approach:**

Follow the steps below to solve the problem:

- Create two variables
**initial**and**final**. - initial = Minimum of N, M.
- final = Divide N + M by 3 ( as an object is made up of 3 components).
- Minimum of initial and final is maximum number of objects that can be created from given N type-1 and M type-2 items.

Below is the implementation of the above approach:

## C++

`// C++ program for the above problem` `#include <bits/stdc++.h> ` `using` `namespace` `std;` `// Function for finding` `// the maximum number of` `// objects from N type-1 and ` `// M type-2 items` `int` `numberOfObjects(` `int` `N, ` `int` `M)` `{` ` ` `// storing minimum of N and M` ` ` `int` `initial = min(N, M);` ` ` `// storing maximum number of` ` ` `// objects from given items` ` ` `int` `final = (N + M) / 3;` ` ` `return` `min(initial, final);` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 8;` ` ` `int` `M = 7;` ` ` `cout << numberOfObjects(N, M) ` ` ` `<< endl;` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above problem` `class` `GFG{` ` ` `// Function for finding` `// the maximum number of` `// objects from N type-1 and ` `// M type-2 items` `static` `int` `numberOfObjects(` `int` `N, ` `int` `M)` `{` ` ` ` ` `// Storing minimum of N and M` ` ` `int` `initial = Math.min(N, M);` ` ` `// Storing maximum number of` ` ` `// objects from given items` ` ` `int` `last = (N + M) / ` `3` `;` ` ` `return` `Math.min(initial, last);` `}` `// Driver Code` `public` `static` `void` `main(String[] args) ` `{` ` ` `int` `N = ` `8` `;` ` ` `int` `M = ` `7` `;` ` ` ` ` `System.out.println(numberOfObjects(N, M));` `}` `}` `// This code is contributed by rutvik_56` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above problem` `# Function for finding` `# the maximum number of` `# objects from N type-1 and` `# M type-2 items` `def` `numberOfObjects(N, M):` ` ` ` ` `# Storing minimum of N and M` ` ` `initial ` `=` `min` `(N, M)` ` ` `# Storing maximum number of` ` ` `# objects from given items` ` ` `final ` `=` `(N ` `+` `M) ` `/` `/` `3` ` ` `return` `min` `(initial, final)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `N ` `=` `8` ` ` `M ` `=` `7` ` ` ` ` `print` `(numberOfObjects(N, M))` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above problem` `using` `System;` `class` `GFG{` ` ` `// Function for finding` `// the maximum number of` `// objects from N type-1 and ` `// M type-2 items` `static` `int` `numberOfObjects(` `int` `N, ` `int` `M)` `{` ` ` ` ` `// Storing minimum of N and M` ` ` `int` `initial = Math.Min(N, M);` ` ` ` ` `// Storing maximum number of` ` ` `// objects from given items` ` ` `int` `last = (N + M) / 3;` ` ` ` ` `return` `Math.Min(initial, last);` `}` ` ` `// Driver Code` `public` `static` `void` `Main(` `string` `[] args) ` `{` ` ` `int` `N = 8;` ` ` `int` `M = 7;` ` ` ` ` `Console.Write(numberOfObjects(N, M));` `}` `}` ` ` `// This code is contributed by rock_cool` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity:** O(1) **Auxiliary Space Complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum modified Array sum possible by choosing elements as per the given conditions
- Count of Binary Strings possible as per given conditions
- Maximize the last Array element as per the given conditions
- Number of K's such that the given array can be divided into two sets satisfying the given conditions
- Number of ways to arrange K different objects taking N objects at a time
- Find the Maximum possible Sum for the given conditions
- Maximum Sum possible by selecting X elements from a Matrix based on given conditions
- Maximum Count of pairs having equal Sum based on the given conditions
- Is it possible to reach N and M from 1 and 0 respectively as per given conditio
- Arrange the numbers in the Array as per given inequalities
- Nth angle of a Polygon whose initial angle and per angle increment is given
- Generate an array B[] from the given array A[] which satisfies the given conditions
- Find an integer in the given range that satisfies the given conditions
- Generate an array using given conditions from a given array
- Change the given string according to the given conditions
- Check if all objects of type A and B can be placed on N shelves
- Number of terms in Geometric Series with given conditions
- Rearrange Array to maximize number having Array elements as digits based on given conditions
- Time taken per hour for stoppage of Car
- Program to find the Speed of train as per speed of sound

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.