├── BitManipulation ├── BinaryToInteger.java └── IntegerToBinary.java ├── BinarySearch └── BS1Darray │ ├── BinarySearch.java │ ├── SearchInsertPosition.java │ └── SearchInRotatedArray.java ├── PinGame ├── Pin1.java └── Question.txt ├── Basic Maths ├── CheckForPrime.java ├── HCF.java ├── CheckPalindrome.java ├── CountDigits.java ├── ArmstrongNumbers.java ├── PrintAllDivisors.java └── ReverseNumber.java ├── Sorting └── MergeSort.java └── BackTracking ├── Nqueens.java └── Nknights.java /BitManipulation/BinaryToInteger.java: -------------------------------------------------------------------------------- 1 | package BitManipulation; 2 | public class BinaryToInteger { 3 | public static void main(String[] args) { 4 | String binaryString = "1101"; // Example binary string 5 | int decimalValue = binaryToInteger(binaryString); 6 | System.out.println("The decimal value of binary " + binaryString + " is: " + decimalValue); 7 | } 8 | 9 | static int binaryToInteger(String binaryString) { 10 | int decimalValue = 0; 11 | int base = 1; // 2^0 12 | 13 | for (int i = binaryString.length() - 1; i >= 0; i--) { 14 | char bit = binaryString.charAt(i); 15 | if (bit == '1') { 16 | decimalValue += base; 17 | } 18 | base *= 2; 19 | } 20 | return decimalValue; 21 | } 22 | // Alternative method using Integer.parseInt 23 | // static int binaryToInteger(String binaryString) { 24 | // return Integer.parseInt(binaryString, 2); 25 | // } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /BinarySearch/BS1Darray/BinarySearch.java: -------------------------------------------------------------------------------- 1 | package BinarySearch.BS1Darray; 2 | 3 | public class BinarySearch { 4 | public static void main(String[] args) { 5 | int[] arr = {1, 2, 3, 4, 5}; 6 | int target = 3; 7 | int result = binarySearch(arr, target); 8 | if (result != -1) { 9 | System.out.println("Element found at index: " + result); 10 | } else { 11 | System.out.println("Element not found"); 12 | } 13 | } 14 | 15 | public static int binarySearch(int[] arr, int target) { 16 | int left = 0; 17 | int right = arr.length - 1; 18 | while (left <= right) { 19 | int mid = left + (right - left) / 2; 20 | if (arr[mid] == target) { 21 | return mid; 22 | } else if (arr[mid] < target) { 23 | left = mid + 1; 24 | } else { 25 | right = mid - 1; 26 | } 27 | } 28 | return -1; // Target not found 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /PinGame/Pin1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Pin1 { 3 | 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int num1 = 3521; //sc.nextInt(); 7 | int num2 = 2452; //sc.nextInt(); 8 | int num3 = 1352; //sc.nextInt(); 9 | int num4 = 38; //sc.nextInt(); 10 | 11 | int ans = max(num1)*min(num1)*max(num2)*min(num2)*max(num3)*min(num3)- num4; 12 | System.out.print(ans); 13 | sc.close(); 14 | 15 | } 16 | 17 | static int max(int num){ 18 | int max = 0; 19 | while(num!=0){ 20 | if(maxnum%10){ 32 | min = num%10; 33 | } 34 | num/=10; 35 | } 36 | return min; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Basic Maths/CheckForPrime.java: -------------------------------------------------------------------------------- 1 | 2 | public class CheckForPrime { 3 | 4 | static boolean checkPrime(int n){ 5 | int cnt = 0; 6 | for(int i = 1; i <= Math.sqrt(n); i++){ 7 | if(n % i == 0){ 8 | cnt = cnt + 1; 9 | if(n / i != i){ 10 | cnt = cnt + 1; 11 | } 12 | } 13 | } 14 | if(cnt == 2){ 15 | return true; 16 | } 17 | else{ 18 | return false; 19 | } 20 | } 21 | 22 | public static void main(String[] args) { 23 | int n = 1483; 24 | boolean isPrime = checkPrime(n); 25 | if(isPrime){ 26 | System.out.println(n + " is a prime number."); 27 | } else { 28 | System.out.println(n + " is not a prime number."); 29 | } 30 | } 31 | } 32 | 33 | // Output: 34 | // 1483 is a prime number. 35 | // Time Complexity: O(sqrt(n)), where n is the input number 36 | // Space Complexity: O(1) as it uses a constant amount of space -------------------------------------------------------------------------------- /BinarySearch/BS1Darray/SearchInsertPosition.java: -------------------------------------------------------------------------------- 1 | package BinarySearch.BS1Darray; 2 | 3 | // SearchInsertPosition.java 4 | public class SearchInsertPosition { 5 | public static void main(String[] args) { 6 | int[] arr = {1, 3, 5, 6}; 7 | int target = 5; 8 | int result = searchInsert(arr, target); 9 | System.out.println("Insert position: " + result); 10 | } 11 | 12 | public static int searchInsert(int[] arr, int target) { 13 | int left = 0; 14 | int right = arr.length - 1; 15 | while (left <= right) { 16 | int mid = left + (right - left) / 2; 17 | if (arr[mid] == target) { 18 | return mid; 19 | } else if (arr[mid] < target) { 20 | left = mid + 1; 21 | } else { 22 | right = mid - 1; 23 | } 24 | } 25 | return left; // Insert position 26 | } 27 | } 28 | 29 | // time complexity: O(log n) 30 | // space complexity: O(1) 31 | 32 | // leetcode problem: https://leetcode.com/problems/search-insert-position/ 33 | -------------------------------------------------------------------------------- /Basic Maths/HCF.java: -------------------------------------------------------------------------------- 1 | public class HCF { 2 | // Method to calculate the HCF (Highest Common Factor) of two numbers using the Euclidean algorithm 3 | static int hcf(int a, int b) { 4 | while (b != 0) { 5 | int temp = b; 6 | b = a % b; // Remainder 7 | a = temp; // Update a to the previous b 8 | } 9 | return a; // The HCF is stored in a 10 | } 11 | 12 | public static void main(String[] args) { 13 | int num1 = 48; 14 | int num2 = 18; 15 | System.out.println("HCF of " + num1 + " and " + num2 + " is: " + hcf(num1, num2)); 16 | } 17 | } 18 | 19 | //Euclidean algorithm is an efficient method for computing the HCF of two integers. 20 | // It works by repeatedly replacing the larger number with the remainder of the division of the two numbers until one of them becomes zero. 21 | // output: 22 | // HCF of 48 and 18 is: 6 23 | // Time Complexity: O(log(min(a, b))) where a and b are the two numbers 24 | // Space Complexity: O(1) as it uses a constant amount of space 25 | 26 | // Leetcode Problem: https://leetcode.com/problems/greatest-common-divisor-of-strings/ -------------------------------------------------------------------------------- /Basic Maths/CheckPalindrome.java: -------------------------------------------------------------------------------- 1 | public class CheckPalindrome { 2 | 3 | // Method to check if a number is a palindrome using arithmetic operations 4 | static boolean isPalindrome1(int n) { 5 | int original = n; 6 | int reversed = 0; 7 | while (n > 0) { 8 | int digit = n % 10; // Get the last digit 9 | reversed = reversed * 10 + digit; // Append it to the reversed number 10 | n /= 10; // Remove the last digit from n 11 | } 12 | return original == reversed; // Check if the original number is equal to the reversed number 13 | } 14 | 15 | public static void main(String[] args) { 16 | int N = 12321; 17 | System.out.println("Number: " + N); 18 | boolean isPalindrome = isPalindrome1(N); 19 | System.out.println("Is Palindrome: " + isPalindrome); 20 | } 21 | } 22 | 23 | // 24 | // Output: 25 | // Number: 12321 26 | // Is Palindrome: true 27 | // Time Complexity: O(d) where d is the number of digits in the number 28 | // Space Complexity: O(1) as it uses a constant amount of space 29 | 30 | // Leetcode Problem: https://leetcode.com/problems/palindrome-number/ -------------------------------------------------------------------------------- /Basic Maths/CountDigits.java: -------------------------------------------------------------------------------- 1 | // This code demonstrates two methods to count the number of digits in a given integer N. 2 | // The first method uses logarithmic properties, while the second method uses a loop to divide the number by 10 until it reaches zero. 3 | // Count the number of digits in a number using two different methods 4 | 5 | public class CountDigits { 6 | 7 | static int countDigits1(int n) { 8 | int cnt = (int) (Math.log10(n) + 1); 9 | return cnt; 10 | } 11 | 12 | static int countDigits2(int n) { 13 | int cnt = 0; 14 | while (n > 0) { 15 | n /= 10; 16 | cnt++; 17 | } 18 | return cnt; 19 | } 20 | 21 | public static void main(String[] args) { 22 | int N = 329823; 23 | System.out.println("N: " + N); 24 | int digits1 = countDigits1(N); 25 | int digits2 = countDigits2(N); 26 | System.out.println("Number of Digits in N: " + digits1 + " and " + digits2); 27 | } 28 | } 29 | // Output: 30 | // N: 329823 31 | // Number of Digits in N: 6 and 6 32 | // Time Complexity: O(log N) for countDigits1, O(d) for countDigits2 where d is the number of digits -------------------------------------------------------------------------------- /Basic Maths/ArmstrongNumbers.java: -------------------------------------------------------------------------------- 1 | public class ArmstrongNumbers { 2 | 3 | // Method to check if a number is an Armstrong number 4 | static boolean isArmstrong(int n) { 5 | int original = n; 6 | int sum = 0; 7 | int digits = String.valueOf(n).length(); // Count the number of digits 8 | 9 | while (n > 0) { 10 | int digit = n % 10; // Get the last digit 11 | sum += Math.pow(digit, digits); // Raise it to the power of the number of digits and add to sum 12 | n /= 10; // Remove the last digit from n 13 | } 14 | 15 | return original == sum; // Check if the original number is equal to the sum 16 | } 17 | 18 | public static void main(String[] args) { 19 | int N = 153; // Example Armstrong number 20 | System.out.println("Number: " + N); 21 | boolean isArmstrong = isArmstrong(N); 22 | System.out.println("Is Armstrong: " + isArmstrong); 23 | } 24 | } 25 | 26 | // 27 | // Output: 28 | // Number: 153 29 | // Is Armstrong: true 30 | // Time Complexity: O(d) where d is the number of digits in the number 31 | // Space Complexity: O(1) as it uses a constant amount of space 32 | -------------------------------------------------------------------------------- /Basic Maths/PrintAllDivisors.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | 4 | public class PrintAllDivisors { 5 | public static ArrayList findDivisors(int n) { 6 | 7 | ArrayList divisors = new ArrayList<>(); 8 | int sqrtN = (int) Math.sqrt(n); 9 | 10 | for (int i = 1; i <= sqrtN; ++i) { 11 | // Check if i divides n 12 | // without leaving a remainder 13 | if (n % i == 0) { 14 | divisors.add(i); 15 | if (i != n / i) { 16 | divisors.add(n / i); 17 | } 18 | } 19 | } 20 | return divisors; 21 | } 22 | 23 | public static void main(String[] args) { 24 | int number = 12; 25 | ArrayList divisors = findDivisors(number); 26 | 27 | System.out.print("Divisors of " + number + " are: "); 28 | for (int divisor : divisors) { 29 | System.out.print(divisor + " "); 30 | } 31 | System.out.println(); 32 | } 33 | } 34 | 35 | // Output: 36 | // Divisors of 12 are: 1 12 2 6 3 4 37 | // Time Complexity: O(sqrt(n)), where n is the input number 38 | // Space Complexity: O(d), where d is the number of divisors found 39 | -------------------------------------------------------------------------------- /BinarySearch/BS1Darray/SearchInRotatedArray.java: -------------------------------------------------------------------------------- 1 | package BinarySearch.BS1Darray; 2 | 3 | public class SearchInRotatedArray { 4 | public static void main(String[] args) { 5 | int[] arr = {4, 5, 6, 7, 0, 1, 2}; 6 | int target = 0; 7 | int result = searchInRotatedArray(arr, target); 8 | System.out.println("Target found at index: "+result); 9 | } 10 | public static int searchInRotatedArray(int[] arr, int target) { 11 | int left = 0; 12 | int right = arr.length - 1; 13 | 14 | while (left <= right) { 15 | int mid = left + (right - left) / 2; 16 | 17 | if (arr[mid] == target) { 18 | return mid; 19 | } 20 | 21 | // Check if the left half is sorted 22 | if (arr[left] <= arr[mid]) { 23 | if (arr[left] <= target && target < arr[mid]) { 24 | right = mid - 1; // Target is in the left half 25 | } else { 26 | left = mid + 1; // Target is in the right half 27 | } 28 | } else { // Right half is sorted 29 | if (arr[mid] < target && target <= arr[right]) { 30 | left = mid + 1; // Target is in the right half 31 | } else { 32 | right = mid - 1; // Target is in the left half 33 | } 34 | } 35 | } 36 | return -1; // Target not found 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Basic Maths/ReverseNumber.java: -------------------------------------------------------------------------------- 1 | public class ReverseNumber { 2 | 3 | // Method to reverse a number using arithmetic operations 4 | static int reverse2(int n) { 5 | int reversed = 0; 6 | while (n > 0) { 7 | int digit = n % 10; // Get the last digit 8 | reversed = reversed * 10 + digit; // Append it to the reversed number 9 | n /= 10; // Remove the last digit from n 10 | } 11 | return reversed; 12 | } 13 | 14 | // Method to reverse a number using long to handle overflow and Integer limits 15 | static int reverse1(int x) { 16 | long sum=0; 17 | while(x!=0) 18 | { 19 | sum= sum*10 + (x%10); 20 | x=x/10; 21 | } 22 | if(sum > Integer.MAX_VALUE || sum= 0; i--) { 11 | // long pow = power(2, i); 12 | // if (number >= pow) { 13 | // System.out.print("1"); 14 | // number -= pow; 15 | // f=1; 16 | // } else if(f==1) { 17 | // System.out.print("0"); 18 | // } 19 | 20 | // } 21 | // scanner.close(); 22 | // } 23 | 24 | // static long power(int base, int exp) { 25 | // long result = 1; 26 | // for (int i = 0; i < exp; i++) { 27 | // result *= base; 28 | // } 29 | // return result; 30 | // } 31 | 32 | public static void main(String[] args) { 33 | Scanner scanner = new Scanner(System.in); 34 | System.out.print("Enter an integer: "); 35 | int number = scanner.nextInt(); 36 | StringBuilder binary = new StringBuilder(); 37 | int flag = 0; // Flag to track if we have started printing bits 38 | 39 | for (int i = 31; i >= 0; i--) { 40 | int bit = (number >> i) & 1; 41 | if(bit == 1) { 42 | flag = 1; // We have started printing bits 43 | } 44 | if(flag == 1) { 45 | binary.append(bit); 46 | } 47 | } 48 | System.out.println("Binary representation: " + binary.toString()); 49 | scanner.close(); 50 | } 51 | } -------------------------------------------------------------------------------- /Sorting/MergeSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | /** 3 | * Merge Sort is a divide-and-conquer algorithm that sorts an array by recursively 4 | * dividing it into halves, sorting each half, and then merging the sorted halves. 5 | * It has a time complexity of O(n log n) and is stable. 6 | */ 7 | 8 | class MergeSort { 9 | 10 | public static void main(String[] args) { 11 | int[] arr = {38, 27, 43, 3, 9, 82, 10}; 12 | System.out.println("Original Array:"); 13 | System.out.println(Arrays.toString(arr)); 14 | mergeSort(arr, 0, arr.length); 15 | 16 | System.out.println("Sorted Array:"); 17 | System.out.println(Arrays.toString(arr)); 18 | } 19 | 20 | static void mergeSort(int[] arr, int left, int right) { 21 | 22 | if (right-left==1) return; 23 | 24 | int mid = left + (right - left) / 2; // Find the middle point 25 | mergeSort(arr, left, mid); // Sort first half 26 | mergeSort(arr, mid, right); // Sort second half 27 | merge(arr, left, mid, right); // Merge the sorted halves 28 | 29 | } 30 | 31 | static void merge(int[] arr, int left, int mid, int right) { 32 | // Find sizes of two subarrays to be merged 33 | int[] mix = new int[right - left]; 34 | int i = left, j = mid; 35 | int k = 0; 36 | 37 | while (i < mid && j < right) { 38 | if (arr[i] <= arr[j]) { 39 | mix[k] = arr[i]; 40 | i++; 41 | } else { 42 | mix[k] = arr[j]; 43 | j++; 44 | } 45 | k++; 46 | } 47 | while (i < mid) { 48 | mix[k] = arr[i]; 49 | i++; 50 | k++; 51 | } 52 | while (j < right) { 53 | mix[k] = arr[j]; 54 | j++; 55 | k++; 56 | } 57 | for(int l = 0; l < mix.length; l++) { 58 | arr[left + l] = mix[l]; // Copy the sorted elements back to the original array 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /BackTracking/Nqueens.java: -------------------------------------------------------------------------------- 1 | package BackTracking; 2 | 3 | public class Nqueens { 4 | 5 | public static void main(String[] args) { 6 | int n = 4; 7 | boolean[][] board = new boolean[n][n]; 8 | System.out.println("No of Possibilities : "+ nQueens(board, 0)); 9 | } 10 | 11 | // recursive aporoach.. 12 | static int nQueens(boolean[][] board,int row){ 13 | 14 | if(row == board.length){ 15 | display(board); 16 | System.out.println(); 17 | return 1; 18 | } 19 | 20 | int count =0; 21 | for(int col = 0;col= 0 && row < board.length && col >=0 && col=1000 and <=9999. i.e. 5 | 1000<=input1<=9999 6 | 1000<=input2<=9999 7 | 1000<=input3<=9999 8 | 9 | input4 is a positive integer number. 10 | 11 | PIN=((MAX digit of input1 X MIN digit of input1) X (MAX digit of input2 X MIN digit of input2) X (MAX digit of input3 X MIN digit of input3)) - input 4. 12 | 13 | Example - If input1=3521, input2=2452, input3=1352, and input4=38, 14 | then PIN=((5x1)x(5x2)x(5x1))-38 = (5x10x5) - 38 = 212 15 | 16 | Assuming that the 4 numbers are passed to the given function, complete the given function to find and return the PIN. 17 | 18 | ___________________________________________________________________________ 19 | 20 | 2. Find PIN:(Correct) 21 | You are at Level-1 of a Maths Game. 22 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 23 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 24 | 1000<=input1<=9999 25 | 1000<=input2<=9999 26 | 1000<=input3<=9999 27 | 28 | input4 is a positive integer number. 29 | 30 | If input4 is EVEN, the PIN = (sum of EVEN POSITIONED digits of input1 input2 input3) - (sum of ODD POSITIONED digits of input1 and input2 and input3) 31 | If input4 is ODD, the PIN = (sum of ODD POSITIONED digits of input1 input2 input3) - (sum of EVEN POSITIONED digits of input1 and input2 and input3) 32 | 33 | Example1 - If input4=38(an EVEN number) and if input1=3521, input2=2452, input3=1352, then PIN =(3+2+2+5+1+5)-(5+1+4+2+3)=1 34 | 35 | Example2 - If input4 =37 (an ODD number) and if input1=3521, input2=2452, input3=1352, then PIN = (5+1+4+2+2) - 36 | (3+2+2+5+1+5) = 14 - 18 = -4 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 3. Find PIN:(Corrected) 47 | You are at Level-1 of a Maths Game. 48 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 49 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 50 | 1000<=input1<=9999 51 | 1000<=input2<=9999 52 | 1000<=input3<=9999 53 | 54 | input4 is a positive integer number. 55 | 56 | PIN = ((smallest digit in input1) x (LARGEST digit in input2) x (smallest digit in input3)) - input4 57 | 58 | Example - If input1=3521, input2=2452, input3=1352, and input4=38, then PIN=(1x5x1)-38=-33 59 | 60 | Assuming that the 4 numbers are passed to the given function, Complete the function to find and return the PIN. 61 | 62 | ___________________________________________________________________________ 63 | 64 | 65 | 66 | 4. Find PIN:(Correct) 67 | You are at Level-1 of a Maths Game. 68 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 69 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 70 | 1000<=input1<=9999 71 | 1000<=input2<=9999 72 | 1000<=input3<=9999 73 | 74 | input4 is a positive integer number. 75 | 76 | PIN=((MAX digit of input1 X MIN digit of input1) + (MAX digit of input2 X MIN digit of input2) + (MAX digit of input3 X MIN digit of input3)) - input 4. 77 | 78 | Example - If input1=3521, input2=2452, input3=1352, and input4=38, 79 | then PIN=((5x1)+(5x2)+(5x1))-38 = (20 - 38) = -18. 80 | 81 | Assuming that the 4 numbers are passed to the given function, complete the given function to find and return the PIN. 82 | 83 | 5. Find PIN:(Corrected) 84 | You are at Level-1 of a Maths Game. 85 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 86 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 87 | 1000<=input1<=9999 88 | 1000<=input2<=9999 89 | 1000<=input3<=9999 90 | 91 | input4 is a positive integer number. 92 | 93 | If input4 is EVEN, the PIN = (sum of even digits in input1, input2 and input3) 94 | If input4 is ODD, the PIN = (sum of odd digits in input1, input2 and input3) 95 | 96 | Example1 - If input4=38(an EVEN number) and if input1=3521. 97 | input2= 2452, input3=1352, then PIN=(2+2+4+2+2)=12 98 | 99 | Example2 - If input4=37(an ODD number) and if input1=3521. 100 | input2=2452, input3=1352, then PIN=(3+5+1+5+1+3+5)=23 101 | 102 | ___________________________________________________________________________ 103 | 104 | 105 | 6. Find PIN:(Corrected) 106 | You are at Level-1 of a Maths Game. 107 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 108 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 109 | 1000<=input1<=9999 110 | 1000<=input2<=9999 111 | 1000<=input3<=9999 112 | 113 | input4 is a positive integer number. 114 | 115 | PIN=((smallest digit in input1)x(Largest digit in input2)x(Largest digit in input3))+input4 116 | 117 | Example - If input1=3521, input2=2452, input3=1352, and input4=38. then PIN=(1x5x5)+38=63 118 | 119 | Assuming that the 4 numbers are passed to the given function. Complete the function to find and return the PIN. 120 | 121 | 122 | 123 | 124 | 125 | 126 | 7. Find PIN:(Corrected) 127 | You are at Level-1 of a Maths Game. 128 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 129 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 130 | 1000<=input1<=9999 131 | 1000<=input2<=9999 132 | 1000<=input3<=9999 133 | 134 | input4 is a positive integer number. 135 | 136 | PIN=((smallest digit in input1)x(smallest digit in input2)x(smallest digit in input3))+input4 137 | 138 | Example - If input1=3521, input2=2452, input3=1352, and input4=38. then PIN=(1x2x1)+38=40 139 | 140 | Assuming that the 4 numbers are passed to the given function. Complete the function to find and return the PIN. 141 | 142 | 143 | 144 | ___________________________________________________________________________ 145 | 146 | 147 | 148 | 8. Find PIN:(Corrected) 149 | You are at Level-1 of a Maths Game. 150 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 151 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 152 | 1000<=input1<=9999 153 | 1000<=input2<=9999 154 | 1000<=input3<=9999 155 | 156 | input4 is a positive integer number. 157 | 158 | PIN=((largest digit in input1)x(largest digit in input2)x(largest digit in input3))+input4 159 | 160 | Example - If input1=3521, input2=2452, input3=1352, and input4=38. then PIN=(5x5x5)+38=163 161 | 162 | Assuming that the 4 numbers are passed to the given function. Complete the function to find and return the PIN. 163 | 164 | 165 | 166 | 167 | 168 | 169 | 9. Create PIN using three given input numbers(Corrected) 170 | 171 | "Secure Assets Private Ltd", a small company that deals with lockers has recently started manufacturing digital locks which can be locked and unlocked using PINs (Passwords). 172 | You have been asked to work onn the module that is expected to generate PINs using three input numbers. 173 | Assumptions: The three given input numbers will always consist of four digits each i.e. each of them will be in the range >=1000 and <=9999 174 | 1000<=input1<=9999 175 | 1000<=input2<=9999 176 | 1000<=input3<=9999 177 | 178 | Below are the rules for generating the PIN- 179 | 1. The PIN should made up of 4 digits. 180 | 2. The THOUSAND position of the PIN should be the digit with maximum frequency i.e. the digit that occurs the most number of times across the three input numbers. if there are more than one digit that occur with max frequency, choose the largest of them. 181 | 3. The HUNDRED position of the PIN should be the digit with minimum frequency 182 | i.e. the digit that occurs the least number of times across the three input numbers. if there are more than one digit that occur with min frequency, choose the smallest of them. 183 | 4. The TENS position of the PIN should be the largest digit across the three input numbers. 184 | 5. The UNIT(ONES) position of the PIN should be the smallest digit across the three input numbers. 185 | 186 | Example1- 187 | input1=1724 188 | input2=5283 189 | input3=1937 190 | then, PIN=7491 191 | Explanation: Below is an expression of how the PIN has been formed in this example. 192 | 1. There are four digits '1','2','3' and '7' that occur two times each. The largest out of them is '7', so 7 occupies the thousands position of the pin. 193 | 2. There are four digits '4','5','8' and '9' that occur once each. The smallest out of them is '4', so 4 occupies the hundreds position of the pin. 194 | 3. The largest digit across the three input numbers is '9', so 9 occupies the tens position of the pin. 195 | 4. The smallest digit across the three input numbers is '1', so 1 occupies the units position of the pin. 196 | 197 | Example2- 198 | input1=8530 199 | input2=5620 200 | input3=7532 201 | then, PIN=5680 202 | 203 | 204 | 205 | 206 | 207 | 208 | 10. Create PIN using three given input numbers 209 | 210 | "Secure Assets Private Ltd", a small company that deals with lockers has recently started manufacturing digital locks which can be locked and unlocked using PINs (Passwords). 211 | You have been asked to work onn the module that is expected to generate PINs using three input numbers. 212 | Assumptions: The three given input numbers will always consist of four digits each i.e. each of them will be in the range >=1000 and <=9999 213 | 1000<=input1<=9999 214 | 1000<=input2<=9999 215 | 1000<=input3<=9999 216 | 217 | Below are the rules for generating the PIN- 218 | 1. The PIN should made up of 4 digits. 219 | 2. The THOUSAND position of the PIN should be the smallest digit across the three input numbers. 220 | 3. The HUNDRED position of the PIN should be the largest digit across the three input numbers. 221 | 4. The TENS position of the PIN should be the digit with minimum frequency i.e. the digit that occurs the least number of times across the three input numbers. If there are more than one digit that occur with min frequency. 222 | choose the smallest of them. 223 | 5. The ONES position of the PIN should be the digit with maximum frequency i.e. the digit that occurs the most number of times across three input numbers. If there are more than one digit that occur with frequency, choose the largest of them. 224 | 225 | Example1- 226 | input1=1724 227 | input2=5283 228 | input3=1937 229 | then, PIN=1947 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 11. Find PIN: 249 | You are at Level-1 of a Maths Game. 250 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 251 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 252 | 1000<=input1<=9999 253 | 1000<=input2<=9999 254 | 1000<=input3<=9999 255 | 256 | input4 is a positive integer number. 257 | 258 | if input4 is EVEN, the PIN =(sum of even digits in input1, input2 and input3)-(sum of odd digits in input1,input2 and input3) 259 | if input4 is ODD, the PIN=(sum of odd digits in input1, input2 and input3)-(sum of even digits in input1, input2 and input3) 260 | 261 | Example1- 262 | if input4=38(an EVEN number) and if input1=3521, input2=2452, input3=1352. then PIN=(2+2+4+2+2)-(3+5+1+5+1+3+5)=-11 263 | Example2- 264 | if input4=37(an ODD number) and if input1=3521,input2=2452, input3=1352, then PIN=(3+5+1+5+1+3+5)-(2+2+4+2+2)=11 265 | 266 | Assuming that the 4 numbers are passed to the given function, complete the function to find and return the PIN. 267 | 268 | 269 | Previous year question for DOT NET and JAVA Logical Building1. Find PIN:(Correct) 270 | You are at Level-1 of a Maths Game. 271 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 272 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 273 | 1000<=input1<=9999 274 | 1000<=input2<=9999 275 | 1000<=input3<=9999 276 | 277 | input4 is a positive integer number. 278 | 279 | PIN=((MAX digit of input1 X MIN digit of input1) X (MAX digit of input2 X MIN digit of input2) X (MAX digit of input3 X MIN digit of input3)) - input 4. 280 | 281 | Example - If input1=3521, input2=2452, input3=1352, and input4=38, 282 | then PIN=((5x1)x(5x2)x(5x1))-38 = (5x10x5) - 38 = 212 283 | 284 | Assuming that the 4 numbers are passed to the given function, complete the given function to find and return the PIN. 285 | 286 | ___________________________________________________________________________ 287 | 288 | 2. Find PIN:(Correct) 289 | You are at Level-1 of a Maths Game. 290 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 291 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 292 | 1000<=input1<=9999 293 | 1000<=input2<=9999 294 | 1000<=input3<=9999 295 | 296 | input4 is a positive integer number. 297 | 298 | If input4 is EVEN, the PIN = (sum of EVEN POSITIONED digits of input1 input2 input3) - (sum of ODD POSITIONED digits of input1 and input2 and input3) 299 | If input4 is ODD, the PIN = (sum of ODD POSITIONED digits of input1 input2 input3) - (sum of EVEN POSITIONED digits of input1 and input2 and input3) 300 | 301 | Example1 - If input4=38(an EVEN number) and if input1=3521, input2=2452, input3=1352, then PIN =(3+2+2+5+1+5)-(5+1+4+2+3)=1 302 | 303 | Example2 - If input4 =37 (an ODD number) and if input1=3521, input2=2452, input3=1352, then PIN = (5+1+4+2+2) - 304 | (3+2+2+5+1+5) = 14 - 18 = -4 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 3. Find PIN:(Corrected) 315 | You are at Level-1 of a Maths Game. 316 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 317 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 318 | 1000<=input1<=9999 319 | 1000<=input2<=9999 320 | 1000<=input3<=9999 321 | 322 | input4 is a positive integer number. 323 | 324 | PIN = ((smallest digit in input1) x (LARGEST digit in input2) x (smallest digit in input3)) - input4 325 | 326 | Example - If input1=3521, input2=2452, input3=1352, and input4=38, then PIN=(1x5x1)-38=-33 327 | 328 | Assuming that the 4 numbers are passed to the given function, Complete the function to find and return the PIN. 329 | 330 | ___________________________________________________________________________ 331 | 332 | 333 | 334 | 4. Find PIN:(Correct) 335 | You are at Level-1 of a Maths Game. 336 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 337 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 338 | 1000<=input1<=9999 339 | 1000<=input2<=9999 340 | 1000<=input3<=9999 341 | 342 | input4 is a positive integer number. 343 | 344 | PIN=((MAX digit of input1 X MIN digit of input1) + (MAX digit of input2 X MIN digit of input2) + (MAX digit of input3 X MIN digit of input3)) - input 4. 345 | 346 | Example - If input1=3521, input2=2452, input3=1352, and input4=38, 347 | then PIN=((5x1)+(5x2)+(5x1))-38 = (20 - 38) = -18. 348 | 349 | Assuming that the 4 numbers are passed to the given function, complete the given function to find and return the PIN. 350 | 351 | 5. Find PIN:(Corrected) 352 | You are at Level-1 of a Maths Game. 353 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 354 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 355 | 1000<=input1<=9999 356 | 1000<=input2<=9999 357 | 1000<=input3<=9999 358 | 359 | input4 is a positive integer number. 360 | 361 | If input4 is EVEN, the PIN = (sum of even digits in input1, input2 and input3) 362 | If input4 is ODD, the PIN = (sum of odd digits in input1, input2 and input3) 363 | 364 | Example1 - If input4=38(an EVEN number) and if input1=3521. 365 | input2= 2452, input3=1352, then PIN=(2+2+4+2+2)=12 366 | 367 | Example2 - If input4=37(an ODD number) and if input1=3521. 368 | input2=2452, input3=1352, then PIN=(3+5+1+5+1+3+5)=23 369 | 370 | ___________________________________________________________________________ 371 | 372 | 373 | 6. Find PIN:(Corrected) 374 | You are at Level-1 of a Maths Game. 375 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 376 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 377 | 1000<=input1<=9999 378 | 1000<=input2<=9999 379 | 1000<=input3<=9999 380 | 381 | input4 is a positive integer number. 382 | 383 | PIN=((smallest digit in input1)x(Largest digit in input2)x(Largest digit in input3))+input4 384 | 385 | Example - If input1=3521, input2=2452, input3=1352, and input4=38. then PIN=(1x5x5)+38=63 386 | 387 | Assuming that the 4 numbers are passed to the given function. Complete the function to find and return the PIN. 388 | 389 | 390 | 391 | 392 | 393 | 394 | 7. Find PIN:(Corrected) 395 | You are at Level-1 of a Maths Game. 396 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 397 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 398 | 1000<=input1<=9999 399 | 1000<=input2<=9999 400 | 1000<=input3<=9999 401 | 402 | input4 is a positive integer number. 403 | 404 | PIN=((smallest digit in input1)x(smallest digit in input2)x(smallest digit in input3))+input4 405 | 406 | Example - If input1=3521, input2=2452, input3=1352, and input4=38. then PIN=(1x2x1)+38=40 407 | 408 | Assuming that the 4 numbers are passed to the given function. Complete the function to find and return the PIN. 409 | 410 | 411 | 412 | ___________________________________________________________________________ 413 | 414 | 415 | 416 | 8. Find PIN:(Corrected) 417 | You are at Level-1 of a Maths Game. 418 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 419 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 420 | 1000<=input1<=9999 421 | 1000<=input2<=9999 422 | 1000<=input3<=9999 423 | 424 | input4 is a positive integer number. 425 | 426 | PIN=((largest digit in input1)x(largest digit in input2)x(largest digit in input3))+input4 427 | 428 | Example - If input1=3521, input2=2452, input3=1352, and input4=38. then PIN=(5x5x5)+38=163 429 | 430 | Assuming that the 4 numbers are passed to the given function. Complete the function to find and return the PIN. 431 | 432 | 433 | 434 | 435 | 436 | 437 | 9. Create PIN using three given input numbers(Corrected) 438 | 439 | "Secure Assets Private Ltd", a small company that deals with lockers has recently started manufacturing digital locks which can be locked and unlocked using PINs (Passwords). 440 | You have been asked to work onn the module that is expected to generate PINs using three input numbers. 441 | Assumptions: The three given input numbers will always consist of four digits each i.e. each of them will be in the range >=1000 and <=9999 442 | 1000<=input1<=9999 443 | 1000<=input2<=9999 444 | 1000<=input3<=9999 445 | 446 | Below are the rules for generating the PIN- 447 | 1. The PIN should made up of 4 digits. 448 | 2. The THOUSAND position of the PIN should be the digit with maximum frequency i.e. the digit that occurs the most number of times across the three input numbers. if there are more than one digit that occur with max frequency, choose the largest of them. 449 | 3. The HUNDRED position of the PIN should be the digit with minimum frequency 450 | i.e. the digit that occurs the least number of times across the three input numbers. if there are more than one digit that occur with min frequency, choose the smallest of them. 451 | 4. The TENS position of the PIN should be the largest digit across the three input numbers. 452 | 5. The UNIT(ONES) position of the PIN should be the smallest digit across the three input numbers. 453 | 454 | Example1- 455 | input1=1724 456 | input2=5283 457 | input3=1937 458 | then, PIN=7491 459 | Explanation: Below is an expression of how the PIN has been formed in this example. 460 | 1. There are four digits '1','2','3' and '7' that occur two times each. The largest out of them is '7', so 7 occupies the thousands position of the pin. 461 | 2. There are four digits '4','5','8' and '9' that occur once each. The smallest out of them is '4', so 4 occupies the hundreds position of the pin. 462 | 3. The largest digit across the three input numbers is '9', so 9 occupies the tens position of the pin. 463 | 4. The smallest digit across the three input numbers is '1', so 1 occupies the units position of the pin. 464 | 465 | Example2- 466 | input1=8530 467 | input2=5620 468 | input3=7532 469 | then, PIN=5680 470 | 471 | 472 | 473 | 474 | 475 | 476 | 10. Create PIN using three given input numbers 477 | 478 | "Secure Assets Private Ltd", a small company that deals with lockers has recently started manufacturing digital locks which can be locked and unlocked using PINs (Passwords). 479 | You have been asked to work onn the module that is expected to generate PINs using three input numbers. 480 | Assumptions: The three given input numbers will always consist of four digits each i.e. each of them will be in the range >=1000 and <=9999 481 | 1000<=input1<=9999 482 | 1000<=input2<=9999 483 | 1000<=input3<=9999 484 | 485 | Below are the rules for generating the PIN- 486 | 1. The PIN should made up of 4 digits. 487 | 2. The THOUSAND position of the PIN should be the smallest digit across the three input numbers. 488 | 3. The HUNDRED position of the PIN should be the largest digit across the three input numbers. 489 | 4. The TENS position of the PIN should be the digit with minimum frequency i.e. the digit that occurs the least number of times across the three input numbers. If there are more than one digit that occur with min frequency. 490 | choose the smallest of them. 491 | 5. The ONES position of the PIN should be the digit with maximum frequency i.e. the digit that occurs the most number of times across three input numbers. If there are more than one digit that occur with frequency, choose the largest of them. 492 | 493 | Example1- 494 | input1=1724 495 | input2=5283 496 | input3=1937 497 | then, PIN=1947 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 11. Find PIN: 517 | You are at Level-1 of a Maths Game. 518 | You are provided with 4 numbers: input1, input2, input3 and input4 and are expected to find a secret PIN. 519 | The three numbers input1, input2 and input3 are four digit numbers within the range >=1000 and <=9999. i.e. 520 | 1000<=input1<=9999 521 | 1000<=input2<=9999 522 | 1000<=input3<=9999 523 | 524 | input4 is a positive integer number. 525 | 526 | if input4 is EVEN, the PIN =(sum of even digits in input1, input2 and input3)-(sum of odd digits in input1,input2 and input3) 527 | if input4 is ODD, the PIN=(sum of odd digits in input1, input2 and input3)-(sum of even digits in input1, input2 and input3) 528 | 529 | Example1- 530 | if input4=38(an EVEN number) and if input1=3521, input2=2452, input3=1352. then PIN=(2+2+4+2+2)-(3+5+1+5+1+3+5)=-11 531 | Example2- 532 | if input4=37(an ODD number) and if input1=3521,input2=2452, input3=1352, then PIN=(3+5+1+5+1+3+5)-(2+2+4+2+2)=11 533 | 534 | Assuming that the 4 numbers are passed to the given function, complete the function to find and return the PIN. 535 | 536 | 537 | Previous year question for DOT NET and JAVA Logical Building --------------------------------------------------------------------------------