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

Project Euler #6: Sum square difference : Problem and Solution

Project Euler #6: Sum square difference : Problem and Solution in java

Problem:
The sum of the squares of the first ten natural numbers is, . The square of the sum of the first ten natural numbers is, . Hence the absolute difference between the sum of the squares of the first ten natural numbers and the square of the sum is .
Find the absolute difference between the sum of the squares of the first  natural numbers and the square of the sum.
Input Format
First line contains  that denotes the number of test cases. This is followed by  lines, each containing an integer, .
Constraints
Output Format
Print the required answer for each test case.
Sample Input 0
2
3
10
Sample Output 0
22
2640
Explanation 0
  • For  , 
  • 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 sc = new Scanner(System.in);
                              int t= sc.nextInt();
                                   for(int i=0;i<t;i++){
                                         int n = sc.nextInt();
                                           long sts= 0;
                                             long ses=0;
                                                   for(int j=1;j<=n;j++){
                                                         sts= sts+j ;
                                                           ses= ses+ (j*j);
                                                         }
                                                         long  nsts= sts*sts;
                                                           long  sum = Math.abs(nsts-ses);
                                                             System.out.println(sum);
                                                               
                                                            }
                                                      }
                                                    }

                                                    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.