×

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

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Java Math floorDiv() Method

The floorDiv () method of Math class returns the largest integer value that is less than or equal to the algebraic quotient. It firstly divides the dividend and divisor and...

2 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

Java Math nextDown() Method

The nextDown() method of Math class returns the floating-point number adjacent to the argument in direction of the negative infinity. Syntax: public static double nextDown (double d)public static float nextDown (float f) Parameters: The...

2 minutes read.

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

Hidden classes in Java

There specifically are some APIs available in the market that generally is harmful to be used in our programs specifically literally, and until JDK 15, there, for all intents and...

4 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

Java Delete File

There are two techniques to erase a record in Java: Utilizing File.delete() technique.Utilizing File.deleteOnExit() technique. Using File.delete() technique: In Java, we can erase a document by utilizing the File.delete() technique for File class....

2 minutes read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.

How to compare characters in Java

In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java Char The character is...

4 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 minutes read.

How to Set Java_home in Linux

To set the Java_home in Linux, we must follow several steps to make us understand it easily.  Java follows the principle called WORA (write once run anywhere). We know that java...

3 minutes read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.

Java Math getExponent() Method

The getExponent() method of Math class returns the unbiased exponent of the argument. Syntax: public static int getExponent (double d) Parameters: The parameter ‘d’ represents the double value. Return Value: The getExponent () method returns the...

1 minute read.

Coin change problem in dynamic programming

In this tutorial, we will understand a popular problem called the coin changeproblem through dynamic programming. This problem checks the logical and critical thinking ability of the person. Dynamic Programming...

5 minutes read.