Dutch national flag problem in Javascript

Image
Dutch national flag problem and solution in Javascript Problem statement:   The Dutch national flag (DNF) problem is one of the most popular programming problems proposed by Edsger Dijkstra. The flag of the Netherlands consists of three colors: white, red, and blue. The task is to randomly arrange balls of white, red, and blue such that balls of the same color are placed together. Now, let's consider an array with 3 distinct values say 0, 1 and 2. We won't be using any sort method and we need to sort this array in 0(n). Input Array :  let   arr  = [ 0 ,  2 ,  1 ,  0 ,  1 ,  2 ,  0 ,  2 ]; Expected Output: [ 0, 0, 0, 1, 1, 2, 2, 2 ] Solution Approach : When we see expected output, we can clearly see that sorted array is divided into 3 sections having values 0 , 1 and 2. So, let's divide the array in 3 sections: a) from 0th index to left boundary b) from left boundary to right boundary c) from right boundary to last index. Now we will create 2 pointers : left (starting from 0

HourRank 20 : Hot and Cold problem and solution in java

HourRank 20 : Hot and Cold problem:
Carl, Caroline, Helen, and Han are four friends sharing a one-room workspace. The workspace has a single thermostat which they can set to any integer temperature between  degrees to  degrees Fahrenheit, inclusive.
The four friends can't agree on the room's temperature! Carl and Christina don't want it to be too cold, while Helen and Han don't want it to be too hot. Specifically:
  • Carl wants it to be at least  degrees Fahrenheit.
  • Caroline wants it to be at least  degrees Fahrenheit.
  • Helen wants it to be at most  degrees Fahrenheit.
  • Han wants it to be at most  degrees Fahrenheit.
Given , and , is there a satisfactory temperature that all four friends will be happy with? If it's possible, print YES; otherwise, print NO.
Input Format
Four space-separated integers describing the respective values of , and .
Constraints
Output Format
Print YES if it's possible to satisfy all four friends' conditions; otherwise, print NO instead.
Sample Input 0
50 40 70 60
Sample Output 0
YES
Explanation 0
The four friends have the following temperature preferences:
  • Carl wants it to be at least  degrees.
  • Caroline wants it to be at least  degrees.
  • Helen wants it to be at most  degrees.
  • Han wants it to be at most  degrees.
image
Any temperature between  and  degrees will satisfy all four friends, so we print YES.
Sample Input 1
55 66 66 77
Sample Output 1
YES
Explanation 1
The four friends have the following temperature preferences:
  • Carl wants it to be at least  degrees.
  • Caroline wants it to be at least  degrees.
  • Helen wants it to be at most  degrees.
  • Han wants it to be at most  degrees.
image
A temperature of exactly  degrees will satisfy all four friends, so we print YES.
Sample Input 2
80 80 40 40
Sample Output 2
NO
Explanation 2
In this test case, Carl wants the temperature to be at least  and Helen wants it to be at most . There is no temperature that is both  and , so we print NO because no satisfactory temperature exists.
image

Solution in java:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static String isSatisfiable(int c1, int c2, int h1, int h2){
        // Complete this function
        int min=0;
        int max=0;
        
        if(c1>c2)
            min=c1;
        else
            min=c2;
        
        if(h1<h2)
            max=h1;
        else
            max=h2;
        
        if(max-min>=0)
            return "YES";
        else
            return "NO";
            
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // Return "YES" if all four conditions can be satisfied, and "NO" otherwise
        int c1 = in.nextInt();
        int c2 = in.nextInt();
        int h1 = in.nextInt();
        int h2 = in.nextInt();
        String result = isSatisfiable(c1, c2, h1, h2);
        System.out.println(result);
    }
}

Comments

  1. It’s always so sweet and also full of a lot of fun for me personally and my office colleagues to search your blog a minimum of thrice in a week to see the new guidance you have got.
    <a href="http://www.traininginmarathahalli.in/core-java-training-in-bangalore/”> Java Training in Marathahalli </a>|

    ReplyDelete
  2. @giselle thanks for such kind and motivating words.

    ReplyDelete

Post a Comment

Popular posts from this blog

Ice Cream Parlor : Hackerrank Problem and Solution

Javascript Problem: Find the next perfect square!!

Disemvowel Trolls || Codewars problem and solution in Javascript || Topic : Strings and RegEx