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 = 7Â
Output: 5Â
Explanation:Â
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 = 3Â
Output: 3Â
Explanation:Â
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 Codeint main(){Â Â Â Â int N = 8;Â Â Â Â int M = 7;Â Â Â Â cout << numberOfObjects(N, M) Â Â Â Â Â Â Â Â Â << endl;Â Â Â Â return 0;} |
Java
// Java program for the above problemclass GFG{Â Â Â Â Â // Function for finding// the maximum number of// objects from N type-1 and // M type-2 itemsstatic 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 Codepublic static void main(String[] args) {Â Â Â Â int N = 8;Â Â Â Â int M = 7;Â Â Â Â Â Â Â Â Â System.out.println(numberOfObjects(N, M));}}Â
// This code is contributed by rutvik_56 |
Python3
# Python3 program for the above problemÂ
# Function for finding# the maximum number of# objects from N type-1 and# M type-2 itemsdef 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 Codeif __name__ == '__main__':Â Â Â Â Â Â Â Â Â N = 8Â Â Â Â M = 7Â Â Â Â Â Â Â Â Â print(numberOfObjects(N, M))Â
# This code is contributed by mohit kumar 29 |
C#
// C# program for the above problemusing System;class GFG{      // Function for finding// the maximum number of// objects from N type-1 and // M type-2 itemsstatic 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 Codepublic static void Main(string[] args) {    int N = 8;    int M = 7;          Console.Write(numberOfObjects(N, M));}}  // This code is contributed by rock_cool |
Javascript
<script>// JavaScript program for the above problemÂ
// Function for finding// the maximum number of// objects from N type-1 and // M type-2 itemsfunction numberOfObjects(N, M){Â
    // storing minimum of N and M    let initial = Math.min(N, M);Â
    // storing maximum number of    // objects from given items    let final = Math.floor((N + M) / 3);    return Math.min(initial, final);}Â
// Driver Code    let N = 8;    let M = 7;    document.write(numberOfObjects(N, M)         + "<br>");Â
// This code is contributed by Surbhi Tyagi.</script> |
5
Â
Time Complexity: O(1)Â
Auxiliary Space Complexity: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
