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

Beautiful days at the movies: Hackerrank Problem and Solution in java

Beautiful days at the movies: Hackerrank Problem

Problem:
Lily likes to play games with integers and their reversals. For some integer , we define  to be the reversal of all digits in . For example, , and .
Logan wants to go to the movies with Lily on some day  satisfying , but he knows she only goes to the movies on days she considers to be beautiful. Lily considers a day to be beautiful if the absolute value of the difference between  and  is evenly divisible by .
Given , and , count and print the number of beautiful days when Logan and Lily can go to the movies.
Input Format
A single line of three space-separated integers describing the respective values of , and .
Constraints
Output Format
Print the number of beautiful days in the inclusive range between  and .
Sample Input
20 23 6
Sample Output
2
Explanation
Logan wants to go to the movies on days , and . We perform the following calculations to determine which days are beautiful:
  • Day  is beautiful because the following evaluates to a whole number: 
  • Day  is not beautiful because the following doesn't evaluate to a whole number: 
  • Day  is beautiful because the following evaluates to a whole number: 
  • Day  is not beautiful because the following doesn't evaluate to a whole number: 
Only two days,  and , in this interval are beautiful. 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) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        int j = sc.nextInt();
        int k = sc.nextInt();
        
        int n= j-i+1;
        int count=0;
           
        while(i<=j){
        int number= i;
        int reverse= 0;
        
             while(number!= 0){
            reverse = (reverse*10)+(number%10);
            number = number/10;
             } 
            int diff= Math.abs(i-reverse);
           if(diff % k==0||diff==0){
                count++;
                    }
        i++;
        }
      System.out.println(count); 
    }
}

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.