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...

Jumping on the Clouds Revisited: Hackerrank Problem and Solution in java

Jumping on the Clouds  Revisited

Hackerrank Problem:
Aerith is playing a cloud game! In this game, there are  clouds numbered sequentially from  to . Each cloud is either an ordinary cloud or a thundercloud.
Aerith starts out on cloud  with energy level . She can use  unit of energy to make a jump of size  to cloud , and she jumps until she gets back to cloud . If Aerith lands on a thundercloud, her energy () decreases by  additional units. The game ends when Aerith lands back on cloud .
Given the values of , and the configuration of the clouds, can you determine the final value of  after the game ends?
Note: Recall that  refers to the modulo operation.
Input Format
The first line contains two space-separated integers,  (the number of clouds) and  (the jump distance), respectively. 
The second line contains  space-separated integers describing the respective values of clouds . Each cloud is described as follows:
  • If , then cloud  is an ordinary cloud.
  • If , then cloud  is a thundercloud.
Constraints
Output Format
Print the final value of  on a new line.
Sample Input
8 2
0 0 1 0 0 1 1 0
Sample Output
92
Explanation
In the diagram below, red clouds are thunderclouds and purple clouds are ordinary clouds:
game board
Observe that our thunderclouds are the clouds numbered , and . Aerith makes the following sequence of moves:
  1. Move: , Energy: .
  2. Move: , Energy: .
  3. Move: , Energy: .
  4. Move: , Energy: .
Thus, we print  as our answer.
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 c[] = new int[n];
        for(int c_i=0; c_i < n; c_i++){
            c[c_i] = in.nextInt();
        }
        int e = 100;
        for(int i=0;i<n;i=i+k){
            if(c[i]==0){
                e=e-1;
            }
            else
                e=e-2-1;
        }
       
        System.out.println(e);
    }
}


Comments

Popular posts from this blog

Ice Cream Parlor : Hackerrank Problem and Solution

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

Descending Order || CodeWars Problem and solution in javascript.