Check if given number is perfect square – O(√N) Solution

Objective: Given a number, write a program to check if given number is perfect sqaure. Example: N = 16 Output: True N = 32 Output: False Approach: Naive Approach: If N = 1 return true. Iterate through 1 to N/2 and check for each number whether square of each number is equal to N, if … Read more Check if given number is perfect square – O(√N) Solution

Find the roots of Quadratic Equation – Java Program

Quadratic equation is in the form = ax2 + bx + c Discriminant (d) = b2 – 4ac Then roots are calculates using the equations below Root_1 =  (-b + √d)/2a Root_2 =  (-b – √d)/2a See the image below Run This Code Java Code: Output: Quadratic Equation: +3x^2 -5x -8 = 0, Roots are:(2.6666666666666665, … Read more Find the roots of Quadratic Equation – Java Program

Calculate Logn base r – Java Implementation

Objective: Given a number n and r, write a program to calculate Logrn Example: N = 32, r =2 Log232= 5 N = 64, r = 4 Log464= 3 Approach: Without using built-In function- Initialize result = 0. Keep dividing the given number by r till number is greater than 0 and add one to … Read more Calculate Logn base r – Java Implementation

Floyd’s Triangle – Java Implementation

Floyd’s Triangle: Floyd’s triangle is a right angled triangular array of natural numbers. It named after Robert Floyd. Rows of the triangle filled by consecutive numbers. First row will have single number which is 1. Second row will have two numbers, which are 2 and 3 Third row will have three numbers, which are 4, … Read more Floyd’s Triangle – Java Implementation

Java Program to determine if Given Year is Leap Year

Objective: Given a Year, write a java program to find whether year is leap year or not. Leap Year: a year, usually occurring once every four years, that has 366 days including February 29 as an intercalary day, called Leap Year. Example: Year: 2004, 2008, 2012 are Leap years. Year: 1993, 2001, 2003 are not … Read more Java Program to determine if Given Year is Leap Year

Find number of Distinct Permutations of a String

Objective – Given a string, find the number of distinct permutations or anagrams of that string. Example: String x = “abc” Output: 6 (abc, acb, bac, bca, cab, cba) String x = “aab” Output: 3 (aab, aba, baa) Approach: Number of ways to arrange n distinct items are = n!. If we have duplicate items, … Read more Find number of Distinct Permutations of a String

Minimum number of guesses needed to find a specific number

Objective– Given the numbers 1 to 1000, what is the minimum number of guesses needed to find a specific number if you are given the hint “higher” or “lower” for each guesses you make. Naive Approach: Linear search Start guessing from 1 and then 2 then 3  …till we do not find the answer. Time … Read more Minimum number of guesses needed to find a specific number

Java Program to find if Triangle can be formed using given 3 sides

Algorithms – Java Program to find if Triangle can be formed using given 3 sides Objective– Given 3 side lengths, write a program to find out if using these 3 sides, a triangle can be formed. Example: Triangle can be formed using side 2.5, 3.5, 5.0 Triangle cannot be formed using side 1.0, 3.0, 5.0 … Read more Java Program to find if Triangle can be formed using given 3 sides