Open In App

Maximum illumination duration with reducing candles

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of sizerepresenting the size of candles which reduce by 1 unit each day. The room is illuminated using the given N candles. Find the maximum number of days the room is without darkness.

Examples:

Input: N = 3, arr[] = {1, 1, 2}
Output: 2
Explanation: The candles’ length reduces by 1 in 1 day. So, at the end of day 1: Sizes would be 0 0 1, So, at the end of day 2: Sizes would be 0 0 0. This means the room was illuminated for 2 days.

Input: N = 5, arr[] = {2, 3, 4, 2, 1}
Output: 4

Approach: An efficient approach to solving the problem is to find the maximum element from the array using a single loop.

Follow the steps to solve the problem:

  • Initialize max element as arr[0].
  • Iterate over the array and check if arr[i] > max.
  • Then update max with arr[i].
  • Return max.

Below is the implementation for the above approach:

C++




// C++ code for above approach:
 
#include <bits/stdc++.h>
using namespace std;
 
long maxDays(vector<long>& arr, int n)
{
 
    long max = arr[0];
    for (int i = 0; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
}
 
// Driver's Code
int main()
{
    int n = 3;
    vector<long> arr = { 1, 1, 2 };
    cout << "The maximum number of days the room is "
            "without darkness are:- "
         <<
 
        // function call
        maxDays(arr, n)
 
         << endl;
    return 0;
}


C




// C code for above idea
 
#include <stdio.h>
 
long maxDays(long arr[], int n) {
    long max = arr[0];
    for (int i = 0; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
}
 
int main() {
    int n = 3;
    long arr[] = {1, 1, 2};
    printf("The maximum number of days the room is without darkness are:- %ld\n", maxDays(arr, n));
    return 0;
}


Java




// Java code for above idea
 
import java.io.*;
import java.util.*;
 
class GFG {
    public static void main (String[] args) {
     int n = 3;
      long arr[] = {1,1,2};
        System.out.println("The maximum number of days the room is without darkness are:- "+maxDays(arr,n));
       
    }
 static long maxDays(long arr[], int n){
         long max = arr[0];
        for (int i = 0; i < n; i++) {
            if(arr[i] > max) {
                max = arr[i];
            }
        }
        return max;
    }
}


Python3




# python code for above idea
 
def maxDays(arr, n):
    max = arr[0]
    for i in range(n):
        if arr[i] > max:
            max = arr[i]
    return max
 
n = 3
arr = [1, 1, 2]
print("The maximum number of days the room is without darkness are:- " + str(maxDays(arr, n)))


C#




using System;
using System.Collections.Generic;
 
class GFG
{
    static long MaxDays(List<long> arr, int n)
    {
        long max = arr[0];
        for (int i = 0; i < n; i++)
        {
            if (arr[i] > max)
            {
                max = arr[i];
            }
        }
        return max;
    }
 
    static void Main(string[] args)
    {
        int n = 3;
        List<long> arr = new List<long>() { 1, 1, 2 };
        Console.WriteLine("The maximum number of days the room is without darkness are: " + MaxDays(arr, n));
    }
}


Javascript




// JavaScript code for above idea
 
function maxDays(arr, n) {
  let max = arr[0];
  for (let i = 0; i < n; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
  }
  return max;
}
 
let n = 3;
let arr = [1, 1, 2];
console.log("The maximum number of days the room is without darkness are:- " + maxDays(arr, n));
// This code is contributed by Tapesh(tapeshdua420)


Output

The maximum number of days the room is without darkness are:- 2





Time Complexity: O(N), Single loop for traversing the array.
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads