×

Minimum Lights to Activate Java Snippet Class

Minimum Lights to Activate Problem in Java

In prison, there is a hallway that is N units long. Given an N-dimensional array A. If the light at the ith position is broken, the ith index of this array is 0, otherwise it is 1.

All lights have a specified power B, and if they are all in position X, they will all be able to illuminate the hallway from [X-B+1, X+B-1].

At first, all of the lights are off.

If the entire corridor cannot be lit, return -1 instead of the minimal number of lights that must be turned on.

Let’s understand the code demonstration of above problem in Java.

Code:

// import the packages  
import java.util.*;  
import java.io.*;
public class MinimumLights 
{  
    // first find the mininmum one among the two x and y 
    int min(int x, int y)  
    {  
        if(x > y)  
        {  
            return y;  
        }  
        return x;  
    }  
    // next find the maximum one among the two x and y 
    int max(int x, int y)  
    {  
        if(x > y)  
        {  
            return x;  
        }  
        return y;  
        
    }  
    // a technique that determines the bare minimum of lights needed to illuminate the entire hallway.  
    public int minnumberlight(int larr[], int lPower)  
    {  
        int n = larr.length;  
        int dparr[] = new int[n + 1];  
        for(int i= 0; i < n + 1; i++)  
        {  
            dparr[i] = Integer.MAX_VALUE / 2;  
        }  
        // Assume that dp[j] represents the minimum number of lights necessary to illuminate the corridor up to the jth location.  
      
        dparr[0] = 0; //0 bulbs are needed to cover the 0th region.
      
        for(int i = 0; i < n; i++)  
        {  
            if(larr[i] == 0)   
            {  
            // The lamp is broken. Check for the next light as a result. 
            continue;  
            }  
            int left = max(0, i - lPower + 1) + 1; // +1 as dpArr.length == size + 1  
        int right = min(i + lPower - 1, n - 1) + 1;  
          
        // calculating the bare minimum of lights needed to illuminate the corridor until the jth position
        for(int j = left; j <= right; j++)  
        {  
            dparr[j] = min(dparr[j], dparr[left - 1] + 1);  
        }  
    }  
   if(dparr[n] == Integer.MAX_VALUE / 2)  
    { 
        return -1; 
        
    }  
    return dparr[n];  
        
    }  
// main method  
public static void main(String[] argvs)  
{  
// creating an object of the class MinimumLights
    MinimumLights obj = new MinimumLights();  
    int larr[] = {1, 1, 0, 1, 1};  
    int n = larr.length;  
    int lPower = 3;  
    int ans = obj.minnumberlight(larr, lPower);  
    System.out.println("for the given lights: ");   
    for(int i = 0; i < n; i++)  
    {  
        System.out.print(larr[i] + " ");  
    }  
    System.out.println();  
    if(ans != -1)  
    {  
        System.out.println("The bare minimum of lights required to illuminate the entire corridor is: " + ans);  
    }  
    else  
    {  
        System.out.println("It is impossible to illuminate the entire corridor.");     
    }  
    int larr1[] = {0, 0, 0, 1, 1, 1, 1, 1, 1};  
    n = larr1.length;  
    lPower = 3;  
    System.out.println();  
    ans = obj.minnumberlight(larr1, lPower);  
    System.out.println("For the following lights: ");   
    for(int i = 0; i < n; i++)  
    {  
        System.out.print(larr1[i] + " ");  
    }  
    System.out.println();  
    if(ans != -1)  
    {  
        System.out.println("The minimum number of lights to light up the whole corridor is: " + ans);  
    }  
    else  
    {  
        System.out.println("It is not possible to light up the whole corridor.");     
    }  
  
}  
}  

Output:

cd /home/cg/root/6367601bbbf54
for the given lights: 
1 1 0 1 1 
The bare minimum of lights required to illuminate the entire corridor is: 2
For the following lights: 
0 0 0 1 1 1 1 1 1 
It is not possible to light up the whole corridor.

Try to turn the problem into a different form or approach like you normally do while solving difficulties. Usually, this will make the issue simpler. When we read this problem, one thought that comes to mind is to divide it into pairs of ranges that the light illuminates.

Nested for-loops were utilised to calculate the solution. Consequently, the program's time complexity is O(n2). Additionally, we added an auxiliary array to store the results, making the program's space complexity O(n), where n is the total number of entries in the array.


Related Topics

Vigesimal in Java

A number system with a base of 20 is known as the vigesimal in Java. A base-20 (base-score) numeric system, often known as a vigesimal system, is centered on the twenty...

4 minutes read.

Difference between print() and println() in Java

In this tutorial, we will discuss the differences between print and println in Java language. There are mainly two methods to display the text on the console printprintln The above two methods are...

4 minutes read.

Byte to Hex in Java

Java exclusively uses byte data types to store in a byte array, which is an array. Each component of a byte array has a default value of 0. Hex String -...

3 minutes read.

Blocking Queue in Java Example

Let's first briefly comprehend queue before moving on to the topic of "Blocking Queue." A queue seems to be an orderly list of items in which elements are added from...

8 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

JDK | Java Development Kit

Java Development Kit (JDK) The Java Development Kit is a software development environment used to create Java applications. The JDK includes JRE (Java Runtime Environment), an interpreter (java), a compiler (javac),...

3 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.

Best Practices to use String Class in Java

Use String Builder or String Buffer for String concatenation in place of + operator.Compare two strings by equals( ) method instead == operator.Call .equals( ) method on a known String...

3 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

How to compare dates in Java

Introduction: In Java, dates can be compared using a similar interface's compareTo() technique. This method returns 'zero' if each date is the same, returns a rate "more than 0" if...

6 minutes read.

Check if the given array is mirror inverse in Java

The primary objective is to check whether the given array is mirror inverse or not. The values and position of the specified array are switched, and a duplicate array is created....

3 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

How to calculate time difference in Java

In this tutorial, we learned about how to calculate time differences in java language and which methods use to find the difference and its example. Prerequisite To understand this example, you need...

5 minutes read.

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

Fork Join in Java

Multithreaded processors are being introduced in new computer systems today. The operation is faster due to multicore CPUs. Therefore, it becomes essential for a programmer to leverage multithreaded processors effectively...

4 minutes read.

How to use scanner in Java

Scanner class in Java is the part of java.util package. Java programming language has various ways to read input from the user, Scanner class is one of the classes to...

5 minutes read.