Posts

Introduction to Kotlin Programming Language for Backend Development - The Coding Shala

Home >> Learn Kotlin >> Kotlin Introduction Are you looking for a powerful and reliable programming language for backend development? Look no further than Kotlin. Kotlin is a modern, statically typed programming language that has gained a lot of popularity in recent years. In this blog post, we'll take a look at what Kotlin is and why it's a great choice for backend development. What is Kotlin? Kotlin is a statically typed, cross-platform, general-purpose programming language that was developed by JetBrains in 2011. It is designed to be more concise, expressive, and safe than Java, the most widely used programming language for Android development. Kotlin can be compiled to run on the Java Virtual Machine (JVM), JavaScript, and native platforms, making it a versatile language for different types of applications. Why use Kotlin for backend development? Kotlin has several features that make it a suitable language for backend development, such as: Concisenes

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