Maximum no. of apples that can be kept in a single basket
Last Updated :
13 Mar, 2023
Given the ‘N’ number of Basket and the total of Green ‘G’ and Red ‘R’ apples. The task is to distribute all the apples in the Basket and tell the maximum number of apples that can be kept in a basket.
Note: None of the basket is empty.
Examples:
Input: N = 2, R = 1, G = 1
Output: Maximum apple kept is = 1
Input: N = 2, R = 1, G = 2
Output: Maximum apple kept is = 2
Approach: The idea is to just check the difference between no. of baskets and total no. of apples(red and Green) i.e. first put 1 apple in 1 basket that means the remaining apples will be extra and can be put together in any basket to make the count maximum. As there is already 1 apple in the basket. So, the maximum number of apples will be (No_of_apples – No_of_baskets) + 1. Since it is mentioned that none of the baskets is empty so apples will always be equal to or greater than no. of baskets.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Number( int Basket, int Red, int Green)
{
return (Green + Red) - Basket + 1;
}
int main()
{
int Basket = 3, Red = 5, Green = 3;
cout << "Maximum apple kept is = "
<< Number(Basket, Red, Green);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int Number( int Basket, int Red, int Green)
{
return (Green + Red) - Basket + 1 ;
}
public static void main(String[] args)
{
int Basket = 3 , Red = 5 , Green = 3 ;
System.out.println( "Maximum apple kept is = "
+ Number(Basket, Red, Green));
}
}
|
Python3
def Number(Basket, Red, Green):
return (Green + Red) - Basket + 1
if __name__ = = '__main__' :
Basket = 3
Red = 5
Green = 3
print ( "Maximum apple kept is =" ,
Number(Basket, Red, Green))
|
C#
using System;
public class GFG {
static int Number( int Basket, int Red, int Green)
{
return (Green + Red) - Basket + 1;
}
static public void Main()
{
int Basket = 3, Red = 5, Green = 3;
Console.WriteLine( "Maximum apple kept is = "
+ Number(Basket, Red, Green));
}
}
|
PHP
<?php
function Number( $Basket , $Red , $Green )
{
return ( $Green + $Red ) - $Basket + 1;
}
$Basket = 3;
$Red = 5 ;
$Green = 3;
echo "Maximum apple kept is = " ,
Number( $Basket , $Red , $Green );
?>
|
Javascript
<script>
function Number(Basket, Red, Green)
{
return (Green + Red) - Basket + 1;
}
var Basket = 3, Red = 5, Green = 3;
document.write( "Maximum apple kept is = " +
Number(Basket, Red, Green));
</script>
|
Output:
Maximum apple kept is = 6
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...