├── README.md ├── Tower_of_Hanoi.java └── Mini_Max.java /README.md: -------------------------------------------------------------------------------- 1 | # Data_Structure-in-JAVA 2 | Data_Structure-in-JAVA 3 | 4 | This Repo consists of DS-JAVA program 5 | 6 | 1. Maximum and Minimum sum among an array of given numbers(Mini-Max Sum) 7 | 8 | 2. Tower of Hanoi(Mathematical game or puzzle) problem 9 | -------------------------------------------------------------------------------- /Tower_of_Hanoi.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Tower_of_Hanoi { 4 | 5 | // Recursive function to solve the Tower of Hanoi puzzle 6 | public static void solveTowerOfHanoi(int n, char fromRod, char toRod, char auxRod) { 7 | if (n == 1) { 8 | System.out.println("Move disk 1 from rod " + fromRod + " to rod " + toRod); 9 | return; 10 | } 11 | solveTowerOfHanoi(n - 1, fromRod, auxRod, toRod); 12 | System.out.println("Move disk " + n + " from rod " + fromRod + " to rod " + toRod); 13 | solveTowerOfHanoi(n - 1, auxRod, toRod, fromRod); 14 | } 15 | 16 | // Main method 17 | public static void main(String[] args) { 18 | Scanner scanner = new Scanner(System.in); 19 | 20 | System.out.print("Enter the number of disks: "); 21 | int n = scanner.nextInt(); 22 | 23 | long startTime = System.nanoTime(); 24 | solveTowerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods 25 | long endTime = System.nanoTime(); 26 | long duration = endTime - startTime; // Execution time in nanoseconds 27 | 28 | System.out.println("Total moves: " + (int)(Math.pow(2, n) - 1)); 29 | System.out.println("Execution time: " + duration + " nanoseconds"); 30 | 31 | scanner.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Mini_Max.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class Mini_Max { 5 | 6 | public static void miniMaxSum(int[] arr) { 7 | // Calculate the start time 8 | long startTime = System.nanoTime(); 9 | 10 | // Sort the array 11 | Arrays.sort(arr); 12 | 13 | // Calculate the minimum sum (sum of the first n-1 elements) 14 | long minSum = 0; 15 | for (int i = 0; i < arr.length - 1; i++) { 16 | minSum += arr[i]; 17 | } 18 | 19 | // Calculate the maximum sum (sum of the last n-1 elements) 20 | long maxSum = 0; 21 | for (int i = 1; i < arr.length; i++) { 22 | maxSum += arr[i]; 23 | } 24 | 25 | // Calculate the end time 26 | long endTime = System.nanoTime(); 27 | 28 | // Output the result 29 | System.out.println("Min Sum: " + minSum + ", Max Sum: " + maxSum); 30 | 31 | // Calculate and output the running time 32 | long duration = endTime - startTime; 33 | System.out.println("Running time: " + duration + " nanoseconds"); 34 | } 35 | 36 | public static void main(String[] args) { 37 | Scanner scanner = new Scanner(System.in); 38 | 39 | System.out.println("Enter the number of elements in the array:"); 40 | int n = scanner.nextInt(); 41 | 42 | int[] arr = new int[n]; 43 | 44 | System.out.println("Enter the elements of the array:"); 45 | for (int i = 0; i < n; i++) { 46 | arr[i] = scanner.nextInt(); 47 | } 48 | 49 | miniMaxSum(arr); 50 | 51 | scanner.close(); 52 | } 53 | } 54 | --------------------------------------------------------------------------------