Javascript required
Skip to content Skip to sidebar Skip to footer

Find Next Perfect Square Javascript Solution

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

Javascript Problem: Find the next perfect square!!

Codewars Problem and solution with approach explained

Objective:

In this challenge, we will solve the "Find the next perfect square!" Codewars puzzle using javascript .

Problem statement:

You might know some pretty large perfect squares. But what about the NEXT one?

Complete the findNextSquare method that finds the next integral perfect square after the one passed as a parameter. Recall that an integral perfect square is an integer n such that sqrt(n) is also an integer.

If the parameter is itself not a perfect square then -1 should be returned. You may assume the parameter is positive.

Examples:

findNextSquare (121) --> returns 144

findNextSquare (625) --> returns 676

findNextSquare (114) --> returns -1 since 114 is not a perfect

Solution Approach:

Step 1: check if the perfect square root of the given number say "n" is available.

Step 2: if yes, then simply get the square of the "n+1" number.

Step 3: if no, return false.

Keeping these  points in mind, let's jump to the solution now:

Solution :

function findNextSquare (sq) {

  // Return the next square if sq is a perfect square, -1 otherwise

  if ( Number. isInteger ( Math. sqrt (sq)))

    return ( Math. sqrt (sq)+ 1)* ( Math .sqrt (sq)+1);

  else

    return -1;

}

Popular posts from this blog

JAVA Date and time (HACKERRANK problem)

Problem The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. You are given a date. Your task is to find what the  day  is on that date. Input Format A single line of input containing the space separated month, day and year, respectively, in      format. Constraints Output Format Output the correct day in capital letters. Sample Input 08 05 2015 Sample Output WEDNESDAY Explanation The day on August  th    was  WEDNESDAY . 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) {         Scanner in = new Scanner(System.in);         String

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

Problem: Disemvowel Trolls Description : Trolls are attacking your comment section! A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat. Your task is to write a function that takes a string and return a new string with all vowels removed. For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!".   Solution 1# function disemvowel(str) {   var str = str.replace(/a/gi,'').replace(/e/gi,'').replace(/i/gi,'').replace(/o/gi,'').replace(/u/gi,'');   return str; } Solution 2# (slightly more concise) function disemvowel(str) {   return str.replace(/[aeiou]/gi, ''); }

Hackerrank Week of Code 30 : Candy Replenishing Robot Solution in java

Image

Candy Replenishing Robot: Hackerrank week of code problem: Alice is hosting a party! The party lasts for   minutes, and she puts out a bowl of   candies at the beginning of the party. During each minute  , a person comes to the bowl and removes   candies. Alice programs the following algorithm into her robot, Bob, to replenish the candy throughout the party: If the party is ending (i.e., it's time  ), do not refill the bowl. If the bowl contains   candies at the end of minute   and  , add   candies to the bowl. For example, if  ,  , and  , then the candy bowl looks like this throughout the party: Note that Bob doesn't replenish the bowl at the party's end, so a total of   candies were added during the party. Given  ,  , and the number of candies removed from the bowl during each minute, print the total number of new candies Bob adds to the bowl during the party. Input Format The first line contains two space-separated integers describing th

Find Next Perfect Square Javascript Solution

Source: https://javasolution4u.blogspot.com/2020/08/javascript-codewars-problem-solution-find-next-perfect-square.html