Find minimum chocolates left after following the given rules
Last Updated :
06 Dec, 2022
Geek has N packs of chocolates and the amount of chocolates in each pack is given in an array arr[]. His sister wants to have a pack of chocolate. Geeks being greedy will pick the packs with more number of chocolates but he can pick from either first or last. Find the number of chocolates his sister will get.
Examples:
Input: arr[ ] = {5, 3, 1, 6, 9}
Output: 1
Explanation: Geek will have the packs with chocolates
5, 3, 6, 9.
Input: arr[ ] = {5, 9, 2, 6}
Output: 2
Approach: The problem can be solved using the following observation:
As Geek is picking chocolate packs greedily, so he can always make sure that the pack with the minimum number of chocolates is left at the end.
Follow the below steps to implement the idea:
- Declare a variable (say ans) and initialize ans = arr[0] to store the minimum number of chocolates.
- Iterate from i = 1 to N-1:
- If the value of arr[i] is less than ans, update ans = arr[i].
- The final value stored in ans will be the minimum value. So return ans as the required answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int chocolates( int arr[], int n)
{
int ans = arr[0];
for ( int i = 1; i < n; i++) {
if (arr[i] < ans) {
ans = arr[i];
}
}
return ans;
}
int main()
{
int arr[] = { 5, 3, 1, 6, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << chocolates(arr, N) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int chocolates( int [] arr, int n)
{
int ans = arr[ 0 ];
for ( int i = 1 ; i < n; i++) {
if (arr[i] < ans) {
ans = arr[i];
}
}
return ans;
}
public static void main(String[] args)
{
int [] arr = { 5 , 3 , 1 , 6 , 9 };
int N = arr.length;
System.out.print(chocolates(arr, N));
}
}
|
C#
using System;
public class GFG {
public static int chocolates( int [] arr, int n)
{
int ans = arr[0];
for ( int i = 1; i < n; i++) {
if (arr[i] < ans) {
ans = arr[i];
}
}
return ans;
}
static public void Main()
{
int [] arr = { 5, 3, 1, 6, 9 };
int N = arr.Length;
Console.WriteLine(chocolates(arr, N));
}
}
|
Python3
def chocolates(arr, n):
ans = arr[ 0 ]
for i in range ( 1 , n):
if (arr[i] < ans):
ans = arr[i]
return ans
if __name__ = = '__main__' :
arr = [ 5 , 3 , 1 , 6 , 9 ]
N = len (arr)
print (chocolates(arr, N))
|
Javascript
<script>
function chocolates(arr,n)
{
let ans = arr[0];
for (let i = 1; i < n; i++) {
if (arr[i] < ans) {
ans = arr[i];
}
}
return ans;
}
let arr = [ 5, 3, 1, 6, 9 ];
let N = arr.length;
console.log(chocolates(arr, N));
</script>
|
PHP
<?php
function chocolates( $arr , $n ) {
$ans = $arr [0];
for ( $i = 1; $i < $n ; $i ++) {
if ( $arr [ $i ] < $ans ) {
$ans = $arr [ $i ];
}
}
return $ans ;
}
$arr = [5, 3, 1, 6, 9];
$n = sizeof( $arr )/sizeof( $arr [0]);
echo chocolates( $arr , $n );
?>
|
Time Complexity: O(N) As we are traversing the array once
Auxiliary Space: O(1) As we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...