Posts

Showing posts from March, 2017

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

Divisible Pairs Sum: Hackerearth problem and solution in java

Divisible Pairs Sum: Hackerearth problem You are given an array of   integers,  , and a positive integer,  . Find and print the number of   pairs where   and   +   is evenly divisible by  . Input Format The first line contains   space-separated integers,   and  , respectively.  The second line contains   space-separated integers describing the respective values of  . Constraints Output Format Print the number of   pairs where   and   +   is evenly divisible by  . Sample Input 6 3 1 3 2 6 1 2 Sample Output 5 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 a[] = new int[n];         for(int a_i=0; a_i < n; a_i++){          

The Next Big Thing: Hackerearth Problem and Solution

The Next Big Thing: Hackerearth Problem and Solution in java Problem: Saharsh (aka  The Next Big Thing ) has a lots of ups and downs in his coding life. He rates his performance of each contest as color code. He has 5 different color codes -  "Purple"-P  ,  "Blue"-B ,  "Yellow"-Y ,  "Orange"-O  and  "Red"-R .  P  is the code for worst performance and  R  for the best. Saharsh keeps track of all his performances using  performance string , where  i 'th character of it denotes the performance code for  i 'th contest. Vaibhav, his friend, is a big stalker and is interested in knowing the number of times Saharsh became "Red" coder immediately after being "Orange" coder. Input First line contains a single integer  t  denoting the number of test cases. Each of the next  t  lines contains a string  s  denoting his  performance string . Output For each testcase, print a single line containing required o

Week of Code 30 :"Find the Minimum Number" Problem and solution in java

Week of Code 30 :"Find the Minimum Number"  Problem: Jessica is learning to code and was recently introduced to the   function. This function compares two integers and returns the smaller one. This is what calling the function looks like when comparing two integers   and  : min(a, b) Jessica realizes that she can also find the smallest of three integers  ,  , and   if she puts the   function inside of another   function: min(a, min(b, c)) For four integers she can nest the functions once more: min(a, min(b, min(c, d))) Jessica is curious about the structure of these function calls and wants to see if she can write a program to construct a string that shows how   number of integers can be compared with nested   functions. Can you help Jessica write this program? Input Format The input contains a single integer   (the number of integers to be compared). Constraints Output Format Print the string on a single line. Each integer