Posts

Java Program to Reverse a String using Stack - The Coding Shala

Home >> Java Programs >> Reverse a String using Stack  Hey there, welcome back to another post. In this post, we will learn how to reverse a string using a stack in Java. Java Program to Reverse a String using Stack As we know,  Stack data structure follows last in the first out (LIFO), so by using stack we can reverse a given string. For example:  Input: hello output: olleh After storing into stack Stack -> o l l e h Now print stack -> olleh Java Program:  import java.util.Scanner; import java.util.Stack; /** * https://www.thecodingshala.com/ */ public class Main { public static String doReverse(String str) { Stack<Character> stack = new Stack<>(); // push all characters into stack for ( int i = 0; i < str.length(); i++) { stack.push(str.charAt(i)); } // pop characters from stack and build string Stri

Java Program to Find LCM of Two Numbers - The Coding Shala

Home >> Java Programs >> Find LCM of Two Numbers  Hey there, welcome back to another post. In this post, we will learn how to find the LCM of two numbers in Java. Java Program to Find LCM of Two Numbers The LCM (Least Common Multiple) is the smallest number that can be divided by both numbers. For example, the LCM of 5 and 10 is 10 because 10 is the smallest number that is divisible by both 5 and 10. Example 1:   Input Number1: 8 Number2: 10 Output LCM of 8 and 10 is: 40 Find LCM of Two Numbers using GCD We can find the LCM of two numbers by using GCD. The relation between LCM and GCD is:  a x b = LCM(a, b) * GCD (a, b) LCM(a, b) = (a x b) / GCD(a, b) For example, LCM(8, 10) is: LCM(8, 10) = ( 8 * 10) / GCD(8,10) LCM(8, 10) = 80 / 2 = 40 Java Program:   import java.util.Scanner; /** * https://www.thecodingshala.com/ */ public class Main { public static int findGCD( int num1, int num2) { if (num1 == 0) { return

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala

Home >> Java Programs >> Find GCD of Two Numbers  Hey there, welcome back to another post. In this post, we will learn how to find GCD or HCF of Two Numbers in Java. Java Program to Find GCD or HCF of Two Numbers The GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. For example:  Number1: 6 (2 * 3) Number2: 9 (3 * 3) GCD or HCF of 6 and 9 is: 3 Find GCD of Two Numbers in Java using for Loop As we already know, GCD is the largest number that divides both numbers. We can start a for loop from 1 (because 1 divides all the numbers) to both numbers and if it divides both numbers then update GCD. Java Program:  import java.util.Scanner; /** * https://www.thecodingshala.com/ */ public class Main { public static void findGCD( int num1, int num2) { int gcd = 1; if (num1 == 0) { gcd = num2; } if (num2 == 0) { gcd = num

N-th Tribonacci Number Solution - The Coding Shala

Home >> Programming >> N-th Tribonacci Number  Hey there, welcome back to another post. In this post, we will learn how to solve the N-th Tribonacci Number problem and will implement its solution in Java. N-th Tribonacci Number Problem Statement The Tribonacci sequence Tn is defined as follows: T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0. Given n, return the value of Tn.  Example 1:  Input: n = 4 Output: 4 Explanation: T_3 = 0 + 1 + 1 = 2 T_4 = 1 + 1 + 2 = 4 N-th Tribonacci Number Java Solution using Bottom-Up DP This problem is similar to the Fibonacci series. We can solve it using the Bottom-Up approach of dynamic programming. Time Complexity: O(n) Space Complexity: O(n) The space complexity can be reduced to O(1) by using variables to store the previous three values instead of using an array. Java Program:  class Solution { public int tribonacci( int n) { if (n == 0) return 0; if (n == 1 || n == 2

Java Program to Convert Binary to Decimal - The Coding Shala

Home >> Java Programs >> Convert Binary to Decimal  Hey there, welcome back to another post. In this post, we will learn how to convert Binary to Decimal in Java. Java Program to Convert Binary to Decimal You have given a binary number, convert it to a decimal number. Example:  Input: 101 Output: 5 Input: 111 Output: 7 Binary to Decimal Conversion in Java Approach We will the below formula to convert binary to decimal: decimal number = (2^0) * (rightmost digit) + (2^1) * (second rightmost digit) + ...  for example:  binary number = 110 decimal number = 2^0 * 0 + 2^1 * 1 + 2^2 * 1 = 1 * 0 + 2 * 1 + 4 * 1 = 0 + 2 + 4 = 6 Java Program:  import java.util.Scanner; /** * https://www.thecodingshala.com/ */ public class Main { public static void printDecimal( int num) { int decimal = 0; int twos = 1; // 2^0 = 1 initial value while (num > 0) { int temp = num

Java Program to Convert Decimal to Binary - The Coding Shala

Home >> Java Programs >> Convert Decimal to Binary  Hey there, welcome back to another post. In this post, we will learn how to convert decimal to binary in Java. Java Program to Convert Decimal to Binary You have given a decimal number. Write a Java program to convert decimal to binary. Example 1:  Input: 7 Output: 111 Input: 10 Output: 1010 Decimal to Binary Conversion using Array Approach To convert decimal to binary we can follow the below steps: Store the remainder in the array when we divide the number by 2. Divide the number by 2. Follow the above two steps until the number is above 0. Print the array in reverse order. For example:  4 % 2 = 0 => 4/2 = 2 2 % 2 = 0 => 2/2 = 1 1 % 2 = 1 => 1/2 = 0 Binary of 4 is: 100 Java Program:  import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * https://www.thecodingshala.com/ */ public class Main { public static void printBinary( int num) {

Find Second Smallest Element in the Array - The Coding Shala

Home >> Programming >> Find Second Smallest Element in the Array  Hey there, welcome back to another post. In this post, we will learn how to find the second smallest number in the array in Java. Find Second Smallest Element in the Array Problem Statement You have given an integer array, return the second smallest element from the array if exists else return no second smallest element exists. Example 1:  Input: [1, 4, 2, 7, 90, -1, -4] Output: -1 Find Second Smallest Element in the Array Solution using Single Iteration Approach By using the below steps we can find the second smallest element in the array by using a single iteration: Initialize two variables first and second for smallest and second smallest numbers and the initial value will be Integer.MAX_VALUE. Traverse the array and check if the current element is smaller than the smallest element then update both first and second. If the current element is between first and second then only update the seco

Find Second Largest Element in Array - The Coding Shala

Home >> Programming >> Find Second Largest Element  Hey there, welcome back to another post. In this post, we will learn how to find the second largest element in the array in Java. Find Second Largest Element in Array Find the second largest element in the given array if exist else return -1. Example 1:  Input: [1, 6, 2, 7, 10, 8, -11] Output: 8 By using the below methods we can find the second largest element in the array. Find Second Largest Element Java Solution Using Single Iteration We can find the largest and second-largest elements in the array by using a single iteration. The approach is below. Approach / Explanation If the given array size if less than 2 then there is no second largest element so return -1. Create two variables first and second to store the largest and second-largest elements and the initial value is Integer.MIN_VALUE. Traverse the array and check if the current element is greater than the largest element then update both the first an