Skip to main content

Posts

Write a function that calculates the Greatest Common Divisor of 2 numbers.

Write a function that calculates the Greatest Common Divisor of 2 numbers. The Greatest Common Divisor (GCD) of two numbers is the largest positive integer that divides both the numbers without leaving any remainder.             Java import java . util .*;   public class Solutions {     public static void main ( String args []) {         Scanner sc = new Scanner ( System . in );         int n1 = sc . nextInt ();         int n2 = sc . nextInt ();           while ( n1 != n2 ) {              if ( n1 > n2 ) {                 n1 = n1 - n2 ;            } else {     ...

Backtracking | N Queens Problem using backtracking algorithm | Permutations | The Java Placement Course | Apna College |

Backtracking in Recursion Java                                          Backtracking is a popular algorithmic technique used in programming to find a solution to a problem by trying out different combinations of choices and undoing those choices that don't lead to a valid solution. In backtracking, the algorithm starts by making a choice and exploring a path to see if it leads to a valid solution. If the path leads to a dead end or invalid solution, the algorithm backtracks and tries another path. This process is repeated until a valid solution is found or all possible combinations have been explored. Backtracking is commonly used in solving problems such as generating all possible permutations or combinations of a given set of elements, solving Sudoku puzzles, solving the N-queens problem, and many others.                    The N Qu...