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

The Hurdle Race: Hackerrank problem and Solution

The Hurdle Race: Hackerrank problem

Problem:
Dan is playing a video game in which his character competes in a hurdle race by jumping over  hurdles with heights . He can initially jump a maximum height of  units, but he has an unlimited supply of magic beverages that help him jump higher! Each time Dan drinks a magic beverage, the maximum height he can jump during the race increases by  unit.
Given , and the heights of all the hurdles, find and print the minimum number of magic beverages Dan must drink to complete the race.
Input Format
The first line contains two space-separated integers describing the respective values of  (the number of hurdles) and  (the maximum height he can jump without consuming any beverages). 
The second line contains  space-separated integers describing the respective values of .
Constraints
Output Format
Print an integer denoting the minimum number of magic beverages Dan must drink to complete the hurdle race.
Sample Input 0
5 4
1 6 3 5 2
Sample Output 0
2
Explanation 0
Dan's character can jump a maximum of  units, but the tallest hurdle has a height of :
image
To be able to jump all the hurdles, Dan must drink  magic beverages.
Sample Input 1
5 7
2 5 4 5 2
Sample Output 1
0
Explanation 1
Dan's character can jump a maximum of  units, which is enough to cross all the hurdles:
image
Because he can already jump all the hurdles, Dan needs to drink  magic beverages.
Solution in java:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int k = in.nextInt();
        int[] height = new int[n];
        
        
        for(int height_i=0; height_i < n; height_i++){
            height[height_i] = in.nextInt();
        
        }
        int largest= height[0];
        for(int i=0;i<n;i++){
            
              if(height[i]>largest){
               largest=height[i]; 
              }
        }
       if(largest>k){
           System.out.println(largest-k);
       } 
        else
          System.out.println("0");  
        
    }
}


Comments

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