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

Ice Cream Parlor : Hackerrank Problem and Solution

Ice Cream Parlor : Hackerrank Problem 


Each time Sunny and Johnny take a trip to the Ice Cream Parlor, they pool together  dollars for ice cream. On any given day, the parlor offers a line of  flavors. Each flavor, , is numbered sequentially with a unique ID number from  to  and has a cost, , associated with it.
Given the value of  and the cost of each flavor for  trips to the Ice Cream Parlor, help Sunny and Johnny choose two flavors such that they spend their entire pool of money () during each visit. For each trip to the parlor, print the ID numbers for the two types of ice cream that Sunny and Johnny purchase as two space-separated integers on a new line. You must print the smaller ID first and the larger ID second.
Note: Two ice creams having unique IDs  and  may have the same cost (i.e., ).
Input Format
The first line contains an integer, , denoting the number of trips to the ice cream parlor. The  subsequent lines describe all of Sunny and Johnny's trips to the parlor; each trip is described as follows:
  1. The first line contains .
  2. The second line contains .
  3. The third line contains  space-separated integers denoting the cost of each respective flavor. The  integer corresponding to the cost, , for the ice cream with ID number  (where ).
Constraints
  • , where 
  • It is guaranteed that there will always be a unique solution.
Output Format
Print two space-separated integers denoting the respective ID numbers for the flavors they choose to purchase, where the smaller ID is printed first and the larger ID is printed second. Recall that each ice cream flavor has a unique ID number in the inclusive range from  to .
Sample Input
2
4
5
1 4 5 3 2
4
4
2 2 4 3
Sample Output
1 4
1 2
Explanation
Sunny and Johnny make the following two trips to the parlor:
  1. The first time, they pool together  dollars. There are five flavors available that day and flavors  and  have a total cost of . Thus, we print 1 4 on a new line.
  2. The second time, they pool together  dollars. There are four flavors available that day and flavors  and  have a total cost of . Thus, we print 1 2 on a new line.

SOLUTION


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 t = sc.nextInt();
        
        for(int i =0; i<t;i++){
            
             int m = sc.nextInt();
             int n = sc.nextInt();
             int arr[] = new int[n];
                 for(int j =0; j<n;j++){
                 arr[j]= sc.nextInt();
                  }
            
                 for(int j=0;j<n;j++){
                     for(int k =j+1; k<n;k++){
                         if(arr[j]+arr[k]==m){
                          System.out.println((j+1)+" "+(k+1)); 
                          }
                     }
                 }  
        }
    }
}

Comments

Popular posts from this blog

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

Descending Order || CodeWars Problem and solution in javascript.