├── Accenture ├── Que 1 - Rat Count House │ ├── Main.java │ └── Problem Statement.txt ├── Que 10 │ ├── Main.java │ └── Problem Statement.txt ├── Que 11 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 12 │ ├── Main.java │ └── Problem Statement.txt ├── Que 13 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 14 │ ├── Main.java │ └── Problem Statement.txt ├── Que 15 │ ├── Main.java │ └── Problem Statement.txt ├── Que 16 │ ├── Main.java │ └── Problem Statement.txt ├── Que 17 │ ├── Main.java │ └── Problem Statement.txt ├── Que 18 │ ├── Main.java │ └── Problem Statement.txt ├── Que 19 │ ├── Main.java │ └── Problem Statement.txt ├── Que 2 │ ├── Main.java │ └── Problem Statement.txt ├── Que 20 - Autobiographical Number │ ├── Problem Statement.txt │ └── Solution.java ├── Que 3 - Password Checker │ ├── Problem Statement.txt │ └── Solution.java ├── Que 4 │ ├── Main.java │ ├── Main.py │ └── Problem Statement.txt ├── Que 5 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 6 │ ├── AddOfArrayLargeinEvenAndSmallInOdd.py │ ├── Main.java │ └── Problem Statement.txt ├── Que 7 │ ├── Main.java │ └── Problem Statement.txt ├── Que 8 │ ├── Main.java │ └── Problem Statement.txt └── Que 9 │ ├── Problem Statement.txt │ └── Solution.java ├── Cognizant ├── Que 1 │ ├── Main.java │ └── Problem Statement.txt ├── Que 10 │ ├── LuckyNum.java │ └── Problem Statement.txt ├── Que 11 │ ├── Course.java │ └── Problem Statement.txt ├── Que 12 │ ├── Main.java │ └── Problem Statement.txt ├── Que 13 │ ├── HighestMarkPerSem.java │ └── Problem Statement.txt ├── Que 14 │ ├── FindFactor.java │ └── Problem Statement.txt ├── Que 15 │ ├── Problem Statement.txt │ └── lcm.cpp ├── Que 16 │ ├── Problem Statement.txt │ └── monthSeason.c ├── Que 17 │ ├── Problem Statement.txt │ └── ticketScheme.c ├── Que 18 │ ├── Problem Statement.txt │ └── prime.cpp ├── Que 19 │ ├── Problem Statement.txt │ └── factor.cpp ├── Que 2 │ ├── Main.java │ └── Problem Statement.txt ├── Que 20 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 21 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 22 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 23 │ ├── PrepInsta.java │ └── Problem Statement.txt ├── Que 24 │ ├── Problem Statement.txt │ └── StringReduction.java ├── Que 25 │ ├── Problem Statement.txt │ └── RepresentBT.cpp ├── Que 26 │ ├── GameOfClicks.java │ └── Problem Statement.txt ├── Que 27 │ ├── Application3.java │ └── Problem Statement.txt ├── Que 28 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 29 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 3 │ ├── Main.java │ └── Problem Statement.txt ├── Que 4 │ ├── Main.java │ └── Problem Statement.txt ├── Que 5 │ ├── Main.java │ └── Problem Statement.txt ├── Que 6 │ ├── Main.java │ └── Problem Statement.txt ├── Que 7 │ ├── Main.java │ └── Problem Statement.txt ├── Que 8 │ ├── Main.java │ └── Problem Statement.txt └── Que 9 │ ├── Main.java │ └── Problem Statement.txt ├── GFG Practice Sets ├── Divide and Conquer │ ├── Counting Inversions │ │ ├── Problem Statement.txt │ │ └── Solution.java │ ├── Karatsuba Algorithm │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Painter's Partition Problem │ │ ├── GFG.java │ │ └── Problem Statement.txt │ └── Power of Numbers │ │ ├── Main.java │ │ └── Problem Statement.txt ├── Greedy Algorithms │ ├── Activity Selection │ │ ├── Problem Statement.txt │ │ └── Solution.java │ ├── Fractional Knapsack │ │ ├── Problem Statement.txt │ │ └── Solution.cpp │ ├── Largest Number with Given Sum │ │ ├── Problem Statement.txt │ │ └── Solution.java │ └── N Meetings in a Room │ │ ├── Problem Statement.txt │ │ └── Solution.cpp ├── Mathematical │ ├── Add two Fractions │ │ ├── Fraction_Addition.java │ │ └── Problem Statement.txt │ ├── Armstrong Number │ │ ├── Problem Statement.txt │ │ └── Solution.java │ ├── Binary to Decimal │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Calculate Permuation nPr │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Calculate nCr Combination │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Closest Number │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Digital Sum Palindrome │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Factorial │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── GCD of Array │ │ ├── Gfg.java │ │ └── Problem Statement.txt │ ├── GCD │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Jumping Numbers │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── LCM │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Largest Prime Factor │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Multiplication Table │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Nth root of M │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Pair Cube Count │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Pairs of Primes │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Perfect Number │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Prime Number │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Print Kth Digit │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Print the Pattern │ │ ├── PrintPattern.java │ │ └── Problem Statement.txt │ ├── Reverse the Digit │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Series Arithmetic Progression │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Series Geometric Progression │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Sieve of Eratosthenes │ │ ├── GFG.java │ │ └── Problem Statement.txt │ └── Sum of Primes from 1 to N │ │ ├── GFG.java │ │ └── Problem Statement.txt ├── Puzzles │ ├── 3 Divisors │ │ ├── Problem Statement.txt │ │ └── Solution.cpp │ ├── Angle Between Hands of the Clock │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Count Squares │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Days of the Week │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── If power of another │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Is Square │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Last 2 Digits of Fibbonacci │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Nth even Fibbonacci Number │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Open Doors │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Overlapping Rectangles │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Squares in a Matrix │ │ ├── GFG.java │ │ └── Problem Statement.txt │ ├── Trailing 0's in Factorial │ │ ├── GFG.java │ │ └── Problem Statement.txt │ └── Triangular Numbers │ │ ├── GFG.java │ │ └── Problem Statement.txt ├── Random Questions │ └── Allocate Min. Pages │ │ ├── Problem Statement.txt │ │ └── Solution.java └── Recursion │ ├── Handshakes │ ├── GFG.java │ └── Problem Statement.txt │ ├── Josephus Problem │ ├── Problem Statement.txt │ └── Recursion.java │ ├── Print Pattern │ ├── GFG.java │ └── Problem Statement.txt │ ├── Remove Recursive Duplicates │ ├── GFG.java │ └── Problem Statement.txt │ └── Tower of Hanoi │ ├── Problem Statement.txt │ └── Recursion.java ├── INTERVIEW QUESTIONS - JAVA └── README.txt ├── LICENSE ├── QUESTIONS Java Developer └── Questions.txt ├── QUESTIONS Software Engineer └── Questions.txt ├── QUESTIONS Web Developer └── Questions.txt ├── README.md ├── TATA ELXSI ├── Analytical.txt ├── Technical.txt └── Verbal.txt ├── TCS - NINJA ├── Que 1 │ ├── Prep.java │ └── Problem Statement.txt ├── Que 2 │ ├── Main.java │ └── Problem Statement.txt ├── Que 3 │ ├── Main.java │ └── Problem Statement.txt ├── Que 4 │ ├── Main.java │ └── Problem Statement.txt └── Que 5 │ ├── Demo.java │ └── Problem Statement.txt ├── TCS ├── BASIC.c ├── Que 1 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 10 │ ├── Problem Statement.txt │ └── ceasarcipher.cpp ├── Que 11 │ ├── Main.java │ └── Problem Statement.txt ├── Que 12 │ ├── Problem Statement.txt │ └── citybus.cpp ├── Que 13 │ ├── Monkeys.java │ └── Problem Statement.txt ├── Que 14 │ ├── Problem Statement.txt │ └── chainmarket.cpp ├── Que 15 │ ├── Problem Statement.txt │ └── vendingmachine.cpp ├── Que 16 │ ├── Problem Statement.txt │ └── consult.cpp ├── Que 17 │ ├── Main.java │ └── Problem Statement.txt ├── Que 18 │ ├── Main.java │ └── Problem Statement.txt ├── Que 19 │ ├── Main.java │ └── Problem Statement.txt ├── Que 2 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 20 │ ├── Main.java │ └── Problem Statement.txt ├── Que 21 │ ├── Main.java │ └── Problem Statement.txt ├── Que 22 │ ├── Main.java │ └── Problem Statement.txt ├── Que 23 │ ├── Main.java │ └── Problem Statement.txt ├── Que 24 │ ├── Problem Statement.txt │ └── series1.cpp ├── Que 3 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 4 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 5 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 6 │ ├── Problem Statement.txt │ └── Solution.java ├── Que 7 │ ├── Main.java │ └── Problem Statement.txt ├── Que 8 │ ├── Main.java │ └── Problem Statement.txt └── Que 9 │ ├── Problem Statement.txt │ └── washingmachine_weight.cpp └── Wipro Elite NLTH ├── Que 1 ├── Main.java └── Problem Statement.txt ├── Que 10 - Matrix Multiply ├── Main.java └── Problem Statement.txt ├── Que 11 - Remove Vowels ├── Problem Statement.txt └── Solution.java ├── Que 12 - Odd digit sum ├── Problem Statement.txt └── Solution.java ├── Que 13 - Transpose ├── Problem Statement.txt └── Solution.java ├── Que 14 - Largest element 2D Array ├── Problem Statement.txt └── Solution.java ├── Que 15 - Balanced Parenthesis ├── Main.java └── Problem Statement.txt ├── Que 16 ├── Main.java └── Problem Statement.txt ├── Que 17 ├── Main.java └── Problem Statement.txt ├── Que 18 ├── Main.java └── Problem Statement.txt ├── Que 19 ├── Main.java └── Problem Statement.txt ├── Que 2 ├── Main.java └── Problem Statement.txt ├── Que 20 ├── Main.java └── Problem Statement.txt ├── Que 21 ├── Main.java └── Problem Statement.txt ├── Que 22 ├── Main.java └── Problem Statement.txt ├── Que 23 ├── Main.java └── Problem Statement.txt ├── Que 24 ├── Main.java └── Problem Statement.txt ├── Que 25 ├── Main.java └── Problem Statement.txt ├── Que 26 ├── Main.java └── Problem Statement.txt ├── Que 3 ├── Main.java └── Problem Statement.txt ├── Que 4 ├── Main.java └── Problem Statement.txt ├── Que 5 - GCD ├── Problem Statement.txt └── Solution.java ├── Que 6 - GCD of 3 no ├── Problem Statement.txt └── Solution.java ├── Que 7 - Prime ├── Problem Statement.txt └── Solution.java ├── Que 8 - Distinct elements of Array ├── Problem Statement.txt └── Solution.java └── Que 9 - Sort Half ├── Problem Statement.txt └── Sort.java /Accenture/Que 1 - Rat Count House/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static int solve (int r, int unit, int arr[], int n) 5 | { 6 | if (arr == null) 7 | return -1; 8 | int res = r * unit; 9 | int sum = 0; 10 | int count = 0; 11 | for (int i = 0; i < n; i++) 12 | { 13 | sum = sum + arr[i]; 14 | count++; 15 | if (sum >= res) 16 | break; 17 | } 18 | if(sum=0 15 | 16 | Example: 17 | 18 | Input 19 | Num 1: 451 20 | Num 2: 349 21 | Output 22 | 2 23 | Explanation: 24 | 25 | Adding ‘num 1’ and ‘num 2’ right-to-left results in 2 carries since ( 1+9) is 10. 1 is carried and (5+4=1) is 10, again 1 is carried. Hence 2 is returned. 26 | 27 | Sample Input 28 | 29 | Num 1: 23 30 | 31 | Num 2: 563 32 | 33 | Sample Output 34 | 35 | 0 36 | -------------------------------------------------------------------------------- /Accenture/Que 11/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Question:11 2 | (Asked in Accenture Offcampus 1 Aug 2021, Slot 3) 3 | 4 | Problem Statement 5 | 6 | You are given a function, 7 | 8 | Void *ReplaceCharacter(Char str[], int n, char ch1, char ch2); 9 | 10 | The function accepts a string ‘ str’ of length n and two characters ‘ch1’ and ‘ch2’ as its arguments . Implement the function to modify and return the string ‘ str’ in such a way that all occurrences of ‘ch1’ in original string are replaced by ‘ch2’ and all occurrences of ‘ch2’ in original string are replaced by ‘ch1’. 11 | 12 | Assumption: String Contains only lower-case alphabetical letters. 13 | 14 | Note: 15 | 16 | Return null if string is null. 17 | If both characters are not present in string or both of them are same , then return the string unchanged. 18 | Example: 19 | 20 | Input: 21 | Str: apples 22 | ch1:a 23 | ch2:p 24 | Output: 25 | paales 26 | Explanation: 27 | 28 | ‘A’ in original string is replaced with ‘p’ and ‘p’ in original string is replaced with ‘a’, thus output is paales. 29 | -------------------------------------------------------------------------------- /Accenture/Que 11/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Solution 3 | { 4 | public static void replaceChar (String str, char ch1, char ch2) 5 | { 6 | String res = ""; 7 | 8 | for (int i = 0; i < str.length (); i++) 9 | { 10 | if (str.charAt (i) == ch1) 11 | res = res + ch2; 12 | else if (str.charAt (i) == ch2) 13 | res = res + ch1; 14 | else 15 | res = res + str.charAt (i); 16 | } 17 | System.out.println (res); 18 | } 19 | 20 | public static void main (String[]args) 21 | { 22 | Scanner sc = new Scanner (System.in); 23 | String str = sc.next (); 24 | char ch1 = sc.next ().charAt (0); 25 | char ch2 = sc.next ().charAt (0); 26 | 27 | replaceChar (str, ch1, ch2); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Accenture/Que 12/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static int count (int i) 5 | { 6 | int count = 0; 7 | 8 | while (i % 2 == 0 && i != 0) 9 | { 10 | count += 1; 11 | i = i / 2; 12 | } 13 | return count; 14 | } 15 | 16 | public static int maxExponents (int a, int b) 17 | { 18 | int max = 0, num = 0, ans; 19 | for (int i = a; i <= b; i++) 20 | { 21 | int temp = count (i); 22 | if (temp > max) 23 | { 24 | max = temp; 25 | num = i; 26 | } 27 | } 28 | return num; 29 | } 30 | 31 | public static void main (String[]args) 32 | { 33 | Scanner sc = new Scanner (System.in); 34 | int a = sc.nextInt (); 35 | int b = sc.nextInt (); 36 | 37 | System.out.println (maxExponents(a, b)); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Accenture/Que 12/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Question:13 2 | (Asked in Accenture Offcampus 2 Aug 2021, Slot 2) 3 | 4 | Problem Statement 5 | 6 | You are given a function, 7 | 8 | Int MaxExponents (int a , int b); 9 | 10 | You have to find and return the number between ‘a’ and ‘b’ ( range inclusive on both ends) which has the maximum exponent of 2. 11 | 12 | The algorithm to find the number with maximum exponent of 2 between the given range is 13 | 14 | Loop between ‘a’ and ‘b’. Let the looping variable be ‘i’. 15 | Find the exponent (power) of 2 for each ‘i’ and store the number with maximum exponent of 2 so faqrd in a variable , let say ‘max’. Set ‘max’ to ‘i’ only if ‘i’ has more exponent of 2 than ‘max’. 16 | Return ‘max’. 17 | Assumption: a max) 10 | { 11 | max = arr[i]; 12 | index = i; 13 | } 14 | 15 | System.out.println (max); 16 | 17 | System.out.println (index); 18 | } 19 | public static void main (String[]args) 20 | { 21 | Scanner sc = new Scanner (System.in); 22 | int n = sc.nextInt (); 23 | int arr[] = new int[n]; 24 | 25 | for (int i = 0; i < n; i++) 26 | arr[i] = sc.nextInt (); 27 | 28 | maxInArray (arr, arr.length); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Accenture/Que 19/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Question : 19 2 | 3 | Find the maximum value and its index in the array 4 | 5 | Problem Statement : 6 | 7 | You are given a function, void MaxInArray(int arr[], int length); The function accepts an integer array ‘arr’ of size ‘length’ as its argument. Implement the function to find the maximum element of the array and print the maximum element and its index to the standard output 8 | 9 | (STDOUT). The maximum element and its index should be printed in separate lines. 10 | 11 | Note: 12 | 13 | Array index starts with 0 14 | Maximum element and its index should be separated by a line in the output 15 | Assume there is only 1 maximum element in the array 16 | Print exactly what is asked, do not print any additional greeting messages 17 | Example: 18 | 19 | Input: 20 | 21 | 23 45 82 27 66 12 78 13 71 86 22 | 23 | Output: 24 | 25 | 86 26 | 27 | 9 28 | 29 | Explanation: 30 | 31 | 86 is the maximum element of the array at index 9. 32 | -------------------------------------------------------------------------------- /Accenture/Que 2/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static int operationsBinaryString (String str) 5 | { 6 | if(str==null) 7 | return -1; 8 | int res = str.charAt (0) - '0'; 9 | for (int i = 1; i < str.length ();) 10 | { 11 | char prev = str.charAt (i); 12 | i++; 13 | if (prev == 'A') 14 | res = res & (str.charAt (i) - '0'); 15 | else if (prev == 'B') 16 | res = res | (str.charAt (i) - '0'); 17 | else 18 | res = res ^ (str.charAt (i) - '0'); 19 | i++; 20 | } 21 | return res; 22 | } 23 | 24 | public static void main (String[]args) 25 | { 26 | Scanner sc = new Scanner (System.in); 27 | String str =sc.next(); 28 | System.out.println (operationsBinaryString (str)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Accenture/Que 2/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Question 2: 2 | (Asked in Accenture OnCampus 10 Aug 2021, Slot 2) 3 | 4 | Problem Description : 5 | 6 | The Binary number system only uses two digits, 0 and 1 and number system can be called binary string. You are required to implement the following function: 7 | 8 | int OperationsBinaryString(char* str); 9 | 10 | The function accepts a string str as its argument. The string str consists of binary digits eparated with an alphabet as follows: 11 | 12 | – A denotes AND operation 13 | – B denotes OR operation 14 | – C denotes XOR Operation 15 | You are required to calculate the result of the string str, scanning the string to right taking one opearation at a time, and return the same. 16 | 17 | Note: 18 | 19 | No order of priorities of operations is required 20 | Length of str is odd 21 | If str is NULL or None (in case of Python), return -1 22 | Input: 23 | 24 | str: 1C0C1C1A0B1 25 | 26 | Output: 27 | 28 | 1 29 | 30 | Explanation: 31 | 32 | The alphabets in str when expanded becomes “1 XOR 0 XOR 1 XOR 1 AND 0 OR 1”, result of the expression becomes 1, hence 1 is returned. 33 | 34 | Sample Input: 35 | 36 | 0C1A1B1C1C1B0A0 37 | 38 | Output: 39 | 40 | 0 41 | -------------------------------------------------------------------------------- /Accenture/Que 20 - Autobiographical Number/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Solution 3 | { 4 | 5 | public static int findAutoCount (String n) 6 | { 7 | int sum = 0; 8 | 9 | for (int i = 0; i < n.length (); i++) 10 | sum = sum + Integer.parseInt (n.charAt (i) + ""); 11 | 12 | if (sum == n.length ()) 13 | { 14 | int count = 0; 15 | int arr[] = new int[10]; 16 | for (int i = 0; i < n.length (); i++) 17 | arr[Integer.parseInt (n.charAt (i) + "")]++; 18 | 19 | for (int i = 0; i < arr.length; i++) 20 | if (arr[i] != 0) 21 | count++; 22 | return count; 23 | } 24 | else 25 | return 0; 26 | } 27 | 28 | public static void main (String[]args) 29 | { 30 | Scanner sc = new Scanner (System.in); 31 | String str = sc.next (); 32 | 33 | System.out.println (findAutoCount (str)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Accenture/Que 3 - Password Checker/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Question 3: Password Checker 2 | (Asked in Accenture OnCampus 10 Aug 2021, Slot 3) 3 | 4 | You are given a function. 5 | int CheckPassword(char str[], int n); 6 | The function accepts string str of size n as an argument. Implement the function which returns 1 if given string str is valid password else 0. 7 | str is a valid password if it satisfies the below conditions. 8 | 9 | – At least 4 characters 10 | – At least one numeric digit 11 | – At Least one Capital Letter 12 | – Must not have space or slash (/) 13 | – Starting character must not be a number 14 | Assumption: 15 | Input string will not be empty. 16 | 17 | Example: 18 | 19 | Input: 20 | aA1_67 21 | Output: 22 | 1 23 | 24 | Sample Input: 25 | a987 abC012 26 | Output: 27 | 0 28 | -------------------------------------------------------------------------------- /Accenture/Que 3 - Password Checker/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Solution 3 | { 4 | 5 | public static int checkPassword (String str, int n) 6 | { 7 | if (n < 4) 8 | return 0; 9 | if (str.charAt (0) >= '0' && str.charAt (0) <= '9') 10 | return 0; 11 | 12 | int num = 0, cap = 0; 13 | for (int i = 0; i < n; i++) 14 | { 15 | if (str.charAt (i) == ' ' || str.charAt (i) == '/') 16 | return 0; 17 | if (str.charAt (i) >= 'A' && str.charAt (i) <= 'Z') 18 | cap++; 19 | if (str.charAt (i) >= '0' && str.charAt (i) <= '9') 20 | num++; 21 | } 22 | if (cap > 0 && num > 0) 23 | return 1; 24 | else 25 | return 0; 26 | } 27 | 28 | public static void main (String[]args) 29 | { 30 | Scanner sc = new Scanner (System.in); 31 | String str = sc.next (); 32 | System.out.println (checkPassword (str, str.length ())); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Accenture/Que 4/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static int findCount (int arr[], int length, int num, int diff) 5 | { 6 | int count = 0; 7 | for (int i = 0; i < length; i++) 8 | { 9 | if (Math.abs (num - arr[i]) <= diff) 10 | count++; 11 | } 12 | return count>0?count:-1; 13 | } 14 | 15 | public static void main (String[]args) 16 | { 17 | Scanner sc = new Scanner (System.in); 18 | int n = sc.nextInt (); 19 | int arr[] = new int[n]; 20 | 21 | for (int i = 0; i < n; i++) 22 | arr[i] = sc.nextInt (); 23 | int num = sc.nextInt (); 24 | int diff = sc.nextInt (); 25 | 26 | System.out.println (findCount (arr, n, num, diff)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Accenture/Que 4/Main.py: -------------------------------------------------------------------------------- 1 | #as same question but in py 2 | def findCount(arr, length, num, diff): 3 | count = 0 #a var for counting 4 | 5 | for i in range(length): 6 | absolute_diff = abs(arr[i] - num) #abs gives no -ve or complex values 7 | if absolute_diff <= diff: 8 | count += 1 9 | 10 | return count if count > 0 else -1 #if condition fail means 11 | 12 | # Test example 13 | arr = [12, 3, 14, 56, 77, 13] 14 | num = 13 15 | diff = 2 16 | result = findCount(arr, len(arr), num, diff) 17 | print("Output:", result) 18 | -------------------------------------------------------------------------------- /Accenture/Que 4/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Question 4: 2 | (Asked in Accenture OnCampus 11 Aug 2021, Slot 1) 3 | 4 | You are given a function, 5 | int findCount(int arr[], int length, int num, int diff); 6 | 7 | The function accepts an integer array ‘arr’, its length and two integer variables ‘num’ and ‘diff’. Implement this function to find and return the number of elements of ‘arr’ having an absolute difference of less than or equal to ‘diff’ with ‘num’. 8 | Note: In case there is no element in ‘arr’ whose absolute difference with ‘num’ is less than or equal to ‘diff’, return -1. 9 | 10 | Example: 11 | Input: 12 | 13 | arr: 12 3 14 56 77 13 14 | num: 13 15 | diff: 2 16 | Output: 17 | 3 18 | 19 | Explanation: 20 | Elements of ‘arr’ having absolute difference of less than or equal to ‘diff’ i.e. 2 with ‘num’ i.e. 13 are 12, 13 and 14. 21 | -------------------------------------------------------------------------------- /Accenture/Que 5/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Question 5 : 2 | (Asked in Accenture OnCampus 11 Aug 2021, Slot 2) 3 | 4 | Implement the following Function 5 | 6 | def differenceofSum(n. m) 7 | 8 | The function accepts two integers n, m as arguments Find the sum of all numbers in range from 1 to m(both inclusive) that are not divisible by n. Return difference between sum of integers not divisible by n with sum of numbers divisible by n. 9 | 10 | Assumption: 11 | 12 | n>0 and m>0 13 | Sum lies between integral range 14 | Example 15 | 16 | Input 17 | n:4 18 | m:20 19 | Output 20 | 90 21 | 22 | Explanation 23 | 24 | Sum of numbers divisible by 4 are 4 + 8 + 12 + 16 + 20 = 60 25 | Sum of numbers not divisible by 4 are 1 +2 + 3 + 5 + 6 + 7 + 9 + 10 + 11 + 13 + 14 + 15 + 17 + 18 + 19 = 150 26 | Difference 150 – 60 = 90 27 | Sample Input 28 | n:3 29 | m:10 30 | Sample Output 31 | 19 32 | -------------------------------------------------------------------------------- /Accenture/Que 5/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Solution 3 | { 4 | public static int differenceOfSum (int m, int n) 5 | { 6 | int sum1 = 0, sum2 = 0; 7 | for (int i = 1; i <= m; i++) 8 | { 9 | if (i % n == 0) 10 | sum1 = sum1 + i; 11 | else 12 | sum2 = sum2 + i; 13 | } 14 | return Math.abs (sum1 - sum2); 15 | } 16 | 17 | public static void main (String[]args) 18 | { 19 | Scanner sc = new Scanner (System.in); 20 | int n = sc.nextInt (); 21 | int m = sc.nextInt (); 22 | System.out.println (differenceOfSum (m, n)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Accenture/Que 6/AddOfArrayLargeinEvenAndSmallInOdd.py: -------------------------------------------------------------------------------- 1 | def LargeSmallSum(arr): 2 | if len(arr) == 0 or len(arr) <= 3: #condition for the check the arr size is small or large 3 | return 0 4 | 5 | even_positions = [arr[i] for i in range(0, len(arr), 2)] 6 | odd_positions = [arr[i] for i in range(1, len(arr), 2)] 7 | 8 | even_positions.sort() 9 | odd_positions.sort() 10 | 11 | return even_positions[-2] + odd_positions[1] 12 | 13 | # Test example 14 | arr1 = [3, 2, 1, 7, 5, 4] 15 | result1 = LargeSmallSum(arr1) 16 | print("Output:", result1) # Output: 7 17 | 18 | arr2 = [1, 8, 0, 2, 3, 5, 6] 19 | result2 = LargeSmallSum(arr2) 20 | print("Output:", result2) # Output: 8 21 | #or in function 22 | even_pos = [] 23 | odd_pos = [] 24 | 25 | for i in range(len(arr)): 26 | if i % 2 == 0: 27 | even_pos.append(arr[i]) 28 | else: 29 | odd_pos.append(arr[i]) 30 | 31 | even_pos.sort() 32 | odd_pos.sort() 33 | 34 | return even_pos[-2] + odd_pos[1] 35 | 36 | 37 | print(LargeSmallSum(arr)) # Output: 16 38 | 39 | -------------------------------------------------------------------------------- /Accenture/Que 6/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static int largeSmallSum (int[]arr,int n) 5 | { 6 | if(n<=3) 7 | return 0; 8 | ArrayList < Integer > even = new ArrayList < Integer > (); 9 | ArrayList < Integer > odd = new ArrayList < Integer > (); 10 | even.add (arr[0]); 11 | 12 | for (int i = 1; i < arr.length; i++) 13 | { 14 | if (i % 2 == 0) 15 | even.add (arr[i]); 16 | else 17 | odd.add (arr[i]); 18 | } 19 | Collections.sort (even); 20 | Collections.sort (odd); 21 | 22 | return even.get (even.size () - 2) + odd.get (1); 23 | } 24 | 25 | public static void main (String[]args) 26 | { 27 | Scanner sc = new Scanner (System.in); 28 | int n = sc.nextInt (); 29 | int arr[] = new int[n]; 30 | for (int i = 0; i < n; i++) 31 | arr[i] = sc.nextInt (); 32 | 33 | System.out.println (largeSmallSum (arr,n)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Accenture/Que 6/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Question:6 2 | (Asked in Accenture OnCampus 11 Aug 2021, Slot 3) 3 | 4 | You are required to implement the following Function 5 | 6 | def LargeSmallSum(arr) 7 | 8 | The function accepts an integers arr of size ’length’ as its arguments you are required to return the sum of second largest element from the even positions and second smallest from the odd position of given ‘arr’ 9 | 10 | Assumption: 11 | 12 | All array elements are unique 13 | Treat the 0th position as even 14 | NOTE 15 | 16 | Return 0 if array is empty 17 | Return 0, if array length is 3 or less than 3 18 | Example 19 | 20 | Input 21 | 22 | arr:3 2 1 7 5 4 23 | 24 | Output 25 | 26 | 7 27 | 28 | Explanation 29 | 30 | Second largest among even position elements(1 3 5) is 3 31 | Second smallest among odd position element is 4 32 | Thus output is 3+4 = 7 33 | Sample Input 34 | 35 | arr:1 8 0 2 3 5 6 36 | 37 | Sample Output 38 | 39 | 8 40 | -------------------------------------------------------------------------------- /Accenture/Que 7/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static int productSmallestPair (int arr[], int n, int sum) 5 | { 6 | if (n <2) 7 | return -1; 8 | int ans, temp, check; 9 | for (int i = 0; i < n; i++) 10 | { 11 | for (int j = i + 1; j < n; j++) 12 | { 13 | if (arr[i] > arr[j]) 14 | { 15 | temp = arr[i]; 16 | arr[i] = arr[j]; 17 | arr[j] = temp; 18 | } 19 | } 20 | } 21 | check = arr[0] + arr[1]; 22 | 23 | if (check <= sum) 24 | return arr[0] * arr[1]; 25 | else 26 | return 0; 27 | } 28 | 29 | public static void main (String[]args) 30 | { 31 | Scanner sc = new Scanner (System.in); 32 | int sum = sc.nextInt (); 33 | int n = sc.nextInt (); 34 | int arr[] = new int[n]; 35 | 36 | for (int i = 0; i < n; i++) 37 | arr[i] = sc.nextInt (); 38 | System.out.println (productSmallestPair (arr, n, sum)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Accenture/Que 7/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Question:7 2 | (Asked in Accenture OnCampus 12 Aug 2021, Slot 1) 3 | 4 | Implement the following Function 5 | 6 | def ProductSmallestPair(sum, arr) 7 | 8 | The function accepts an integers sum and an integer array arr of size n. Implement the function to find the pair, (arr[j], arr[k]) where j!=k, Such that arr[j] and arr[k] are the least two elements of array (arr[j] + arr[k] <= sum) and return the product of element of this pair 9 | 10 | NOTE 11 | 12 | Return -1 if array is empty or if n<2 13 | Return 0, if no such pairs found 14 | All computed values lie within integer range 15 | Example 16 | 17 | Input 18 | 19 | sum:9 20 | 21 | Arr:5 2 4 3 9 7 1 22 | 23 | Output 24 | 25 | 2 26 | 27 | Explanation 28 | 29 | Pair of least two element is (2, 1) 2 + 1 = 3 < 9, Product of (2, 1) 2*1 = 2. Thus, output is 2 30 | 31 | Sample Input 32 | 33 | sum:4 34 | 35 | Arr:9 8 3 -7 3 9 36 | 37 | Sample Output 38 | 39 | -21 40 | -------------------------------------------------------------------------------- /Accenture/Que 8/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static String dectoNBase (int n, int num) 5 | { 6 | String res = ""; 7 | int quotient = num / n; 8 | ArrayList < Integer > rem = new ArrayList < Integer > (); 9 | rem.add (num % n); 10 | 11 | while(quotient != 0) 12 | { 13 | rem.add (quotient % n); 14 | quotient = quotient / n; 15 | } 16 | 17 | for (int i = 0; i < rem.size (); i++) 18 | { 19 | if (rem.get (i) > 9) 20 | { 21 | res = (char) (rem.get (i) - 9 + 64) + res; 22 | } 23 | else 24 | res = rem.get (i) + res; 25 | } 26 | return res; 27 | } 28 | 29 | public static void main (String[]args) 30 | { 31 | Scanner sc = new Scanner (System.in); 32 | int n = sc.nextInt (); 33 | int num = sc.nextInt (); 34 | System.out.println (dectoNBase (n, num)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Accenture/Que 9/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Question:9 2 | (Asked in Accenture Offcampus 1 Aug 2021, Slot 1) 3 | 4 | Implement the following functions.a 5 | 6 | char*MoveHyphen(char str[],int n); 7 | 8 | The function accepts a string “str” of length ‘n’, that contains alphabets and hyphens (-). Implement the function to move all hyphens(-) in the string to the front of the given string. 9 | 10 | NOTE:- Return null if str is null. 11 | 12 | Example :- 13 | 14 | Input: 15 | str.Move-Hyphens-to-Front 16 | Output: 17 | —MoveHyphenstoFront 18 | Explanation:- 19 | 20 | The string “Move-Hyphens -to-front” has 3 hyphens (-), which are moved to the front of the string, this output is “— MoveHyphen” 21 | 22 | Sample Input 23 | 24 | Str: String-Compare 25 | Sample Output- 26 | 27 | -StringCompare 28 | -------------------------------------------------------------------------------- /Accenture/Que 9/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Solution 3 | { 4 | public static String moveHyphen (String str, int n) 5 | { 6 | String res = ""; 7 | for (int i = 0; i < n; i++) 8 | { 9 | if (str.charAt (i) == '-') 10 | res = str.charAt (i) + res; 11 | else 12 | res = res + str.charAt (i); 13 | } 14 | return res; 15 | } 16 | 17 | public static void main (String[]args) 18 | { 19 | Scanner sc = new Scanner (System.in); 20 | String str = sc.next (); 21 | 22 | System.out.println (moveHyphen (str, str.length ())); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Cognizant/Que 1/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.text.*; 3 | class Main{ 4 | 5 | public static void main (String[] args) { 6 | DecimalFormat df2 =new DecimalFormat("0.00"); 7 | Scanner sc= new Scanner (System.in); 8 | 9 | System.out.println("Enter the no of liters to fill the tank"); 10 | int ltt =sc.nextInt(); 11 | double lt= (ltt*1.00); 12 | 13 | if(ltt<1){ 14 | System.out.println(ltt+" is an Invalid Input"); 15 | System.exit(0); 16 | } 17 | System.out.println("Enter the distance covered"); 18 | int diss =sc.nextInt(); 19 | double dis= (diss*1.00); 20 | 21 | if(diss<1){ 22 | System.out.println(diss+" is an Invalid Input"); 23 | System.exit(0); 24 | } 25 | 26 | double hundered = ((lt/dis)*100); 27 | System.out.println("Liters/100KM"); 28 | System.out.println(df2.format(hundered)); 29 | 30 | double miles = (dis*0.6214); 31 | double gallons =(lt*0.2642); 32 | double mg = miles/gallons; 33 | System.out.println("Miles/gallons"); 34 | System.out.println(df2.format(mg)); 35 | } 36 | } -------------------------------------------------------------------------------- /Cognizant/Que 10/LuckyNum.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class LuckyNum{ 3 | public static void main(String[] args){ 4 | int sum=0; 5 | Scanner sc= new Scanner (System.in); 6 | System.out.print("Enter the car no:"); 7 | int carNum = sc.nextInt(); 8 | 9 | if(carNum<1000 || carNum>9999){ 10 | System.out.println(carNum+" is not a valid car number"); 11 | } 12 | else{ 13 | while(carNum!=0){ 14 | int l=carNum%10; 15 | sum=sum+l; 16 | carNum=carNum/10; 17 | } 18 | 19 | if(sum%3==0||sum%5==0||sum%7==0){ 20 | System.out.println("Lucky Number"); 21 | } 22 | else{ 23 | System.out.println("Sorry its not my lucky number"); 24 | } 25 | 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Cognizant/Que 10/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement – Chaman planned to choose a four digit lucky number for his car. His lucky numbers are 3,5 and 7. Help him find the number, whose sum is divisible by 3 or 5 or 7. Provide a valid car number, Fails to provide a valid input then display that number is not a valid car number. 2 | 3 | Note : The input other than 4 digit positive number[includes negative and 0] is considered as invalid. 4 | 5 | Refer the samples, to read and display the data. 6 | 7 | Sample Input 1: 8 | 9 | Enter the car no:1234 10 | Sample Output 1: 11 | 12 | Lucky Number 13 | Sample Input 2: 14 | 15 | Enter the car no:1214 16 | Sample Output 2: 17 | 18 | Sorry its not my lucky number 19 | Sample Input 3: 20 | 21 | Enter the car no:14 22 | Sample Output 3: 23 | 24 | 14 is not a valid car number -------------------------------------------------------------------------------- /Cognizant/Que 11/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement – 2 | 3 | IIHM institution is offering a variety of courses to students. Students have a facility to check whether a particular course is available in the institution. Write a program to help the institution accomplish this task. If the number is less than or equal to zero display “Invalid Range”. 4 | 5 | Assume maximum number of courses is 20. 6 | 7 | Sample Input 1: 8 | 9 | Enter no of course: 10 | 5 11 | 12 | Enter course names: 13 | Java 14 | 15 | Oracle 16 | 17 | C++ 18 | 19 | Mysql 20 | 21 | Dotnet 22 | 23 | Enter the course to be searched: 24 | C++ 25 | 26 | Sample Output 1: 27 | 28 | C++ course is available 29 | 30 | Sample Input 2: 31 | 32 | Enter no of course: 33 | 3 34 | 35 | Enter course names: 36 | Java 37 | 38 | Oracle 39 | 40 | Dotnet 41 | 42 | Enter the course to be searched: 43 | C++ 44 | 45 | Sample Output 2: 46 | 47 | C++ course is not available 48 | 49 | Sample Input 3: 50 | 51 | Enter no of course: 52 | 0 53 | 54 | Sample Output 3: 55 | 56 | Invalid Range -------------------------------------------------------------------------------- /Cognizant/Que 12/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement – Mayuri buys “N” no of products from a shop. The shop offers a different percentage of discount on each item. She wants to know the item that has the minimum discount offer, so that she can avoid buying that and save money. 2 | [Input Format: The first input refers to the no of items; the second input is the item name, price and discount percentage separated by comma(,)] 3 | Assume the minimum discount offer is in the form of Integer. 4 | 5 | Note: There can be more than one product with a minimum discount. 6 | 7 | Sample Input 1: 8 | 9 | 4 10 | 11 | mobile,10000,20 12 | 13 | shoe,5000,10 14 | 15 | watch,6000,15 16 | 17 | laptop,35000,5 18 | 19 | Sample Output 1: 20 | 21 | shoe 22 | 23 | Explanation: The discount on the mobile is 2000, the discount on the shoe is 500, the discount on the watch is 900 and the discount on the laptop is 1750. So the discount on the shoe is the minimum. 24 | 25 | Sample Input 2: 26 | 27 | 4 28 | 29 | Mobile,5000,10 30 | 31 | shoe,5000,10 32 | 33 | WATCH,5000,10 34 | 35 | Laptop,5000,10 36 | 37 | Sample Output 2: 38 | 39 | Mobile 40 | 41 | shoe 42 | 43 | WATCH 44 | 45 | Laptop -------------------------------------------------------------------------------- /Cognizant/Que 14/FindFactor.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class FindFactor{ 4 | public static void main(String[] args){ 5 | int i; 6 | Scanner sc = new Scanner (System.in); 7 | int num = sc.nextInt(); 8 | if(num==0){ 9 | System.out.println("No Factors"); 10 | } 11 | else if(num>0){ 12 | for(i=1;i 2 | using namespace std; 3 | int HCF(int, int); 4 | int LCM(int, int); 5 | 6 | int main() { 7 | int num1, num2, result; 8 | cin>>num1; 9 | cin>>num2; 10 | result = LCM ( num1, num2 ); 11 | cout<< result; 12 | return 0; 13 | } 14 | 15 | // This function find the Highest Common Factor of the two numbers 16 | int HCF (int num1, int num2) { 17 | if (num2 == 0) 18 | return num1; 19 | return HCF(num2, num1 % num2); 20 | } 21 | 22 | // This function find the Lowest Common Factor of the two numbers 23 | int LCM (int num1, int num2) { 24 | return (num1 * num2) / HCF(num1, num2); 25 | } 26 | -------------------------------------------------------------------------------- /Cognizant/Que 16/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Ananya Pandey’s professor has suggested her to prepare well for the lesson on seasons. While her professor says a month, she needs to tell the season corresponding to that month. Write a program to solve the above task. 2 | 3 | Spring – March to May, 4 | Summer – June to August, 5 | Autumn – September to November and, 6 | Winter – December to February. 7 | The month should be in the range of 1 to 12. If not the output should be “Invalid month”. 8 | Sample Input 1: 9 | 10 | Enter the month: 08 11 | Sample Output 1: 12 | 13 | Season: Summer 14 | Sample Input 2: 15 | 16 | Enter the month: 14 17 | Sample Output 2: 18 | 19 | Invalid month 20 | -------------------------------------------------------------------------------- /Cognizant/Que 16/monthSeason.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | printf("Enter the month:"); 8 | int entry ; 9 | scanf("%d",&entry); 10 | 11 | switch (entry) 12 | { 13 | case 12: 14 | case 1: 15 | case 2: 16 | printf("Season: Winter"); 17 | break; 18 | 19 | case 3: 20 | case 4: 21 | case 5: 22 | printf("Season: Spring"); 23 | break; 24 | 25 | case 6: 26 | case 7: 27 | case 8: 28 | printf("Season: Summer"); 29 | break; 30 | 31 | case 9: 32 | case 10: 33 | case 11: 34 | printf("Season: Autumn"); 35 | break; 36 | 37 | default: 38 | printf("Invalid month"); 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Cognizant/Que 17/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | There is a scheme declared in theatre, in which one ticket gets a 10% discount on the total cost of tickets if there is a bulk booking which is more than 20 tickets, and if there is a special coupon card then there is 2% discount on total the cost. Also, customer can buy Refreshments which additional cost of Rs. 50 per person. 2 | 3 | The cost of the K class Rs. 75 and the Q class is Rs. 150. 4 | 5 | Write a program to find the total cost as per the scheme. 6 | 7 | Note: In-class K and class Q, you have to buy at a time the minimum of 5 tickets and a maximum of 40. If this condition fails then display the message “Minimum of 5 and Maximum of 40 Tickets”. 8 | 9 | If the user is given a value other than class ‘K’ or ‘Q’ the output should be “Invalid Input”. 10 | 11 | The cost should be printed correctly to the two decimal places. 12 | 13 | Sample Input 1: 14 | 15 | Enter the no of ticket:35 16 | Do you want refreshment:y 17 | Do you have coupon code:y 18 | Enter the circle:k 19 | Sample Output 1: 20 | 21 | Ticket cost:4065.25 22 | Sample Input 2: 23 | 24 | Enter the no of ticket:1 25 | Sample Output 2: 26 | 27 | Minimum of 5 and Maximum of 40 Tickets -------------------------------------------------------------------------------- /Cognizant/Que 17/ticketScheme.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int noTicket; 6 | double total = 0,cost; 7 | char ref[2], co[2] , circle[2]; 8 | 9 | printf("Enter the no of ticket:"); 10 | scanf("%d",&noTicket); 11 | 12 | if (noTicket < 5 || noTicket > 40) { 13 | printf("Minimum of 5 and Maximum of 40 tickets"); 14 | exit(0); 15 | } 16 | 17 | printf("Do you want refreshment:"); 18 | scanf("%s",&ref); 19 | printf("Do you have coupon code:"); 20 | scanf("%s",&co); 21 | printf("Enter the circle:"); 22 | scanf("%s",&circle); 23 | 24 | if(circle[0] == 'K') 25 | cost=75*noTicket; 26 | 27 | else if(circle[0]== 'Q') 28 | cost=150*noTicket; 29 | 30 | else 31 | { 32 | printf("Invalid Input"); 33 | exit(0); 34 | } 35 | total=cost; 36 | 37 | if(noTicket>20) 38 | cost= cost - ((0.1)*cost); 39 | total=cost; 40 | 41 | if(co[0]== 'y') 42 | total= cost - ((0.02)*cost); 43 | 44 | if(ref[0]== 'y') 45 | total += (noTicket*50); 46 | printf("Ticket cost:%.2f",total); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Cognizant/Que 18/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Print the prime numbers from 1 to N, where N is an input. 2 | 3 | Sample Input 1: 4 | 5 | 15 6 | 7 | Sample Output 1: 8 | 9 | 2 3 5 7 11 13 10 | 11 | Sample Input 2: 12 | 13 | 5 14 | 15 | Sample Output 2: 16 | 17 | 2 3 5 -------------------------------------------------------------------------------- /Cognizant/Que 18/prime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool isPrime(int); 4 | 5 | int main(){ 6 | int n; 7 | cin>>n; 8 | for(int i = 1; i <= n; i++) { 9 | int isPrimeNumber = isPrime(i); 10 | if(isPrimeNumber == true) { 11 | cout< 1){ 27 | // Number is prime 28 | return true; 29 | } 30 | // Number is not a prime 31 | return false; 32 | } 33 | -------------------------------------------------------------------------------- /Cognizant/Que 19/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Bela teaches his son to find the factors of the given numbers. When he provides a number to his son, he should tell the factors of that number. Help him to do this, by writing a program. 2 | 3 | Note: 4 | 5 | If the input is zero (0) then the output will be "No Factors". 6 | If the input is any negative number, then first convert it positive solve. 7 | Sample Input 1: 8 | 9 | 54 10 | 11 | Sample Output 1: 12 | 13 | Factors for 56 are: 14 | 15 | 1 16 | 17 | 2 18 | 19 | 4 20 | 21 | 7 22 | 23 | 8 24 | 25 | 14 26 | 27 | 28 28 | 29 | 56 30 | 31 | 32 | 33 | Sample Input 2: 34 | 35 | -256 36 | 37 | Sample Output 2: 38 | 39 | Factors of 256 are: 40 | 41 | 1 42 | 43 | 2 44 | 45 | 4 46 | 47 | 8 48 | 49 | 16 50 | 51 | 32 52 | 53 | 64 54 | 55 | 128 56 | 57 | 256 -------------------------------------------------------------------------------- /Cognizant/Que 19/factor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n, i; 8 | cout << "Enter the positive integer: "; 9 | cin >> n; 10 | 11 | if(n == 0) { 12 | cout << "No Factors"; 13 | return false; 14 | } 15 | if(n < 0) { 16 | n = abs( n ); 17 | } 18 | cout << "Factors of " << n << " are: " << endl; 19 | for(i = 1; i <= n; ++i) { 20 | if(n % i == 0) { 21 | cout << i << endl; 22 | } 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Cognizant/Que 2/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main 3 | { 4 | public static void main (String[]args) 5 | { 6 | int totalprice; 7 | Scanner sc = new Scanner (System.in); 8 | 9 | System.out.print ("Enter the no of pizzas bought:"); 10 | int pizza = sc.nextInt (); 11 | 12 | System.out.print ("Enter the no of puffs bought:"); 13 | int puffs = sc.nextInt (); 14 | 15 | System.out.print ("Enter the no of cool drinks bought:"); 16 | int coolDrinks = sc.nextInt (); 17 | 18 | int pizzaa = Math.abs (pizza) * 100; 19 | int puffss = Math.abs (puffs) * 20; 20 | int coolDrinkss = Math.abs (coolDrinks) * 10; 21 | 22 | System.out.println ("Bill Details"); 23 | System.out.println ("No of pizzas:" + pizza); 24 | System.out.println ("No of puffs:" + puffs); 25 | System.out.println ("No of cooldrinks:" + coolDrinks); 26 | 27 | totalprice = pizzaa + puffss + coolDrinkss; 28 | System.out.println ("Total price=" + totalprice); 29 | System.out.println ("ENJOY THE SHOW!!!"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Cognizant/Que 2/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement – Vohra went to a movie with his friends in a Wave theatre and during break time he bought pizzas, puffs and cool drinks. Consider the following prices : 2 | 3 | Rs.100/pizza 4 | Rs.20/puffs 5 | Rs.10/cooldrink 6 | Generate a bill for What Vohra has bought. 7 | 8 | Sample Input 1: 9 | 10 | Enter the no of pizzas bought:10 11 | Enter the no of puffs bought:12 12 | Enter the no of cool drinks bought:5 13 | Sample Output 1: 14 | 15 | Bill Details 16 | 17 | No of pizzas:10 18 | No of puffs:12 19 | No of cooldrinks:5 20 | Total price=1290 21 | ENJOY THE SHOW!!! -------------------------------------------------------------------------------- /Cognizant/Que 20/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem statement-: Elliot made a KeyLogger for his friend Romero, so that he can see the passwords of his friend. Keylogger is a software that can tell you the buttons pressed in the keyboard without the consent of the user, and hence unethical. Elliot made it to hack Romero’s passwords. The one problem is, Romero writes the passwords in lowercase characters only, and the keylogger only takes the values of the keys. Like, for a it takes 1, for b 2, and for z 26. For a given number Elliot produces all combinations of passwords in a dictionary and starts a dictionary based password attack. For a given number, print all the possible passwords in a lexicographic order. 2 | 3 | Input Format: 4 | 5 | One line, denoting the value given by the keylogger 6 | Output Format: 7 | 8 | All possible combinations of keyloggers in new lines are lexicographically ordered. 9 | Constraints: 10 | 11 | 2<=Number of digit in input<=1000 12 | Sample Input: 13 | 14 | 1234 15 | 16 | Sample Output: 17 | 18 | abcd 19 | 20 | awd 21 | 22 | lcd 23 | 24 | Explanation: 25 | 26 | For 12, you can take 1,2 that is ab, or you can take l. -------------------------------------------------------------------------------- /Cognizant/Que 21/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Solution 3 | { 4 | static int n, w; 5 | static int a[]; 6 | static int b[]; 7 | static int c[]; 8 | 9 | public static int func (int i, int cb, int t, int ww) 10 | { 11 | if (i == n) 12 | { 13 | if ((cb & 1) != 0) 14 | return 0; 15 | else 16 | return t; 17 | } 18 | if (ww + b[i] <= w) 19 | return Math.max (func (i + 1, cb + c[i], t + a[i], ww + b[i]),func (i + 1, cb, t, ww)); 20 | return func (i + 1, cb, t, ww); 21 | } 22 | 23 | public static void main (String[]args) 24 | { 25 | Scanner sc = new Scanner (System.in); 26 | n = sc.nextInt (); 27 | w = sc.nextInt (); 28 | int t; 29 | a = new int[n]; 30 | b = new int[n]; 31 | c = new int[n]; 32 | 33 | for (int i = 0; i < n; i++) 34 | { 35 | t = sc.nextInt (); 36 | a[i] = sc.nextInt (); 37 | b[i] = sc.nextInt (); 38 | c[i] = sc.nextInt (); 39 | } 40 | 41 | System.out.println (func (0, 0, 0, 0)); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Cognizant/Que 24/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement – Codu is given a string and he thinks the letters that are repeated do have more power. He gathers only the repeating characters and keeps them as the most powerful to least powerful manner. Now it is your turn to write a code that will help Codu to do that. 2 | 3 | Note that: only lowercase alphabets are accepted in input. 4 | 5 | 6 | 7 | Input Format: 8 | 9 | A string in a single line 10 | 11 | 12 | Output Format: 13 | 14 | A string made of only the repeated characters as sorted their frequency reducin, if the same the lower ascii value comes before. 15 | 16 | 17 | Constraints: 18 | 19 | Length of string<=10^5 20 | 21 | 22 | Sample Input: 23 | 24 | abcdefghaabca 25 | 26 | 27 | 28 | Sample Output: 29 | 30 | abc -------------------------------------------------------------------------------- /Cognizant/Que 24/StringReduction.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class StringReduction 3 | { 4 | public static void main (String[]args) 5 | { 6 | Scanner sc = new Scanner (System.in); 7 | String str = sc.next (); 8 | TreeSet < Character > list = new TreeSet < Character > (); 9 | for (int i = 0; i + 1 < str.length (); i++) 10 | { 11 | String prefix = str.substring (0, i); 12 | String suffix = str.substring (i + 1, str.length ()); 13 | char ch = str.charAt (i); 14 | if (prefix.indexOf (ch) != -1 || suffix.indexOf (ch) != -1) 15 | list.add (ch); 16 | } 17 | if (str.substring (0, str.length () - 1). 18 | indexOf (str.charAt (str.length () - 1)) != -1) 19 | list.add (str.charAt (str.length () - 1)); 20 | Iterator itr = list.iterator (); 21 | while (itr.hasNext ()) 22 | { 23 | System.out.print ((Character) itr.next ()); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Cognizant/Que 26/GameOfClicks.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class GameOfClicks 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | int l=sc.nextInt(); 8 | int h=sc.nextInt(); 9 | int b=sc.nextInt(); 10 | Listbl=new ArrayList<>(); 11 | for(int i=0;ivl=new ArrayList<>(); 16 | for(int i=0;isl=new HashSet<>(); 20 | int res=0; 21 | for(Integer i:vl){ 22 | if(bl.contains(i)) 23 | continue; 24 | sl.add(i); 25 | } 26 | for(Integer i:sl){ 27 | String s=i+""; 28 | res+=s.length(); 29 | } 30 | System.out.println(res); 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Cognizant/Que 28/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement : Given two integers, the task is to find the hamming distance between two integers. Hamming Distance between two integers is the number of bits that are different at the same position in both numbers. 2 | 3 | Example : 4 | 5 | Input 1: 6 | 7 | a= 10 , b =8 8 | 9 | Output 1: 10 | 11 | 1 12 | 13 | Explanation: 10= 1010 , 8=1000 , No of different bits=1. 14 | 15 | Input 2: 16 | 17 | a=9, b=14 18 | 19 | Output 2: 20 | 21 | 3 22 | 23 | Explanation: 9=1001 , 14=1110 , No. of different bits=3. -------------------------------------------------------------------------------- /Cognizant/Que 28/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Solution 3 | { 4 | public static int hammingDistance (int a, int b) 5 | { 6 | int max = a > b ? a : b; 7 | int count = 0; 8 | while (max > 0) 9 | { 10 | if ((a & 1) != (b & 1)) 11 | count++; 12 | a = a >> 1; 13 | b = b >> 1; 14 | max = max >> 1; 15 | } 16 | return count; 17 | } 18 | 19 | public static void main (String[]args) 20 | { 21 | Scanner sc = new Scanner (System.in); 22 | int a = sc.nextInt (); 23 | int b = sc.nextInt (); 24 | 25 | System.out.println (hammingDistance (a, b)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Cognizant/Que 29/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement: Given boolean 2D matrix , find the number of islands. A group of connected 1s form an island. For example, the below matrix contains 5 islands. 2 | 3 | Example : 4 | 5 | m =5, n=5 6 | 7 | 1 1 0 0 0 8 | 9 | 0 1 0 0 1 10 | 11 | 1 0 0 1 1 12 | 13 | 0 0 0 0 0 14 | 15 | 1 0 1 0 1 16 | 17 | Output : 18 | 19 | 5 20 | 21 | Explanation: There are total 5 island in the matrix. -------------------------------------------------------------------------------- /Cognizant/Que 3/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main 4 | { 5 | public static void main (String args[]) 6 | { 7 | Scanner in = new Scanner (System.in); 8 | System.out.println ("Enter the digits: "); 9 | 10 | int a = in.nextInt (); 11 | int b = in.nextInt (); 12 | int c = in.nextInt (); 13 | int d = in.nextInt (); 14 | 15 | char q = (char) a; 16 | char w = (char) b; 17 | char e = (char) c; 18 | char r = (char) d; 19 | 20 | System.out.println (); 21 | System.out.print (a); 22 | System.out.println ("-" + q); 23 | 24 | System.out.print (b); 25 | System.out.println ("-" + w); 26 | 27 | System.out.print (c); 28 | System.out.println ("-" + e); 29 | 30 | System.out.print (d); 31 | System.out.println ("-" + r); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Cognizant/Que 3/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement – Ritik wants a magic board, which displays a character for a corresponding number for his science project. Help him to develop such an application. 2 | For example when the digits 65,66,67,68 are entered, the alphabet ABCD are to be displayed. 3 | [Assume the number of inputs should be always 4 ] 4 | 5 | Sample Input 1: 6 | 7 | Enter the digits: 8 | 65 9 | 66 10 | 67 11 | 68 12 | Sample Output 1: 13 | 14 | 65-A 15 | 66-B 16 | 67-C 17 | 68-D 18 | 19 | Sample Input 2: 20 | 21 | Enter the digits: 22 | 115 23 | 116 24 | 101 25 | 112 26 | Sample Output 2: 27 | 28 | 115-s 29 | 116-t 30 | 101-e 31 | 112-p -------------------------------------------------------------------------------- /Cognizant/Que 5/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement – In a theater, there is a discount scheme announced where one gets a 10% discount on the total cost of tickets when there is a bulk booking of more than 20 tickets, and a discount of 2% on the total cost of tickets if a special coupon card is submitted. Develop a program to find the total cost as per the scheme. The cost of the k class ticket is Rs.75 and q class is Rs.150. Refreshments can also be opted by paying an additional of Rs. 50 per member. 2 | 3 | Hint: k and q and You have to book minimum of 5 tickets and maximum of 40 at a time. If fails display “Minimum of 5 and Maximum of 40 Tickets”. If circle is given a value other than ‘k’ or ‘q’ the output should be “Invalid Input”. 4 | 5 | The ticket cost should be printed exactly to two decimal places. 6 | 7 | Sample Input 1: 8 | 9 | Enter the no of ticket:35 10 | Do you want refreshment:y 11 | Do you have coupon code:y 12 | Enter the circle:k 13 | Sample Output 1: 14 | 15 | Ticket cost:4065.25 16 | Sample Input 2: 17 | 18 | Enter the no of ticket:1 19 | Sample Output 2: 20 | 21 | Minimum of 5 and Maximum of 40 Tickets -------------------------------------------------------------------------------- /Cognizant/Que 6/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Main 3 | { 4 | 5 | public static void main(String args[]) 6 | { 7 | System.out.print("Enter the month:"); 8 | Scanner s = new Scanner(System.in); 9 | int entry = s.nextInt(); 10 | 11 | switch (entry) 12 | { 13 | case 12: 14 | case 1: 15 | case 2: 16 | System.out.println("Season:Winter"); 17 | break; 18 | case 3: 19 | case 4: 20 | case 5: 21 | System.out.println("Season:Spring"); 22 | break; 23 | case 6: 24 | case 7: 25 | case 8: 26 | System.out.println("Season:Summer"); 27 | break; 28 | case 9: 29 | case 10: 30 | case 11: 31 | System.out.println("Season:Autumn"); 32 | break; 33 | default: 34 | System.out.println("Invalid month"); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Cognizant/Que 6/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement – Rhea Pandey’s teacher has asked her to prepare well for the lesson on seasons. When her teacher tells a month, she needs to say the season corresponding to that month. Write a program to solve the above task. 2 | 3 | Spring – March to May, 4 | Summer – June to August, 5 | Autumn – September to November and, 6 | Winter – December to February. 7 | Month should be in the range 1 to 12. If not the output should be “Invalid month”. 8 | 9 | Sample Input 1: 10 | 11 | Enter the month:11 12 | Sample Output 1: 13 | 14 | Season:Autumn 15 | Sample Input 2: 16 | 17 | Enter the month:13 18 | Sample Output 2: 19 | 20 | Invalid month -------------------------------------------------------------------------------- /Cognizant/Que 7/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement – To speed up his composition of generating unpredictable rhythms, Blue Bandit wants the list of prime numbers available in a range of numbers.Can you help him out? 2 | 3 | Write a java program to print all prime numbers in the interval [a,b] (a and b, both inclusive). 4 | 5 | Note 6 | 7 | Input 1 should be lesser than Input 2. Both the inputs should be positive. 8 | Range must always be greater than zero. 9 | If any of the condition mentioned above fails, then display “Provide valid input” 10 | Use a minimum of one for loop and one while loop 11 | Sample Input 1: 12 | 13 | 2 14 | 15 | 15 16 | 17 | Sample Output 1: 18 | 19 | 2 3 5 7 11 13 20 | 21 | Sample Input 2: 22 | 23 | 8 24 | 25 | 5 26 | 27 | Sample Output 2: 28 | 29 | Provide valid input -------------------------------------------------------------------------------- /Cognizant/Que 8/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main 4 | { 5 | public static void main(String args[]) 6 | { 7 | Scanner in = new Scanner(System.in); 8 | int n = in.nextInt(); 9 | int sum = 0, r; 10 | int temp = n; 11 | 12 | if(n>-1) 13 | { 14 | while(n>0) 15 | { 16 | r = n % 10; 17 | sum = (sum*10)+r; 18 | n = n/10; 19 | } 20 | if(temp==sum) 21 | System.out.println("Palindrome"); 22 | else 23 | System.out.println("Not a Palindrome"); 24 | } 25 | else 26 | { 27 | System.out.println("Invalid Input"); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Cognizant/Que 8/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement – Goutam and Tanul plays by telling numbers. Goutam says a number to Tanul. Tanul should first reverse the number and check if it is same as the original. If yes, Tanul should say “Palindrome”. If not, he should say “Not a Palindrome”. If the number is negative, print “Invalid Input”. Help Tanul by writing a program. 2 | 3 | Sample Input 1 : 4 | 5 | 21212 6 | 7 | Sample Output 1 : 8 | 9 | Palindrome 10 | 11 | Sample Input 2 : 12 | 13 | 6186 14 | 15 | Sample Output 2 : 16 | 17 | Not a Palindrome -------------------------------------------------------------------------------- /Cognizant/Que 9/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class IncrementCalculation{ 4 | 5 | public static void main (String[] args) { 6 | 7 | 8 | 9 | Scanner sc = new Scanner (System.in); 10 | 11 | System.out.println("Enter the salary"); 12 | 13 | int salary = sc.nextInt(); 14 | 15 | System.out.println("Enter the Performance appraisal rating"); 16 | 17 | float rating = sc.nextFloat(); 18 | 19 | if(salary<1||rating<1.0||rating>5.0){ 20 | 21 | System.out.println("Invalid Input"); 22 | 23 | System.exit(0); 24 | 25 | } 26 | 27 | else if(rating>=1&&rating<=3){ 28 | 29 | salary=salary+(int)(0.1*salary); 30 | 31 | System.out.println(salary); 32 | 33 | } 34 | 35 | else if(rating>3&&rating<=4){ 36 | 37 | salary=salary+(int)(0.25*salary); 38 | 39 | System.out.println(salary); 40 | 41 | } 42 | 43 | else /*if(rating>4&&rating<=5)*/{ 44 | 45 | salary=salary+(int)(0.3*salary); 46 | 47 | System.out.println(salary); 48 | 49 | } 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /GFG Practice Sets/Divide and Conquer/Karatsuba Algorithm/GFG.java: -------------------------------------------------------------------------------- 1 | //Initial Template for Java 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | import java.lang.*; 6 | 7 | class GFG { 8 | public static void main(String args[]) throws IOException { 9 | BufferedReader read = 10 | new BufferedReader(new InputStreamReader(System.in)); 11 | int t = Integer.parseInt(read.readLine()); 12 | while (t-- > 0) { 13 | String S[] = read.readLine().split(" "); 14 | String A = S[0]; 15 | String B = S[1]; 16 | 17 | Solution ob = new Solution(); 18 | System.out.println(ob.karatsubaAlgo(A,B)); 19 | } 20 | } 21 | } 22 | // } Driver Code Ends 23 | 24 | 25 | //User function Template for Java 26 | 27 | class Solution { 28 | static Long karatsubaAlgo(String A, String B) { 29 | // code here 30 | long decimal1=Long.parseLong(A,2); 31 | long decimal2=Long.parseLong(B,2); 32 | return (decimal1*decimal2); 33 | 34 | 35 | 36 | 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /GFG Practice Sets/Divide and Conquer/Karatsuba Algorithm/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given two binary strings A and B that represent value of two integers, find the product of two strings in Decimal Value. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | Input: 8 | A = "1100" , B = "01" 9 | Output: 10 | 12 11 | Explanation: 12 | Decimal representation of A is 12 and 13 | that of B is 1. So, A*B gives the 14 | output 12. 15 | Example 2: 16 | 17 | Input: 18 | A = "01" , B = "01" 19 | Output: 20 | 1 21 | Explanation: 22 | Decimal representation of both A and 23 | B is 1. So, A*B gives the output 1. 24 | 25 | 26 | Your Task: 27 | You don't need to read input or print anything. Your task is to complete the function karatsubaAlgo() which takes Strings A and B as input and returns the answer. 28 | 29 | 30 | 31 | Expected Time Complexity: O(Length of Binary String) 32 | Expected Auxiliary Space: O(1) 33 | 34 | 35 | 36 | Constraints: 37 | 1 <= |A|,|B| <= 25 38 | -------------------------------------------------------------------------------- /GFG Practice Sets/Divide and Conquer/Power of Numbers/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a number and its reverse. Find that number raised to the power of its own reverse. 2 | Note: As answers can be very large, print the result modulo 10^9 + 7. 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 2 8 | Output: 4 9 | Explanation: The reverse of 2 is 2 10 | and after raising power of 2 by 2 11 | we get 4 which gives remainder as 12 | 4 by dividing 1000000007. 13 | Example 2: 14 | 15 | Input: 16 | N = 12 17 | Output: 864354781 18 | Explanation: The reverse of 12 is 21 19 | and 1221 , when divided by 1000000007 20 | gives remainder as 864354781. 21 | Your Task: 22 | You don't need to read input or print anything. You just need to complete the function pow() that takes two parameters N and R denoting the input number and its reverse and returns power of (N to R)mod(109 + 7). 23 | 24 | Expected Time Complexity: O(LogN). 25 | Expected Auxiliary Space: O(LogN). 26 | 27 | Constraints: 28 | 1 <= N <= 10^9 29 | -------------------------------------------------------------------------------- /GFG Practice Sets/Greedy Algorithms/Activity Selection/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given N activities with their start and finish day given in array start[ ] and end[ ]. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a given day. 2 | Note : Duration of the activity includes both starting and ending day. 3 | 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 2 9 | start[] = {2, 1} 10 | end[] = {2, 2} 11 | Output: 12 | 1 13 | Explanation: 14 | A person can perform only one of the 15 | given activities. 16 | Example 2: 17 | 18 | Input: 19 | N = 4 20 | start[] = {1, 3, 2, 5} 21 | end[] = {2, 4, 3, 6} 22 | Output: 23 | 3 24 | Explanation: 25 | A person can perform activities 1, 2 26 | and 4. 27 | 28 | Your Task : 29 | You don't need to read input or print anything. Your task is to complete the function activityselection() which takes array start[ ], array end[ ] and integer N as input parameters and returns the maximum number of activities that can be done. 30 | 31 | 32 | Expected Time Complexity : O(N * Log(N)) 33 | Expected Auxilliary Space : O(N) 34 | 35 | Constraints: 36 | 1 ≤ N ≤ 2*10^5 37 | 1 ≤ start[i] ≤ end[i] ≤ 10^9 38 | -------------------------------------------------------------------------------- /GFG Practice Sets/Greedy Algorithms/Fractional Knapsack/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given weights and values of N items, we need to put these items in a knapsack of capacity W to get the maximum total value in the knapsack. 2 | Note: Unlike 0/1 knapsack, you are allowed to break the item. 3 | 4 | 5 | 6 | Example 1: 7 | 8 | Input: 9 | N = 3, W = 50 10 | values[] = {60,100,120} 11 | weight[] = {10,20,30} 12 | Output: 13 | 240.00 14 | Explanation:Total maximum value of item 15 | we can have is 240.00 from the given 16 | capacity of sack. 17 | Example 2: 18 | 19 | Input: 20 | N = 2, W = 50 21 | values[] = {60,100} 22 | weight[] = {10,20} 23 | Output: 24 | 160.00 25 | Explanation: 26 | Total maximum value of item 27 | we can have is 160.00 from the given 28 | capacity of sack. 29 | 30 | 31 | Your Task : 32 | Complete the function fractionalKnapsack() that receives maximum capacity , array of structure/class and size n and returns a double value representing the maximum value in knapsack. 33 | Note: The details of structure/class is defined in the comments above the given function. 34 | 35 | 36 | Expected Time Complexity : O(NlogN) 37 | Expected Auxilliary Space: O(1) 38 | 39 | 40 | Constraints: 41 | 1 <= N <= 10^5 42 | 1 <= W <= 10^5 43 | -------------------------------------------------------------------------------- /GFG Practice Sets/Greedy Algorithms/Largest Number with Given Sum/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Geek lost the password of his super locker. He remembers the number of digits N as well as the sum S of all the digits of his password. He know that his password is the largest number of N digits that can be made with given sum S. As he is busy doing his homework, help him retrieving his password. 2 | 3 | Example 1: 4 | 5 | Input: 6 | N = 5, S = 12 7 | Output: 8 | 93000 9 | Explanation: 10 | Sum of elements is 12. Largest possible 11 | 5 digit number is 93000 with sum 12. 12 | Example 2: 13 | 14 | Input: 15 | N = 3, S = 29 16 | Output: 17 | -1 18 | Explanation: 19 | There is no such three digit number 20 | whose sum is 29. 21 | Your Task : 22 | You don't need to read input or print anything. Your task is to complete the function largestNumber() which takes 2 integers N and S as input parameters and returns the password in the form of string, else return "-1" in the form of string. 23 | 24 | Constraints: 25 | 1 ≤ N ≤ 10^4 26 | 0 ≤ S ≤ 9*10^4 27 | 28 | Expected Time Complexity : O(N) 29 | Expected Auxilliary Space : O(1) 30 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Armstrong Number/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | For a given 3 digit number, find whether it is armstrong number or not. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. Return "Yes" if it is a armstrong number else return "No". 2 | NOTE: 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371 3 | 4 | Example 1: 5 | 6 | Input: N = 153 7 | Output: "Yes" 8 | Explanation: 153 is an Armstrong number 9 | since 1^3 + 5^3 + 3^3 = 153. 10 | Hence answer is "Yes". 11 | Example 2: 12 | 13 | Input: N = 370 14 | Output: "Yes" 15 | Explanation: 370 is an Armstrong number 16 | since 3^3 + 7^3 + 0^3 = 370. 17 | Hence answer is "Yes". 18 | 19 | Your Task: 20 | You dont need to read input or print anything. Complete the function armstrongNumber() which takes n as input parameter and returns "Yes" if it is a armstrong number else returns "No".. 21 | 22 | Expected Time Complexity: O(1) 23 | Expected Auxiliary Space: O(1) 24 | 25 | Constraints: 26 | 100 ≤ n <1000 27 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Armstrong Number/Solution.java: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | //Initial Template for Java 3 | import java.util.*; 4 | import java.io.*; 5 | class GFG { 6 | public static void main(String args[]) throws IOException { 7 | BufferedReader read = 8 | new BufferedReader(new InputStreamReader(System.in)); 9 | int t = Integer.parseInt(read.readLine()); 10 | while (t-- > 0) { 11 | int n = Integer.parseInt(read.readLine()); 12 | Solution ob = new Solution(); 13 | 14 | System.out.println(ob.armstrongNumber(n)); 15 | } 16 | } 17 | }// } Driver Code Ends 18 | 19 | 20 | //User function Template for Java 21 | class Solution { 22 | static String armstrongNumber(int n){ 23 | // code here 24 | int f=n%10; 25 | int s=((n/10)%10); 26 | int t=(((n/10)/10)%10); 27 | 28 | if( (f*f*f)+(s*s*s)+(t*t*t)==n) 29 | { 30 | return "Yes"; 31 | } 32 | else 33 | { 34 | return "No"; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Binary to Decimal/GFG.java: -------------------------------------------------------------------------------- 1 | //Initial Template for Java 2 | 3 | import java.util.*; 4 | import java.lang.*; 5 | import java.io.*; 6 | class GFG 7 | { 8 | public static void main(String[] args) throws IOException 9 | { 10 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 11 | int T = Integer.parseInt(br.readLine().trim()); 12 | while(T-->0) 13 | { 14 | String str = br.readLine().trim(); 15 | Solution ob = new Solution(); 16 | int ans = ob.binary_to_decimal(str); 17 | System.out.println(ans); 18 | } 19 | } 20 | } 21 | // } Driver Code Ends 22 | 23 | 24 | //User function Template for Java 25 | 26 | class Solution 27 | { 28 | public int binary_to_decimal(String str) 29 | { 30 | // Code here 31 | int m=0; 32 | int k=1; 33 | for(int i=str.length()-1;i>=0;i--){ 34 | m+=(str.charAt(i)-'0')*k; 35 | k=k*2; 36 | } 37 | return m; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Binary to Decimal/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a Binary Number B, find its decimal equivalent. 2 | 3 | 4 | Example 1: 5 | 6 | Input: B = 10001000 7 | Output: 136 8 | Example 2: 9 | 10 | Input: B = 101100 11 | Output: 44 12 | 13 | 14 | Your Task: 15 | You don't need to read or print anything. Your task is to complete the function binary_to_decimal() which takes the binary number as string input parameter and returns its decimal equivalent. 16 | 17 | 18 | Expected Time Complexity: O(K * Log(K)) where K is number of bits in binary number. 19 | Expected Space Complexity: O(1) 20 | 21 | 22 | Constraints: 23 | 1 <= number of bits in binary number <= 16 24 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Calculate Permuation nPr/GFG.java: -------------------------------------------------------------------------------- 1 | //Initial Template for Java 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | class GFG{ 7 | public static void main(String args[])throws IOException 8 | { 9 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 10 | int t = Integer.parseInt(in.readLine()); 11 | while(t-- > 0){ 12 | String a[] = in.readLine().trim().split("\\s+"); 13 | Long n = Long.parseLong(a[0]); 14 | Long r = Long.parseLong(a[1]); 15 | 16 | Solution ob = new Solution(); 17 | System.out.println(ob.nPr(n, r)); 18 | } 19 | } 20 | }// } Driver Code Ends 21 | 22 | 23 | //User function Template for Java 24 | 25 | class Solution{ 26 | static long nPr(long n, long r){ 27 | // code here 28 | long factn=1, factnr=1; 29 | for(long l=1; l<=n; l++){ 30 | factn = factn*l; 31 | } 32 | for(long l=1; l<=(n-r); l++){ 33 | factnr = factnr*l; 34 | } 35 | return factn / factnr; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Calculate Permuation nPr/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Write a program to calculate nPr. nPr represents n permutation r and value of nPr is (n!) / (n-r)!. 2 | 3 | Example 1: 4 | 5 | Input: n = 2, r = 1 6 | Output: 2 7 | Explaination: 2!/(2-1)! = 2!/1! = (2*1)/1 = 2. 8 | Example 2: 9 | 10 | Input: n = 3, r = 3 11 | Output: 6 12 | Explaination: 3!/(3-3)! = 3!/0! = 6/1 = 6. 13 | Your Task: 14 | You do not need to read input or print anything. Your task is to complete the function nPr() which takes n and r as input parameters and returns nPr . 15 | 16 | Expected Time Complexity: O(n) 17 | Expected Auxiliary Space: O(1) 18 | 19 | Constraints: 20 | 1 ≤ n, r ≤ 20 21 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Calculate nCr Combination/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given two integers n and r, find nCr. Since the answer may be very large, calculate the answer modulo 109+7. 2 | 3 | Example 1: 4 | 5 | Input: n = 3, r = 2 6 | Output: 3 7 | Explaination: 3C2 = 3. 8 | 9 | Example 2: 10 | 11 | Input: n = 2, r = 4 12 | Output: 0 13 | Explaination: r is greater than n. 14 | 15 | Your Task: 16 | You do not need to take input or print anything. Your task is to complete the function nCr() which takes n and r as input parameters and returns nCr modulo 109+7.. 17 | 18 | 19 | Expected Time Complexity: O(n*r) 20 | Expected Auxiliary Space: O(r) 21 | 22 | 23 | Constraints: 24 | 1 ≤ n ≤ 1000 25 | 1 ≤ r ≤ 800 26 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Closest Number/GFG.java: -------------------------------------------------------------------------------- 1 | //Initial Template for Java 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | class GFG { 7 | public static void main(String args[]) throws IOException { 8 | BufferedReader read = 9 | new BufferedReader(new InputStreamReader(System.in)); 10 | int t = Integer.parseInt(read.readLine()); 11 | while (t-- > 0) { 12 | String S[] = read.readLine().split(" "); 13 | 14 | int N = Integer.parseInt(S[0]); 15 | int M = Integer.parseInt(S[1]); 16 | 17 | Solution ob = new Solution(); 18 | System.out.println(ob.closestNumber(N,M)); 19 | } 20 | } 21 | }// } Driver Code Ends 22 | 23 | 24 | //User function Template for Java 25 | 26 | class Solution { 27 | static int closestNumber(int N , int M) { 28 | // code here 29 | int q = N / M; 30 | int n1 = q*M; 31 | int n2 = (N*M) > 0 ? (M * (q+1)) :(M*(q-1)); 32 | 33 | if(Math. abs(N-n1)< Math. abs(N-n2)) 34 | return n1; 35 | 36 | return n2; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Closest Number/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given non-zero two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 13 , M = 4 9 | Output: 10 | 12 11 | Explanation: 12 | 12 is the Closest Number to 13 | 13 which is divisible by 4. 14 | Example 2: 15 | 16 | Input: 17 | N = -15 , M = 6 18 | Output: 19 | -18 20 | Explanation: 21 | -12 and -18 are both similarly close to 22 | -15 and divisible by 6. but -18 has 23 | the maximum absolute value. 24 | So, Output is -18 25 | 26 | 27 | Your Task: 28 | You don't need to read input or print anything. Your task is to complete the function closestNumber() which takes an Integer n as input and returns the answer. 29 | 30 | 31 | 32 | Expected Time Complexity: O(1) 33 | Expected Auxiliary Space: O(1) 34 | 35 | 36 | 37 | Constraints: 38 | -10^5 <= N <= 10^5 39 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Digital Sum Palindrome/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a number N.Find if the digit sum(or sum of digits) of N is a Palindrome number or not. 2 | Note:A Palindrome number is a number which stays the same when reversed.Example- 121,131,7 etc. 3 | 4 | Example 1: 5 | 6 | Input: 7 | N=56 8 | Output: 9 | 1 10 | Explanation: 11 | The digit sum of 56 is 5+6=11. 12 | Since, 11 is a palindrome number.Thus, 13 | answer is 1. 14 | Example 2: 15 | 16 | Input: 17 | N=98 18 | Output: 19 | 0 20 | Explanation: 21 | The digit sum of 98 is 9+8=17. 22 | Since 17 is not a palindrome,thus, answer 23 | is 0. 24 | 25 | Your Task: 26 | You don't need to read input or print aything.Your Task is to complete the function isDigitSumPalindrome() which takes a number N as input parameter and returns 1 if the Digit sum of N is a palindrome.Otherwise it returns 0. 27 | 28 | 29 | Expected Time Complexity:O(LogN) 30 | Expected Auxillary Space:O(1) 31 | 32 | Constraints: 33 | 1<=N<=10^9 34 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Factorial/GFG.java: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | import java.io.*; 3 | import java.util.*; 4 | 5 | class GFG 6 | { 7 | public static void main(String args[])throws IOException 8 | { 9 | BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); 10 | int t = Integer.parseInt(read.readLine()); 11 | while(t-- > 0) 12 | { 13 | int N = Integer.parseInt(read.readLine()); 14 | Solution ob = new Solution(); 15 | System.out.println(ob.factorial(N)); 16 | } 17 | } 18 | }// } Driver Code Ends 19 | 20 | 21 | class Solution{ 22 | static long factorial(int N){ 23 | // code here 24 | long fact = 1; //declared as long as we are expected to return the same 25 | for(int i = 1; i <= N; i++) 26 | { 27 | fact = fact * i; 28 | } 29 | return fact; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Factorial/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a positive integer, N. Find the factorial of N. 2 | 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 5 8 | Output: 9 | 120 10 | Explanation: 11 | 5*4*3*2*1 = 120 12 | Example 2: 13 | 14 | Input: 15 | N = 4 16 | Output: 17 | 24 18 | Explanation: 19 | 4*3*2*1 = 24 20 | 21 | Your Task: 22 | You don't need to read input or print anything. Your task is to complete the function factorial() which takes an integer N as input parameters and returns an integer, the factorial of N. 23 | 24 | 25 | Expected Time Complexity: O(N) 26 | Expected Space Complexity: O(1) 27 | 28 | 29 | Constraints: 30 | 0 <= N <= 18 31 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/GCD of Array/Gfg.java: -------------------------------------------------------------------------------- 1 | //Initial Template for Java 2 | import java.io.*; 3 | import java.util.*; 4 | class GfG 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | int t = sc.nextInt(); 10 | while(t-->0) 11 | { 12 | int N = sc.nextInt(); 13 | int Arr[] = new int[N]; 14 | for(int i = 0;i0) 13 | { 14 | int A = sc.nextInt(); 15 | int B = sc.nextInt(); 16 | Solution ob = new Solution(); 17 | System.out.println(ob.gcd(A,B)); 18 | } 19 | } 20 | } // } Driver Code Ends 21 | 22 | 23 | //User function Template for Java 24 | 25 | class Solution 26 | { 27 | public int gcd(int A , int B) 28 | { 29 | //code here 30 | if(B == 0) 31 | { 32 | return A; 33 | } 34 | return gcd(B, A%B); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/GCD/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given two positive integers A and B, find GCD of A and B. 2 | 3 | 4 | Example 1: 5 | 6 | Input: A = 3, B = 6 7 | Output: 3 8 | Explanation: GCD of 3 and 6 is 3 9 | 10 | Example 2: 11 | 12 | Input: A = 1, B = 1 13 | Output: 1 14 | Explanation: GCD of 1 and 1 is 1 15 | 16 | Your Task: 17 | You don't need to read input or print anything. Complete the function gcd() which takes two positive integers as input parameters and returns an integer. 18 | 19 | 20 | Expected Time Complexity: O(log(min(A, B))) 21 | Expected Auxiliary Space: O(1) 22 | 23 | 24 | Constraints: 25 | 1 ≤ A, B ≤ 10^9 26 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Jumping Numbers/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a positive number X. Find the largest Jumping Number smaller than or equal to X. 2 | Jumping Number: A number is called Jumping Number if all adjacent digits in it differ by only 1. All single digit numbers are considered as Jumping Numbers. For example 7, 8987 and 4343456 are Jumping numbers but 796 and 89098 are not. 3 | 4 | 5 | 6 | Example 1: 7 | 8 | Input: 9 | X = 10 10 | Output: 11 | 10 12 | Explanation: 13 | 10 is the largest Jumping Number 14 | possible for X = 10. 15 | Example 2: 16 | 17 | Input: 18 | X = 50 19 | Output: 20 | 45 21 | Explanation: 22 | 45 is the largest Jumping Number 23 | possible for X = 50. 24 | 25 | 26 | Your Task: 27 | You don't need to read input or print anything. Your task is to complete the function jumpingNums() which takes an Integer X as input and returns the largest Jumping Number less than or equal to X. 28 | 29 | 30 | 31 | Expected Time Complexity: O(k), where k is no of jumping numbers 32 | Expected Auxiliary Space: O(k), where k is no of jumping numbers 33 | 34 | 35 | 36 | Constraints: 37 | 1 <= X <= 10^9 38 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/LCM/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given two numbers A and B. The task is to find out their LCM and GCD. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | Input: 8 | A = 5 , B = 10 9 | Output: 10 | 10 5 11 | Explanation: 12 | LCM of 5 and 10 is 10, while 13 | thier GCD is 5. 14 | Example 1: 15 | 16 | Input: 17 | A = 14 , B = 8 18 | Output: 19 | 56 2 20 | Explanation: 21 | LCM of 14 and 8 is 56, while 22 | thier GCD is 2. 23 | 24 | 25 | Your Task: 26 | You don't need to read input or print anything. Your task is to complete the function lcmAndGcd() which takes an Integer N as input and returns a List of two Integers, the required LCM and GCD. 27 | 28 | 29 | 30 | Expected Time Complexity: O(log(min(A, B)) 31 | Expected Auxiliary Space: O(1) 32 | 33 | 34 | 35 | Constraints: 36 | 1 <= A,B <= 10^18 37 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Largest Prime Factor/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a number N, the task is to find the largest prime factor of that number. 2 | 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 5 8 | Output: 9 | 5 10 | Explanation: 11 | 5 has 1 prime factor 12 | i.e 5 only. 13 | Example 2: 14 | 15 | Input: 16 | N = 24 17 | Output: 18 | 3 19 | Explanation: 20 | 24 has 2 prime factors 21 | 3 and 2 in which 3 is 22 | greater 23 | 24 | Your Task: 25 | You don't need to read input or print anything. Your task is to complete the function largestPrimeFactor() which takes an integer N as input parameters and returns an integer, largest prime factor of N. 26 | 27 | 28 | Expected Time Complexity: O(sqrt(N)) 29 | Expected Space Complexity: O(1) 30 | 31 | 32 | Constraints: 33 | 1 <= N <= 10^5 34 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Multiplication Table/GFG.java: -------------------------------------------------------------------------------- 1 | //Initial Template for Java 2 | import java.io.*; 3 | import java.util.*; 4 | 5 | class GFG 6 | { 7 | public static void main(String args[])throws IOException 8 | { 9 | Scanner sc = new Scanner(System.in); 10 | int t = sc.nextInt(); 11 | while(t-- > 0) 12 | { 13 | int N = sc.nextInt(); 14 | Solution ob = new Solution(); 15 | ArrayList ans = ob.getTable(N); 16 | for (Integer val: ans) 17 | System.out.print(val+" "); 18 | System.out.println(); 19 | } 20 | } 21 | } 22 | // } Driver Code Ends 23 | 24 | 25 | //User function Template for Java 26 | class Solution{ 27 | static ArrayList getTable(int N){ 28 | // code here 29 | ArrayList A=new ArrayList<>(); 30 | 31 | for(int i=1;i<=10;i++) 32 | 33 | { 34 | 35 | A.add(N*i); 36 | 37 | } 38 | 39 | return A; 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Multiplication Table/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Print the multiplication table of a given number N. 2 | 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 9 8 | 9 | Output: 10 | 9 18 27 36 45 54 63 72 81 90 11 | 12 | Explanation: 13 | The table of 9 is the output whose 1st 14 | term is 9 and the 10th term is 90. 15 | 16 | Example 2: 17 | 18 | Input: 19 | N = 2 20 | 21 | Output: 22 | 2 4 6 8 10 12 14 16 18 20 23 | 24 | 25 | Your Task: 26 | You don't need to read input. Your task is to complete the function getTable() which takes an integer N as input parameter and returns a list of integers denoting the multiplication of table of N from 1 to 10. 27 | 28 | Expected Time Complexity: O(1) 29 | Expected Auxiliary Space: O(1) 30 | 31 | 32 | Constraints: 33 | 1 <= N <= 10^6 34 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Nth root of M/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | You are given 2 numbers (n , m); the task is to find n√m (nth root of m). 2 | 3 | 4 | Example 1: 5 | 6 | Input: n = 2, m = 9 7 | Output: 3 8 | Explanation: 32 = 9 9 | Example 2: 10 | 11 | Input: n = 3, m = 9 12 | Output: -1 13 | Explanation: 3rd root of 9 is not 14 | integer. 15 | 16 | 17 | Your Task: 18 | You don't need to read or print anyhting. Your task is to complete the function NthRoot() which takes n and m as input parameter and returns the nth root of m. If the root is not integer then returns -1. 19 | 20 | 21 | Expected Time Complexity: O(n* log(m)) 22 | Expected Space Complexity: O(1) 23 | 24 | 25 | Constraints: 26 | 1 <= n <= 30 27 | 1 <= m <= 10^9 28 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Pair Cube Count/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given N, count all ‘a’(>=1) and ‘b’(>=0) that satisfy the condition a^3 + b^3 = N. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 9 9 | Output: 10 | 2 11 | Explanation: 12 | There are two solutions: (a=1, b=2) 13 | and (a=2, b=1). 14 | 15 | Example 2: 16 | 17 | Input: 18 | N = 27 19 | Output: 20 | 1 21 | Explanation: 22 | Thereis only one solution: (a=3, b=0) 23 | 24 | 25 | Your Task: 26 | You don't need to read input or print anything. Your task is to complete the function pairCubeCount() which takes an Integer N as input and returns the answer. 27 | 28 | 29 | 30 | Expected Time Complexity: O(cbrt(N)) 31 | Expected Auxiliary Space: O(1) 32 | 33 | 34 | 35 | Constraints: 36 | 1 <= N <= 10^5 37 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Pairs of Primes/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Find all pairs (sets) of prime numbers (p,q) such that p*q <= n, where n is given number. 2 | 3 | 4 | Example 1: 5 | 6 | Input: n = 4 7 | Output: 2 2 8 | Explanation: Pair (2, 2) which has both prime 9 | numbers as well as satisfying the condition 10 | 2*2 <= 4. 11 | Example 2: 12 | 13 | Input: n = 8 14 | Output: 2 2 2 3 3 2 15 | Explanation: Pairs(2, 2), (2, 3) and (3, 2) 16 | which has both prime numbers and satisfying 17 | the given condition. 18 | 19 | 20 | Your Task: 21 | You don't need to read or print anything. Your task is to complete the function prime_pairs() which takes n as input parameter and returns a list of pairs in ascending order. 22 | 23 | 24 | Expected Time Complexity: O(n2* sqrt(n)) 25 | Expected Space Complexity: O(1) 26 | 27 | 28 | Constraints: 29 | 1 <= n <= 400 30 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Perfect Number/GFG.java: -------------------------------------------------------------------------------- 1 | //Initial Template for Java 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | class GFG { 7 | public static void main(String args[]) throws IOException { 8 | BufferedReader read = 9 | new BufferedReader(new InputStreamReader(System.in)); 10 | int t = Integer.parseInt(read.readLine()); 11 | while (t-- > 0) { 12 | Long N = Long.parseLong(read.readLine()); 13 | 14 | Solution ob = new Solution(); 15 | System.out.println(ob.isPerfectNumber(N)); 16 | } 17 | } 18 | }// } Driver Code Ends 19 | 20 | 21 | //User function Template for Java 22 | 23 | class Solution { 24 | static int isPerfectNumber(Long N) { 25 | // code here 26 | if(N==1) 27 | return 0; 28 | 29 | int sum=1; 30 | for(int i=2;i<=Math.sqrt(N);i++) 31 | { 32 | if(N%i==0) 33 | { 34 | sum+=i; 35 | sum+=N/i; 36 | } 37 | } 38 | return sum==N?1:0; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Perfect Number/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a number N, check if a number is perfect or not. A number is said to be perfect if sum of all its factors excluding the number itself is equal to the number. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | Input: 8 | N = 6 9 | Output: 10 | 1 11 | Explanation: 12 | Factors of 6 are 1, 2, 3 and 6. 13 | Excluding 6 their sum is 6 which 14 | is equal to N itself. So, it's a 15 | Perfect Number. 16 | Example 2: 17 | 18 | Input: 19 | N = 10 20 | Output: 21 | 0 22 | Explanation: 23 | Factors of 10 are 1, 2, 5 and 10. 24 | Excluding 10 their sum is 8 which 25 | is not equal to N itself. So, it's 26 | not a Perfect Number. 27 | 28 | 29 | Your Task: 30 | You don't need to read input or print anything. Your task is to complete the function isPerfectNumber() which takes an Integer N as input and returns 1 if N is a Perfect number else returns 0. 31 | 32 | 33 | 34 | Expected Time Complexity: O(sqrt(N)) 35 | Expected Auxiliary Space: O(1) 36 | 37 | 38 | 39 | Constraints: 40 | 1 <= N <= 10^12 41 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Prime Number/GFG.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class GFG 5 | { 6 | public static void main(String args[])throws IOException 7 | { 8 | BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); 9 | int t = Integer.parseInt(read.readLine()); 10 | while(t-- > 0) 11 | { 12 | int N = Integer.parseInt(read.readLine()); 13 | Solution ob = new Solution(); 14 | System.out.println(ob.isPrime(N)); 15 | } 16 | } 17 | }// } Driver Code Ends 18 | 19 | 20 | class Solution{ 21 | static int isPrime(int N){ 22 | // code here 23 | int j=1; 24 | for(int i=2;i 0) 13 | { 14 | int A = in.nextInt(); 15 | int B = in.nextInt(); 16 | int K = in.nextInt(); 17 | Solution ob = new Solution(); 18 | System.out.println(ob.kthDigit(A,B,K)); 19 | } 20 | } 21 | }// } Driver Code Ends 22 | 23 | 24 | //User function Template for Java 25 | 26 | class Solution{ 27 | static long kthDigit(int A,int B,int K){ 28 | // code here 29 | long num=(long)(Math.pow(A,B)); 30 | long div=num; 31 | long sum=0; 32 | String s=""; 33 | while(div!=0){ 34 | s+=div%10; 35 | div=div/10; 36 | } 37 | String k=""; 38 | k+=(s.charAt(K-1)); 39 | 40 | return Long.parseLong(k); 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Print Kth Digit/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given two numbers A and B, find Kth digit from right of AB. 2 | 3 | 4 | Example 1: 5 | 6 | Input: 7 | A = 3 8 | B = 3 9 | K = 1 10 | Output: 11 | 7 12 | Explanation: 13 | 3^3 = 27 and 1st 14 | digit from right is 15 | 7 16 | Example 2: 17 | 18 | Input: 19 | A = 5 20 | B = 2 21 | K = 2 22 | Output: 23 | 2 24 | Explanation: 25 | 5^2 = 25 and second 26 | digit from right is 27 | 2. 28 | 29 | Your Task: 30 | You don't need to read input or print anything. Your task is to complete the function kthDigit() which takes integers A, B, K as input parameters and returns Kth Digit of AB from right side. 31 | 32 | 33 | Expected Time Complexity: O(log AB) 34 | Expected Space Complexity: O(1) 35 | 36 | 37 | Constraints: 38 | 1 <= A,B <= 15 39 | 1 <=K<= digits in A^B 40 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Print the Pattern/PrintPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | 4 | 5 | class PrintPattern 6 | { 7 | public static void main(String args[]) 8 | { 9 | Scanner sc = new Scanner(System.in); 10 | int t = sc.nextInt(); 11 | while(t>0) 12 | { 13 | int n = sc.nextInt(); 14 | GfG g = new GfG(); 15 | g.printPat(n); 16 | System.out.println(); 17 | 18 | t--; 19 | } 20 | } 21 | }// } Driver Code Ends 22 | 23 | 24 | class GfG 25 | { 26 | void printPat(int n) 27 | { 28 | // Your code here 29 | for(int i=n;i>=1;i--){ 30 | for(int j=n;j>=1;j--){ 31 | int c=0; 32 | while(c0) 12 | { 13 | long n = Long.parseLong(br.readLine().trim()); 14 | Solution ob = new Solution(); 15 | long ans = ob.reverse_digit(n); 16 | System.out.println(ans); 17 | } 18 | } 19 | } 20 | // } Driver Code Ends 21 | 22 | 23 | //User function Template for Java 24 | 25 | class Solution 26 | { 27 | public long reverse_digit(long n) 28 | { 29 | long rev=0; 30 | // Code here 31 | while(n != 0){ 32 | int d = (int)(n%10); 33 | n=n/10; 34 | rev = (10*rev)+d; 35 | } 36 | 37 | return rev; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Reverse the Digit/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given N, reverse the digits of N. 2 | 3 | 4 | Example 1: 5 | 6 | Input: 200 7 | Output: 2 8 | Explanation: By reversing the digts of 9 | number, number will change into 2. 10 | Example 2: 11 | 12 | Input : 122 13 | Output: 221 14 | Explanation: By reversing the digits of 15 | number, number will change into 221. 16 | 17 | 18 | Your Task: 19 | You don't need to read or print anything. Your task is to complete the function reverse_digit() which takes N as input parameter and returns the reversed number. 20 | 21 | 22 | Expected Time Complexity: O(Log(N)) 23 | Expected Space Complexity: O(1) 24 | 25 | 26 | Constraints: 27 | 1 <= N <= 10^15 28 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Series Arithmetic Progression/GFG.java: -------------------------------------------------------------------------------- 1 | // Initial Template for Java 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | class GFG { 7 | public static void main(String args[]) throws IOException { 8 | BufferedReader read = 9 | new BufferedReader(new InputStreamReader(System.in)); 10 | int t = Integer.parseInt(read.readLine()); 11 | while (t-- > 0) { 12 | String[] S = read.readLine().split(" "); 13 | int A1 = Integer.parseInt(S[0]); 14 | int A2 = Integer.parseInt(S[1]); 15 | int N = Integer.parseInt(S[2]); 16 | Solution ob = new Solution(); 17 | System.out.println(ob.nthTermOfAP(A1, A2, N)); 18 | } 19 | } 20 | }// } Driver Code Ends 21 | 22 | 23 | // User function Template for Java 24 | 25 | class Solution { 26 | int nthTermOfAP(int A1, int A2, int N) { 27 | // code here 28 | return A1+(N-1)*(A2-A1); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Series Arithmetic Progression/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given the first 2 terms A1 and A2 of an Arithmetic Series.Find the Nth term of the series. 2 | 3 | Example 1: 4 | 5 | Input: 6 | A1=2 7 | A2=3 8 | N=4 9 | Output: 10 | 5 11 | Explanation: 12 | The series is 2,3,4,5,6.... 13 | Thus,4th term is 5. 14 | Example 2: 15 | 16 | Input: 17 | A1=1 18 | A2=2 19 | N=10 20 | Output: 21 | 10 22 | Explanation: 23 | The series is1,2,3,4,5,6,7,8,9,10,11.. 24 | Thus,10th term is 10. 25 | 26 | Your Task: 27 | You don't need to read input or print anything.Your Task is to complete the function nthTermOfAP() which takes three integers A1,A2 and N as input parameters and returns the nth term of the AP that has A1 and A2 as the first and second terms respectively. 28 | 29 | 30 | Expected Time Complexity:O(1) 31 | Expected Auxillary Space:O(1) 32 | 33 | 34 | Constraints: 35 | -10^4<=A1,A2<=10^4 36 | 1<=N<=10^3 37 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Series Geometric Progression/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given the A and R i,e first term and common ratio of a GP series. Find the Nth term of the series. 2 | 3 | Note: As the answer can be rather large print its modulo 1000000007 (109 + 7). 4 | 5 | Example 1: 6 | 7 | Input: A = 2, R = 2, N = 4 8 | Output: 16 9 | Explanation: The GP series is 10 | 2, 4, 8, 16, 32,... in which 16 11 | is th 4th term. 12 | Example 2: 13 | 14 | Input: A = 4, R = 3, N = 3 15 | Output: 36 16 | Explanation: The GP series is 17 | 4, 12, 36, 108,.. in which 36 is 18 | the 3rd term. 19 | 20 | 21 | Your Task: 22 | You don't need to read or print anything. Your task is to complete the function Nth_term() which takes A, R and N as input parameter ans returns Nth term of the GP series modulo 109 + 7. 23 | 24 | 25 | Expected Time Complexity: O(log2(N)) 26 | Expected Space Complexity: O(1) 27 | 28 | Constraints: 29 | 1 <= A, R, N <= 1000000 30 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Sieve of Eratosthenes/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a number N, calculate the prime numbers up to N using Sieve of Eratosthenes. 2 | 3 | Example 1: 4 | 5 | Input: 6 | N = 10 7 | 8 | Output: 9 | 2 3 5 7 10 | 11 | Explanation: 12 | Prime numbers less than equal to N 13 | are 2 3 5 and 7. 14 | Example 2: 15 | 16 | Input: 17 | N = 35 18 | 19 | Output: 20 | 2 3 5 7 11 13 17 19 23 29 31 21 | 22 | Explanation: 23 | Prime numbers less than equal to 35 are 24 | 2 3 5 7 11 13 17 19 23 29 and 31. 25 | Your Task: 26 | You don't need to read input or print anything. Your task is to complete the function sieveOfEratosthenes() which takes an integer N as an input parameter and return the list of prime numbers less than equal to N. 27 | 28 | Expected Time Complexity: O(NloglogN) 29 | Expected Auxiliary Space: O(N) 30 | 31 | Constraints: 32 | 1<= N <= 10^4 33 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Sum of Primes from 1 to N/GFG.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io.*; 4 | class GFG 5 | { 6 | public static void main(String[] args) throws IOException 7 | { 8 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 9 | int T = Integer.parseInt(br.readLine().trim()); 10 | while(T-->0) 11 | { 12 | int n = Integer.parseInt(br.readLine().trim()); 13 | Solution ob = new Solution(); 14 | long ans = ob.prime_Sum(n); 15 | System.out.println(ans); 16 | } 17 | } 18 | } 19 | // } Driver Code Ends 20 | 21 | 22 | 23 | class Solution 24 | 25 | { 26 | 27 | static boolean isPrime(int n) 28 | 29 | { 30 | 31 | if (n <= 1) 32 | 33 | return false; 34 | 35 | for (int i = 2; i*i <= n; i++) 36 | 37 | if (n % i == 0) 38 | 39 | return false; 40 | 41 | return true; 42 | 43 | } 44 | 45 | public long prime_Sum(int n) 46 | 47 | { 48 | 49 | long count = 0; 50 | 51 | for(int i = 2; i <= n; i++) 52 | 53 | { 54 | 55 | if(isPrime(i)){ 56 | 57 | count += i; 58 | 59 | } 60 | 61 | } 62 | return count; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /GFG Practice Sets/Mathematical/Sum of Primes from 1 to N/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a positive integer N, find the sum of all prime numbers between 1 and N(inclusive). 2 | 3 | 4 | Example 1: 5 | 6 | Input: N = 5 7 | Output: 10 8 | Explanation: 2, 3, and 5 are prime 9 | numbers between 1 and 5(inclusive). 10 | Example 2: 11 | 12 | Input: N = 10 13 | Output: 17 14 | Explanation: 2, 3, 5 and 7 are prime 15 | numbers between 1 and 10(inclusive). 16 | 17 | 18 | Your Task: 19 | You don't need to read or print anyhting. Your task is to complete the function prime_Sum() which takes N as input parameter and returns the sum of all primes between 1 and N(inclusive). 20 | 21 | 22 | Expected Time Complexity: O(N*log(N)) 23 | Expected Space Complexity: O(N) 24 | 25 | Constraints: 26 | 1 <= N <= 1000000 27 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/3 Divisors/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | You have given a list of q queries and for every query, you are given an integer N. The task is to find how many numbers(less than or equal to N) have number of divisors exactly equal to 3. 2 | 3 | Example 1: 4 | 5 | Input: 6 | q = 1 7 | query[0] = 6 8 | Output: 9 | 1 10 | Explanation: 11 | There is only one number 4 which has 12 | exactly three divisors 1, 2 and 4 and 13 | less than equal to 6. 14 | Example 2: 15 | 16 | Input: 17 | q = 2 18 | query[0] = 6 19 | query[1] = 10 20 | Output: 21 | 1 22 | 2 23 | Explanation: 24 | For query 1 it is covered in the 25 | example 1. 26 | query 2: There are two numbers 4 and 9 27 | having exactly 3 divisors and less than 28 | equal to 10. 29 | Your Task: 30 | You don't need to read input or print anything. Your task is to complete the function threeDivisors() which takes an integer q and a list of integer of size q as input parameter and returns the list containing the count of the numbers having exactly 3 divisors for each query. 31 | 32 | Expected Time Complexity: O(NlogN), 33 | Expected Auxiliary Space: O(N), where N is min(10^6,N) 34 | 35 | Constraints : 36 | 1 <= q <= 103 37 | 1 <= query[i] <= 10^12 38 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Angle Between Hands of the Clock/GFG.java: -------------------------------------------------------------------------------- 1 | //Initial Template for Java 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | class GFG { 7 | public static void main(String args[]) throws IOException { 8 | BufferedReader read = 9 | new BufferedReader(new InputStreamReader(System.in)); 10 | int t = Integer.parseInt(read.readLine()); 11 | while (t-- > 0) { 12 | String S[] = read.readLine().split(" "); 13 | 14 | int H = Integer.parseInt(S[0]); 15 | int M = Integer.parseInt(S[1]); 16 | 17 | Solution ob = new Solution(); 18 | System.out.println(ob.getAngle(H,M)); 19 | } 20 | } 21 | }// } Driver Code Ends 22 | 23 | 24 | //User function Template for Java 25 | 26 | class Solution { 27 | static int getAngle(int H , int M) { 28 | // code here 29 | double totalmin, hr, min,diff; 30 | totalmin = H*60+M; 31 | hr= 0.5*totalmin; 32 | min = 6*M; 33 | diff= Math.abs(hr-min); 34 | diff = Math.min( 360 - diff, diff); 35 | return (int)Math.floor((int)diff); 36 | 37 | 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Angle Between Hands of the Clock/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Calculate the angle between the hour hand and minute hand. 2 | 3 | Note: There can be two angles between hands; we need to print a minimum of two. Also, we need to print the floor of the final result angle. For example, if the final angle is 10.61, we need to print 10. 4 | 5 | 6 | Example 1: 7 | 8 | Input: 9 | H = 9 , M = 0 10 | Output: 11 | 90 12 | Explanation: 13 | The minimum angle between hour and minute 14 | hand when the time is 9 is 90 degress. 15 | Example 2: 16 | 17 | Input: 18 | H = 3 , M = 30 19 | Output: 20 | 75 21 | Explanation: 22 | The minimum angle between hour and minute 23 | hand when the time is 3:30 is 75 degress. 24 | 25 | Your Task: 26 | You don't need to read, input, or print anything. Your task is to complete the function getAngle(), which takes 2 Integers H and M as input and returns the answer. 27 | 28 | 29 | Expected Time Complexity: O(1) 30 | Expected Auxiliary Space: O(1) 31 | 32 | 33 | Constraints: 34 | 1 ≤ H ≤ 12 35 | 0 ≤ M < 60 36 | H and M are Integers 37 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Count Squares/GFG.java: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | //Initial Template for Java 3 | 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | class GFG { 8 | public static void main(String args[]) throws IOException { 9 | BufferedReader read = 10 | new BufferedReader(new InputStreamReader(System.in)); 11 | int t = Integer.parseInt(read.readLine()); 12 | while (t-- > 0) { 13 | int N = Integer.parseInt(read.readLine()); 14 | 15 | Solution ob = new Solution(); 16 | System.out.println(ob.countSquares(N)); 17 | } 18 | } 19 | }// } Driver Code Ends 20 | 21 | 22 | //User function Template for Java 23 | 24 | class Solution { 25 | static int countSquares(int N) { 26 | // code here 27 | int count = 0; 28 | for(int i = 1; (i*i) 0) 12 | { 13 | String[] inp=read.readLine().split(" "); 14 | Long X=Long.parseLong(inp[0]); 15 | Long Y=Long.parseLong(inp[1]); 16 | 17 | Solution ob = new Solution(); 18 | System.out.println(ob.isPowerOfAnother(X,Y)); 19 | } 20 | } 21 | } 22 | // } Driver Code Ends 23 | 24 | 25 | //User function Template for Java 26 | class Solution{ 27 | static Long isPowerOfAnother(Long X, Long Y){ 28 | // code here 29 | for(long i=0;i<=Y;i++) 30 | { 31 | if(Math.pow(X,i) == Y) 32 | { 33 | return 1l; 34 | } 35 | } 36 | return 0l; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/If power of another/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given two positive numbers X and Y, check if Y is a power of X or not. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | Input: 8 | X = 2, Y = 8 9 | Output: 10 | 1 11 | Explanation: 12 | 2^3 is equal to 8. 13 | 14 | 15 | Example 2: 16 | 17 | Input: 18 | X = 1, Y = 3 19 | Output: 20 | 0 21 | Explanation: 22 | Any power of 1 is not 23 | equal to 8. 24 | 25 | 26 | Your Task: 27 | 28 | You don't need to read input or print anything. Your task is to complete the function isPowerOfAnother() which takes an integer and returns 1 if y is the power of x, else return 0. 29 | 30 | 31 | 32 | Expected Time Complexity: O(N) 33 | Expected Auxiliary Space: O(1) 34 | 35 | 36 | 37 | Constraints: 38 | 1 <= x <= 10^3 39 | 1 <= y <= 10^8 40 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Is Square/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given four different points in space. Find whether these points can form a square or not. 2 | 3 | 4 | 5 | Example 1: 6 | 7 | Input: 8 | x1 = 20, y1 = 10, x2 = 10, y2 = 20, 9 | x3 = 20, y3 = 20, x4 = 10, y4 = 10 10 | Output: 11 | Yes 12 | Explanation: 13 | The points (20,10), (10,20), (20,20), 14 | (10,10) forms a square. 15 | Example 2: 16 | 17 | Input: 18 | x1 = 2, y1 = 1, x2 = 10, y2 = 20, 19 | x3 = 5, y3 = 6, x4 = 10, y4 = 10 20 | Output: 21 | No 22 | Explanation: 23 | The points (2,1), (10,20), (5,6), 24 | (10,10) doesn't form a square. 25 | 26 | 27 | Your Task: 28 | You don't need to read input or print anything. Your task is to complete the function isSquare() which takes 8 Integers x1, y1, x2, y2, x3, y3, x4, and y4 as input and returns whether these points form a square. 29 | 30 | 31 | 32 | Expected Time Complexity: O(1) 33 | Expected Auxiliary Space: O(1) 34 | 35 | 36 | 37 | Constraints: 38 | -10^3 ≤ x1, y1, x2, y2, x3, y3, x4, y4 ≤ 10^3 39 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Last 2 Digits of Fibbonacci/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a number N. Find the last two digits of the Nth fibonacci number. 2 | Note: If the last two digits are 02, return 2. 3 | 4 | Example 1: 5 | 6 | Input: 7 | N = 13 8 | Output: 9 | 33 10 | Explanation: 11 | The 13th Fibonacci number is 233. 12 | So last two digits are 3 and 3. 13 | Example 2: 14 | 15 | Input: 16 | N = 255 17 | Output: 18 | 70 19 | Explanation: 20 | The 255th fibonacci number is 875715953430- 21 | 18854458033386304178158174356588264390370. 22 | Thus, last two digits are 7 and 0. 23 | 24 | Your Task: 25 | You don't need to read input or print anything.Your task is to complete the function fibonacciDigits() which takes a number N as input parameter and returns the last two digits of the Nth fibonacci number. 26 | 27 | 28 | Expected Time Complexity:O(K) 29 | Expected Auxillary Space:O(K) 30 | K is of the order 102. 31 | 32 | 33 | Constraints: 34 | 1<=N<=10^18 35 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Nth even Fibbonacci Number/GFG.java: -------------------------------------------------------------------------------- 1 | //Initial Template for Java 2 | import java.util.*; 3 | import java.io.*; 4 | class GFG { 5 | public static void main(String args[]) throws IOException { 6 | BufferedReader read = 7 | new BufferedReader(new InputStreamReader(System.in)); 8 | int t = Integer.parseInt(read.readLine()); 9 | while (t-- > 0) { 10 | long n = Integer.parseInt(read.readLine()); 11 | Solution ob = new Solution(); 12 | 13 | System.out.println(ob.nthEvenFibonacci(n)); 14 | } 15 | } 16 | }// } Driver Code Ends 17 | 18 | 19 | //User function Template for Java 20 | 21 | //User function Template for Java 22 | class Solution { 23 | static final long mod = 1000000007; 24 | static long nthEvenFibonacci(long n){ 25 | long b = 1; 26 | long c = 2; 27 | for(long i = 1 ; i < n; i++){ 28 | long nextA = (c + b)%mod; 29 | long nextB = (nextA + c)%mod; 30 | c = (nextA + nextB)%mod; 31 | b = nextB; 32 | } 33 | return c; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Nth even Fibbonacci Number/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a positive integer N, find the Nth Even Fibonacci number. Since the answer can be very large, return the answer modulo 1000000007. 2 | 3 | Example 1: 4 | 5 | Input: n = 1 6 | Output: 2 7 | Explanation: 2 is the first even 8 | number in the fibonacci series. 9 | Example 2: 10 | 11 | Input: n = 2 12 | Output: 8 13 | Explanation: 8 is the second even 14 | number in the fibonacci series. 15 | 16 | Your Task: 17 | You dont need to read input or print anything. Complete the function nthEvenFibonacci() which takes S as input parameter and returns the Nth even fibonacci number. 18 | 19 | Expected Time Complexity: O(n) 20 | Expected Auxiliary Space: O(n) 21 | 22 | Constraints: 23 | 1<= n <=1000 24 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Open Doors/GFG.java: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | //Initial Template for Java 3 | 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | class GFG { 8 | public static void main(String args[]) throws IOException { 9 | BufferedReader read = 10 | new BufferedReader(new InputStreamReader(System.in)); 11 | int t = Integer.parseInt(read.readLine()); 12 | while (t-- > 0) { 13 | Long N = Long.parseLong(read.readLine()); 14 | 15 | Solution ob = new Solution(); 16 | System.out.println(ob.noOfOpenDoors(N)); 17 | } 18 | } 19 | }// } Driver Code Ends 20 | 21 | 22 | //User function Template for Java 23 | 24 | class Solution { 25 | static int noOfOpenDoors(Long N) { 26 | // code here 27 | return (int) Math.sqrt(N); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Squares in a Matrix/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a mxn matrix, count the number of squares in the matrix. 2 | Input: 3 | m = 2, n = 2 4 | Output: 5 | 5 6 | Explanation: 7 | There are a total of 5 squares in a 2*2 8 | matrix. Four 1*1 squares and one 2*2 square. 9 | Example 2: 10 | 11 | Input: 12 | m = 4, n = 3 13 | Output: 14 | 20 15 | Explanation: 16 | There are a total of 20 squares in a 4*3 17 | matrix. It's explained clearly in the 18 | diagram given in the problem statement. 19 | 20 | 21 | Your Task: 22 | You don't need to read input or print anything. Your task is to complete the function squaresInMatrix() which takes 2 Integers m and n as input and returns the number of total squares in a m*n matrix. 23 | 24 | 25 | 26 | Expected Time Complexity: O(1) 27 | Expected Auxiliary Space: O(1) 28 | 29 | 30 | 31 | Constraints: 32 | 1 <= m,n <= 10^4 33 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Trailing 0's in Factorial/GFG.java: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | //Initial Template for Java 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | class GFG 7 | { 8 | public static void main(String args[])throws IOException 9 | { 10 | Scanner sc = new Scanner(System.in); 11 | int t = sc.nextInt(); 12 | while(t-- > 0) 13 | { 14 | int N = sc.nextInt(); 15 | 16 | Solution ob = new Solution(); 17 | int ans = ob.trailingZeroes(N); 18 | System.out.println(ans); 19 | } 20 | } 21 | } 22 | // } Driver Code Ends 23 | 24 | 25 | //User function Template for Java 26 | class Solution{ 27 | static int trailingZeroes(int N){ 28 | // Write your code here 29 | int zero_count = 0; 30 | do 31 | { 32 | N = (N / 5); 33 | zero_count += N; 34 | }while(N / 5 != 0); 35 | return zero_count; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Trailing 0's in Factorial/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | For an integer N find the number of trailing zeroes in N!. 2 | 3 | Example 1: 4 | 5 | Input: 6 | N = 5 7 | Output: 8 | 1 9 | Explanation: 10 | 5! = 120 so the number of trailing zero is 1. 11 | Example 2: 12 | 13 | Input: 14 | N = 4 15 | Output: 16 | 0 17 | Explanation: 18 | 4! = 24 so the number of trailing zero is 0. 19 | Your Task: 20 | You don't need to read input or print anything. Your task is to complete the function trailingZeroes() which take an integer N as an input parameter and returns the count of trailing zeroes in the N!. 21 | 22 | Expected Time Complexity: O(logN) 23 | Expected Auxiliary Space: O(1) 24 | 25 | Constraints: 26 | 1 <= N <= 10^9 27 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Triangular Numbers/GFG.java: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | //Initial Template for Java 3 | 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | class GFG { 8 | public static void main(String args[]) throws IOException { 9 | BufferedReader read = 10 | new BufferedReader(new InputStreamReader(System.in)); 11 | int t = Integer.parseInt(read.readLine()); 12 | while (t-- > 0) { 13 | int N=Integer.parseInt(read.readLine()); 14 | Solution ob = new Solution(); 15 | System.out.println(ob.isTriangular(N)); 16 | } 17 | } 18 | }// } Driver Code Ends 19 | 20 | 21 | //User function Template for Java 22 | 23 | class Solution { 24 | int isTriangular(int N){ 25 | //code here 26 | int k = (int)Math.sqrt(N*2); 27 | k = (int)k*(k+1)/2; 28 | if(k == N) 29 | return 1; 30 | return 0; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /GFG Practice Sets/Puzzles/Triangular Numbers/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a number N.Check whether it is a triangular number or not. 2 | Note: A number is termed as a triangular number if we can represent it in the form of a triangular grid of points such that the points form an equilateral triangle and each row contains as many points as the row number, i.e., the first row has one point, the second row has two points, the third row has three points and so on. 3 | The starting triangular numbers are 1, 3 (1+2), 6 (1+2+3), 10 (1+2+3+4). 4 | 5 | Example 1: 6 | 7 | Input: 8 | N=55 9 | Output: 10 | 1 11 | Explanation: 12 | 55 is a triangular number. 13 | It can be represented in 10 rows. 14 | Example 2: 15 | 16 | Input: 17 | N=56 18 | Output: 19 | 0 20 | Explanation: 21 | 56 is not a triangular number. 22 | 23 | Your Task: 24 | You don't need to read input or print anything. Your task is to complete the function isTriangular() that takes a number N as input parameter and returns 1 if it is a triangular number. Otherwise, it returns 0. 25 | 26 | 27 | Expected Time complexity:O(LogN) 28 | Expected Auxillary Space:O(1) 29 | 30 | 31 | Constraints: 32 | 1<=N<=10^6 33 | -------------------------------------------------------------------------------- /GFG Practice Sets/Recursion/Handshakes/GFG.java: -------------------------------------------------------------------------------- 1 | // { Driver Code Starts 2 | //Initial Template for Java 3 | 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | class GFG 8 | { 9 | public static void main(String args[])throws IOException 10 | { 11 | BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); 12 | int t = Integer.parseInt(read.readLine()); 13 | while(t-- > 0) 14 | { 15 | int N = Integer.parseInt(read.readLine()); 16 | Solution ob = new Solution(); 17 | System.out.println(ob.count(N)); 18 | } 19 | } 20 | }// } Driver Code Ends 21 | 22 | 23 | //User function Template for Java 24 | 25 | class Solution{ 26 | static int count(int N) 27 | { 28 | // code here 29 | if(N%2==1) return 0; 30 | 31 | else if(N==0) 32 | return 1; 33 | int res=0; 34 | 35 | for(int i=0;i0) 12 | { 13 | Solution obj=new Solution(); 14 | 15 | int n,k; 16 | //taking input n and k 17 | n=sc.nextInt(); 18 | k=sc.nextInt(); 19 | 20 | //calling josephus() function 21 | System.out.println(obj.josephus(n,k)); 22 | 23 | 24 | } 25 | 26 | } 27 | } 28 | 29 | // } Driver Code Ends 30 | 31 | 32 | class Solution 33 | { 34 | int checkPosition(int n, int k) 35 | { 36 | if (n == 1) 37 | { 38 | return 0; 39 | } 40 | return (checkPosition(n-1,k) + k) % n; 41 | } 42 | public int josephus(int n, int k) 43 | { 44 | return checkPosition(n,k) + 1; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /GFG Practice Sets/Recursion/Print Pattern/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Print a sequence of numbers starting with N where A[0] = N, without using loop, in which A[i+1] = A[i] - 5, until A[i] > 0. After that A[i+1] = A[i] + 5 repeat it until A[i] = N. 2 | 3 | Example 1: 4 | 5 | Input: N = 16 6 | Output: 16 11 6 1 -4 1 6 11 16 7 | Explaination: The value decreses until it 8 | is greater than 0. After that it increases 9 | and stops when it becomes 16 again. 10 | Example 2: 11 | 12 | Input: N = 10 13 | Output: 10 5 0 5 10 14 | Explaination: It follows the same logic as 15 | per the above example. 16 | Your Task: 17 | You do not need to read input or print anything. Your task is to complete the function pattern() which takes N as input parameters and returns a list containing the pattern. 18 | 19 | Expected Time Complexity: O(N) 20 | Expected Auxiliary Space: O(N) 21 | 22 | Constraints: 23 | 1 ≤ N ≤ 10^4 24 | -------------------------------------------------------------------------------- /GFG Practice Sets/Recursion/Remove Recursive Duplicates/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a string s, remove all its adjacent duplicate characters recursively. 2 | 3 | Example 1: 4 | 5 | Input: 6 | S = "geeksforgeek" 7 | Output: "gksforgk" 8 | Explanation: 9 | g(ee)ksforg(ee)k -> gksforgk​ 10 | 11 | Example 2: 12 | 13 | Input: 14 | S = "acaaabbbacdddd" 15 | Output: "acac" 16 | Explanation: 17 | ac(aaa)(bbb)ac(dddd) -> acac 18 | 19 | Your Task: 20 | You don't need to read input or print anything. Your task is to complete the function remove() which takes the string S as input parameter and returns the resultant string. 21 | Note: For some test cases, the resultant string would be an empty string. For that case, the function should return the empty string only. 22 | 23 | 24 | Expected Time Complexity: O(|S|) 25 | Expected Auxiliary Space: O(|S|) 26 | 27 | 28 | Constraints: 29 | 1<=|S|<=10^5 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Saket Khopkar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /TCS - NINJA/Que 1/Prep.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Prep 3 | { 4 | public static voidmain(String args[]) 5 | { 6 | 7 | String str[]= {"break", "case", "continue", "default", "defer", "else","for", "func", "goto", 8 | "if", "map", "range", "return", "struct", "type", "var"}; 9 | 10 | int flag = 0; 11 | Scanner sc = new Scanner(System.in); 12 | String input=sc.nextLine(); 13 | 14 | for(int i = 0; i<16;i++){ 15 | 16 | if(str[i].equals(input)){ 17 | flag = 1; 18 | break; 19 | } 20 | } 21 | 22 | if(flag==1){ 23 | System.out.println(input+" is a keyword"); 24 | } 25 | else{ 26 | System.out.println(input+" is not a keyword"); 27 | } 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /TCS - NINJA/Que 1/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | One programming language has the following keywords that cannot be used as identifiers: 2 | 3 | break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type, var 4 | 5 | Write a program to find if the given word is a keyword or not 6 | 7 | Test cases 8 | Case 1 9 | Input – defer 10 | Expected Output – defer is a keyword 11 | Case 2 12 | Input – While 13 | Expected Output – while is not a keyword 14 | -------------------------------------------------------------------------------- /TCS - NINJA/Que 2/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main(String[] args) { 5 | HashMap hmap = new HashMap(); 6 | hmap.put('A',10); 7 | hmap.put('B',11); 8 | hmap.put('C',12); 9 | hmap.put('D',13); 10 | hmap.put('E',14); 11 | hmap.put('F',15); 12 | hmap.put('G',16); 13 | hmap.put('a',10); 14 | hmap.put('b',11); 15 | hmap.put('c',12); 16 | hmap.put('d',13); 17 | hmap.put('e',14); 18 | hmap.put('f',15); 19 | hmap.put('g',16); 20 | Scanner sin = new Scanner(System.in); 21 | 22 | String s = sin.nextLine(); 23 | long num=0; 24 | int k=0; 25 | 26 | for(int i=s.length()-1;i>=0;i--) 27 | { 28 | if((s.charAt(i)>='A'&&s.charAt(i)<='Z')||(s.charAt(i)>='a' &&s.charAt(i)<='z')) 29 | { 30 | num = num + hmap.get(s.charAt(i))*(int)Math.pow(17,k++); 31 | } 32 | else 33 | { 34 | num = num+((s.charAt(i)-'0')*(int)Math.pow(17,k++)); 35 | } 36 | } 37 | System.out.println(num); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /TCS - NINJA/Que 2/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Given a maximum of four digit to the base 17 (10 – A, 11 – B, 12 – C, 13 – D … 16 – G} as input, output its decimal value. 2 | 3 | Test Cases 4 | Case 1 5 | Input – 1A 6 | Expected Output – 27 7 | Case 2 8 | Input – 23GF 9 | Expected Output – 10980 10 | -------------------------------------------------------------------------------- /TCS - NINJA/Que 3/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main(String[] args) { 5 | Scanner sin = new Scanner(System.in); 6 | String s=sin.nextLine(); 7 | 8 | long num = 0, num1 = 0; 9 | num=num + s.charAt(0)-'0'; 10 | 11 | for(int i=1;i=1) 37 | System.out.println("The number that you have entered is not prime"); 38 | else 39 | System.out.println("The number that you have entered is prime"); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /TCS - NINJA/Que 4/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Ques. Write a code to check whether no is prime or not. Condition use function check() to find whether entered no is positive or negative ,if negative then enter the no, And if yes pas no as a parameter to prime() and check whether no is prime or not? 2 | 3 | -------------------------------------------------------------------------------- /TCS - NINJA/Que 5/Demo.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Demo { 3 | 4 | public static void main(String[] args) { 5 | 6 | int year; 7 | Scanner scan = new Scanner(System.in); 8 | System.out.println("Enter any Year:"); 9 | year = scan.nextInt(); 10 | scan.close(); 11 | boolean isLeap = false; 12 | 13 | if(year % 4 == 0) 14 | { 15 | if( year % 100 == 0) 16 | { 17 | if ( year % 400 == 0) 18 | isLeap = true; 19 | else 20 | isLeap = false; 21 | } 22 | else 23 | isLeap = true; 24 | } 25 | else { 26 | isLeap = false; 27 | } 28 | 29 | if(isLeap==true) 30 | System.out.println(year + " is a Leap Year."); 31 | else 32 | System.out.println(year + " is not a Leap Year."); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TCS - NINJA/Que 5/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Program to check if a year is Leap Year or not 💖 2 | -------------------------------------------------------------------------------- /TCS/BASIC.c: -------------------------------------------------------------------------------- 1 | // Program to print all value of 2 | // command line argument 3 | // once we get the value from command 4 | // line we can use them to solve our problem. 5 | #include // this is used to print the result using printf 6 | #include // this is used for function atoi() for converting string into int 7 | 8 | // argc tells the number of arguments 9 | // char *argv[] is used to store the command line 10 | //arguments in the pointer to char array i.e string format 11 | int main(int argc, char *argv[]) 12 | { 13 | // means only one argument exist that is file.exe 14 | if (argc == 1) { 15 | printf("No command line argument exist Please provide them first \n"); 16 | return 0; 17 | } 18 | 19 | else { 20 | int i; 21 | // actual arguments starts from index 1 to (argc-1) 22 | for (i = 1; i < argc; i++) { 23 | int value = atoi(argv[i]); 24 | // print value using stdio.h library's printf() function 25 | printf("%d\n", value); 26 | } 27 | return 0; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /TCS/Que 1/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Solution 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | int v=sc.nextInt(); 8 | int w=sc.nextInt(); 9 | float res=((4*v)-w)/2; 10 | if(w>=2 && (w%2==0) && v 2 | using namespace std; 3 | int main() { 4 | string s , d; 5 | cin>>s>>d; 6 | transform(s.begin(),s.end() , s.begin(),::toupper); 7 | transform(d.begin(),d.end() , d.begin(),::toupper); 8 | string arrs[8] = {"TH" , "GA", "IC" , "HA" , "TE", "LU" ,"NI","CA"}; 9 | float arr[8]={800,600,750,900,1400,1200,1100,1500}; 10 | float res=0; 11 | int st ,ed; 12 | for(int i=0;i<8;i++) 13 | { 14 | if(s==arrs[i]) 15 | st=i; 16 | 17 | if(d==arrs[i]) 18 | ed=i; 19 | } 20 | if(st==ed) 21 | { 22 | cout<<" INVALID INPUT"; 23 | return 0; 24 | } 25 | else 26 | { 27 | int i=st+1; 28 | cout<0) 20 | { 21 | atebanana =m/k; 22 | m=m%k; 23 | } 24 | if(j>0) 25 | { 26 | atepeanut = p/j; 27 | p=p%j; 28 | } 29 | n=n-atebanana-atepeanut; 30 | if((m!=0) || (p!=0)) 31 | n=n-1; 32 | System.out.println("Number of Monkeys left on the Tree: "+n); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /TCS/Que 14/chainmarket.cpp: -------------------------------------------------------------------------------- 1 | using namespace std; 2 | int main() 3 | { 4 | string par; 5 | cin >> par; 6 | string x; 7 | cin >> x; 8 | if (x == "N") { 9 | cout << "TOTAL MEMBERS:1\n"; 10 | cout << "COMISSION DETAILS\n"; 11 | cout << par << ":250 INR\n"; 12 | } else { 13 | string child; 14 | cin >> child; 15 | vectorv; 16 | string temp = ""; 17 | for (int i = 0; i < child.length(); i++) { 18 | if (child[i] == ',') { 19 | v.push_back(temp); 20 | temp = ""; 21 | } 22 | else if (child[i] != ' ') 23 | temp += child[i]; 24 | } 25 | v.push_back(temp); 26 | cout << "TOTAL MEMBERS:" << v.size() + 1 << "\n"; 27 | cout << "COMISSION DETAILS\n"; 28 | cout << par << ":" << v.size() * 500 << " INR\n"; 29 | for (auto a : v) { 30 | cout << a << ":" << "250 INR\n"; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /TCS/Que 15/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem Statement 2 | 3 | FULLY AUTOMATIC VENDING MACHINE – dispenses your cuppa on just press of button. A vending machine can serve range of products as follows: 4 | 5 | Coffee 6 | 7 | Espresso Coffee 8 | Cappuccino Coffee 9 | Latte Coffee 10 | Tea 11 | 12 | Plain Tea 13 | Assam Tea 14 | Ginger Tea 15 | Cardamom Tea 16 | Masala Tea 17 | Lemon Tea 18 | Green Tea 19 | Organic Darjeeling Tea 20 | Soups 21 | 22 | Hot and Sour Soup 23 | Veg Corn Soup 24 | Tomato Soup 25 | Spicy Tomato Soup 26 | Beverages 27 | 28 | Hot Chocolate Drink 29 | Badam Drink 30 | Badam-Pista Drink 31 | Write a program to take input for main menu & sub menu and display the name of sub menu selected in the following format (enter the first letter to select main menu): 32 | 33 | Welcome to CCD 34 | 35 | Enjoy your 36 | 37 | Example 1: 38 | 39 | Input: 40 | c 41 | 1 42 | Output 43 | Welcome to CCD! 44 | Enjoy your Espresso Coffee! 45 | Example 2: 46 | 47 | Input 48 | t 49 | 9 50 | Output 51 | INVALID OUTPUT! 52 | -------------------------------------------------------------------------------- /TCS/Que 15/vendingmachine.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | string c[3]={"Espresso Coffee","Cappuccino Coffee","Latte Coffee"}; 7 | 8 | string t[8]={"Plain Tea","Assam Tea","Ginger Tea","Cardamom Tea","Masala Tea","Lemon Tea","Green Tea","Organic Darjeeling Tea"}; 9 | 10 | string s[4]={"Hot and Sour Soup","Veg Corn Soup","Tomato Soup","Spicy Tomato Soup"}; 11 | 12 | string b[3]={"Hot Chocolate Drink","Badam Drink","Badam-Pista Drink"}; 13 | 14 | 15 | char ch; 16 | int n; 17 | 18 | string res = ""; 19 | 20 | cin>>ch>>n; 21 | 22 | if(ch=='c' and n <= 3) 23 | res = c[n-1]; 24 | 25 | else if(ch=='t' and n <= 8) 26 | res = t[n-1]; 27 | 28 | else if(ch=='s' and n <= 4) 29 | res = s[n-1]; 30 | 31 | else if(ch=='b' and n <= 3) 32 | res = b[n-1]; 33 | 34 | else res = "Invalid Input"; 35 | 36 | if(res != "Invalid Input" ) 37 | cout<<"Welcome to CCD!\nEnjoy your "< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int x, count =0, flag = 0, fee_sum=0; 7 | 8 | while(cin>>x){ 9 | 10 | if(x<=0 and x>120) 11 | { 12 | flag=1; 13 | break; 14 | } 15 | 16 | count++; 17 | 18 | if(x<17) fee_sum +=200; 19 | 20 | else if(x>=17 and x<=40) fee_sum += 400; 21 | 22 | else fee_sum += 300; 23 | } 24 | 25 | if(count >20 and flag != 1) 26 | cout<<"INVALID INPUT"; 27 | 28 | else cout<<"Total income : "<=1) 38 | System.out.println("Entered number is not a prime number"); 39 | else 40 | System.out.println("Entered number is a prime number"); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /TCS/Que 18/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Ques. Write a code to check whether no is prime or not. Condition use function check() to find whether entered no is positive or negative ,if negative then enter the no, And if yes pas no as a parameter to prime() and check whether no is prime or not? 2 | 3 | Whether the number is positive or not, if it is negative then print the message “please enter the positive number” 4 | It is positive then call the function prime and check whether the take positive number is prime or not. 5 | -------------------------------------------------------------------------------- /TCS/Que 19/Main.java: -------------------------------------------------------------------------------- 1 | //Java program to find 15th element of the series 2 | class Main 3 | { 4 | public static void main(String[] args) 5 | { 6 | int a = 7, b = 0,c; 7 | System.out.println("Series :"); 8 | for(int i = 1 ; i < 8 ; i++) 9 | { 10 | c = a * b; 11 | System.out.print(c+" "+(c-b)+" "); 12 | b++; 13 | } 14 | c = a * b; 15 | System.out.println(c); 16 | System.out.print("15th element of the series is = "+c); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /TCS/Que 19/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Find the 15th term of the series? 2 | 3 | 0,0,7,6,14,12,21,18, 28 4 | 5 | Explanation : In this series the odd term is increment of 7 {0, 7, 14, 21, 28, 35 – – – – – – } 6 | 7 | And even term is a increment of 6 {0, 6, 12, 18, 24, 30 – – – – – – } 8 | -------------------------------------------------------------------------------- /TCS/Que 2/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | TCS NQT Coding Question 2022 – September Day 1 – Slot 1 2 | Problem Statement – Given a string S(input consisting) of ‘*’ and ‘#’. The length of the string is variable. The task is to find the minimum number of ‘*’ or ‘#’ to make it a valid string. The string is considered valid if the number of ‘*’ and ‘#’ are equal. The ‘*’ and ‘#’ can be at any position in the string. 3 | 4 | 5 | 6 | Note : The output will be a positive or negative integer based on number of ‘*’ and ‘#’ in the input string. 7 | 8 | (*>#): positive integer 9 | (#>*): negative integer 10 | (#=*): 0 11 | 12 | 13 | Example 1: 14 | 15 | Input 1: 16 | 17 | ###*** -> Value of S 18 | Output : 19 | 20 | 0 → number of * and # are equal 21 | -------------------------------------------------------------------------------- /TCS/Que 2/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Solution 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | String str=sc.next(); 8 | int count1=0,count2=0; 9 | for(int i=0;i 100) { 12 | trainee[i][j] = 0; 13 | } 14 | } 15 | } 16 | for(int i = 0; i < 3; i++) { 17 | for(int j = 0; j < 3; j++) { 18 | average[i] = average[i] + trainee[j][i]; 19 | } 20 | average[i] = average[i] / 3; 21 | } 22 | for(int i = 0; i < 3; i++) { 23 | if(average[i] > max) { 24 | max = average[i]; 25 | } 26 | } 27 | for(int i = 0; i < 3; i++) { 28 | if(average[i] == max) { 29 | System.out.println("Trainee Number : " + (i + 1)); 30 | } 31 | if(average[i] <70) { 32 | System.out.print("Trainee is Unfit"); 33 | } 34 | } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /TCS/Que 9/washingmachine_weight.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void calculateTime (int n) 5 | { 6 | if (n == 0) 7 | cout << "Time Estimated : 0 Minutes"; 8 | 9 | else if (n > 0 && n <= 2000) 10 | cout << "Time Estimated : 25 Minutes"; 11 | 12 | else if (n > 2000 && n <= 4000) 13 | cout << "Time Estimated : 35 Minutes"; 14 | 15 | else if (n > 4000 && n <= 7000) 16 | cout << "Time Estimated : 45 Minutes"; 17 | 18 | else 19 | cout << "INVALID INPUT"; 20 | } 21 | 22 | int main () 23 | { 24 | int Weight; 25 | cin >> Weight; 26 | 27 | calculateTime (Weight); 28 | return 0; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 1/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | static int size=4; 5 | public static boolean areSame(int A[][], int B[][]) 6 | { 7 | int i,j; 8 | for(i=0;i0) 10 | { 11 | if((no%10)%2!=0) 12 | sum=sum+no%10; 13 | no=no/10; 14 | } 15 | System.out.println(sum); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 13 - Transpose/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Transpose of a Matrix 2 | In this article, we will discuss the java program to print the transpose of a matrix. Converting rows of matrix into columns and columns of a matrix into rows is called transpose of a matrix. In other words, transpose of matrix[][] is obtained by changing matrix[i][j] to matrix[j][i]. 3 | 4 | Transpose of matrix in java 5 | Transpose of a Matrix in Java 6 | A transpose of a matrix is the matrix flipped over its diagonal i.e. the row and column indices of the matrix are switched. An example of this is given as follows − 7 | 8 | Matrix = 9 | 1 2 3 10 | 4 5 6 11 | 7 8 9 12 | 13 | Transpose of Matrix = 14 | 1 4 7 15 | 2 5 8 16 | 3 6 9 17 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 13 - Transpose/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Solution 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | System.out.println("Enter the number of rows of matrix"); 8 | int row=sc.nextInt(); 9 | System.out.println("Enter the number of columns of a matrix"); 10 | int col=sc.nextInt(); 11 | int matrix[][]=new int[row][col]; 12 | System.out.println("Enter the elements of a matrix"); 13 | for(int i=0;i0) 28 | { 29 | finalPrice[count]= price[i] * distance[i]; 30 | count++; 31 | } 32 | } 33 | 34 | for(int i=0;imax) 26 | { 27 | max=sum; 28 | index=i+1; 29 | } 30 | } 31 | 32 | System.out.println("Row "+index+" is having the maximum sum : "+max); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 17/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem: in this first line you are required to take the value of m and n as the number of rows and columns of matrix, then you are required to take the input elements of array. 2 | As an output you are required to print the sum of each row then the row having the maximum sum. 3 | Test Case : 4 | Input : 3 3 5 | 1 2 3 6 | 4 5 6 7 | 7 8 9 8 | Output : 9 | Row 1 : 6 10 | Row 2 : 15 11 | Row 3 : 24 12 | Row 3 is having the maximum sum : 24 13 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 18/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | String str=sc.nextLine(); 8 | str=str.trim(); 9 | String arr[]=str.split(" "); 10 | int count=0; 11 | for(int i=0;i0) 15 | count++; 16 | } 17 | System.out.println(count); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 18/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem: You are required to count the number of words in a sentence. 2 | You are required to pass all the test cases 3 | 4 | Test Cases : 5 | Test Case : 1 6 | Input : welcome to the world 7 | Output : 4 8 | 9 | Test Case : 2 10 | Input : [space] say hello 11 | Output : 2 12 | 13 | Test Case : 3 14 | Input : To get pass you need to study hard [space] [space] 15 | Output : 8 16 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 19/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | String str=sc.nextLine(); 8 | String target=sc.next(); 9 | str=str.trim(); 10 | String arr[]=str.split(" "); 11 | int count=0; 12 | for(int i=0;istart && list[i]0) 10 | { 11 | int rem=num%10; 12 | if(rem%2==0) 13 | even=even+rem; 14 | else 15 | odd=odd+rem; 16 | num=num/10; 17 | } 18 | 19 | System.out.println(Math.abs(even-odd)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 23/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem: you are given a number, and you have to extract the key by finding the difference between the sum of the even and odd numbers of the input. 2 | 3 | Test Case : 4 | 5 | Input : 24319587 6 | 7 | Output : 11 8 | 9 | Explanation : odd terms : 3 + 1 + 9 + 5 + 7 = 25 10 | 11 | even terms : 2 + 4 + 8 = 14 12 | 13 | output : 11 (25-14) 14 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 24/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | int m=sc.nextInt(); 8 | int n=sc.nextInt(); 9 | int arr[][]=new int[m][n]; 10 | 11 | for(int i=0;imax) 26 | { 27 | max=sum; 28 | index=i+1; 29 | } 30 | } 31 | 32 | System.out.println("Row "+index+" is having the maximum sum : "+max); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 24/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem: in this first line you are required to take the value of m and n as the number of rows and columns of matrix, then you are required to take the input elements of array. 2 | 3 | As an output you are required to print the sum of each row then the row having the maximum sum. 4 | 5 | Test Case : 6 | 7 | Input : 3 3 8 | 9 | 1 2 3 10 | 11 | 4 5 6 12 | 13 | 7 8 9 14 | 15 | Output : 16 | 17 | Row 1 : 6 18 | Row 2 : 15 19 | Row 3 : 24 20 | 21 | Row 3 is having the maximum sum : 24 22 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 25/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | int n=sc.nextInt(); 8 | int arr[]=new int[n]; 9 | 10 | for(int i=0;i0) 11 | { 12 | int rem=num%10; 13 | if(rem==target) 14 | count++; 15 | num=num/10; 16 | } 17 | System.out.println(count); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 26/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Problem: in this first line you are required to take the input of an integer number, and in the second line you are required to input the target element 2 | 3 | You are required to print the number of time target element occured in the integer 4 | 5 | Test Case : 6 | 7 | Input : 734139 8 | 9 | 3 10 | 11 | Output : 12 | 13 | 2 14 | 15 | Explanation : 16 | 17 | 3 occured 2 times in the integer. 18 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 3/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | int row=sc.nextInt(); 8 | int col=sc.nextInt(); 9 | int a,ans=-1; 10 | 11 | int sum=0,max=Integer.MAX_VALUE; 12 | for(int i=0;isum) 21 | { 22 | max=sum; 23 | ans=i+1; 24 | } 25 | } 26 | System.out.println(ans); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 3/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | PrepSter Wipro NLTH Coding Question – 3 2 | Ques: Given an n-by-n matrix of 0’s and 1’s where all 1’s in each row come before all 0’s, find the most efficient way to return the row with the maximum number of 0’s. 3 | 4 | {1,1,1,1}, 5 | {1,1,0,0}, 6 | {1,0,0,0}, 7 | {1,1,0,0}, 8 | 9 | NOTE:- Please comment down the code in other languages as well below – 10 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 4/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Main 3 | { 4 | public static void pythagoreanTriplets(int limit) 5 | { 6 | int a,b,c=0; 7 | int m=2; 8 | while(climit) 16 | break; 17 | System.out.println(a+" "+b+" "+c); 18 | } 19 | m++; 20 | } 21 | } 22 | public static void main(String[] args) 23 | { 24 | int limit=20; 25 | pythagoreanTriplets(limit); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 5 - GCD/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Program to calculate GCD of Two Numbers 2 | In this article, we will discuss about GCD of Two Numbers in Java. GCD of Two Numbers or integers (where integers not equal to zero) is largest positive integer that divides each of the integers. For example, GCD of 12 and 16 is 4 , where factors of 12==>1,2,4,6,12 3 | factors of 16==>1,2,4,8,16 4 | common factors are 1,2,4 5 | greatest common factor is 4. 6 | 7 | Program to check a number is prime or not in C++ 8 | Algorithm for GCD of Two Numbers: 9 | Take Two Numbers num1 and num2 as input. 10 | Initialize a variable i to minimum of num1 and num2 and loop until i is greater than or equal to 1. 11 | Check if i divides num1 and num2 completely or not. If divides completely then break the loop. 12 | Now , print the value of i. 13 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 5 - GCD/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Solution 3 | { 4 | public static void main(String[] args) { 5 | Scanner sc=new Scanner(System.in); 6 | int num1=sc.nextInt(); 7 | int num2=sc.nextInt(); 8 | int min=num1=1;i--) 10 | if((num1%i==0) && (num2%i==0)) 11 | break; 12 | System.out.println("GCD of "+num1+" and "+num2+" is "+i); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 6 - GCD of 3 no/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Program to calculate GCD of three numbers 2 | In this article we will discuss about GCD of three numbers. GCD of three integers (where integers not equal to zero) is largest positive integer that divides each of the three integers. For example GCD of 12 , 16 , 22 is 2 where factors of 12==>1,2,3,4,6,12 3 | factors of 16==>1,2,4,8,16 4 | factors of 22==>1,2,11,22 5 | common factors==>1,2 6 | greatest common factor==>2 7 | 8 | GCD of three numbers in java 9 | Algorithm for GCD of three numbers: 10 | Take Three Numbers num1, num2 and num3 as input. 11 | Initialize a variable i to minimum of num1 ,num2, num3 and loop until i is greater than or equal to 1. 12 | Check if i divides num1, num2, num3 completely or not. If divides completely then break the loop. 13 | Now , print the value of i. 14 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 6 - GCD of 3 no/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Solution 3 | { 4 | public static void main(String[] args) { 5 | Scanner sc=new Scanner(System.in); 6 | int num1=sc.nextInt(); 7 | int num2=sc.nextInt(); 8 | int num3=sc.nextInt(); 9 | int min=Math.min(num1,Math.min(num2,num3)); 10 | int i; 11 | for(i=min;i>=1;i--) 12 | if((num1%i==0) && (num2%i==0) && (num3%i==0)) 13 | break; 14 | System.out.println("GCD of "+num1+", "+num2+" and "+num3+" is "+i); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 7 - Prime/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Check if Number is Prime or Not 2 | A number is said to be Prime Number if and only if it is divisible by one and itself. In this article, we will discuss about the program to check if the given number is prime or not. 3 | 4 | Sample Input : 17 5 | Sample Output : 17 is a Prime Number 6 | Prime Number in java 7 | Steps to Check if the number is Prime or not 8 | Step 1: Take input as a function parameter. 9 | Step 2: Check if the number is less than to 1 or not. If it is less than 1, return false. 10 | Step 3: Check if no is less than or equal to 3 or not. If it is less than or equal to 3, return true. 11 | Step 4: Now, check if no is completely divisible by 2 or 3 or not. If it is complete divisible by 2 or 3 then return false. 12 | Step 5: Now, initialize variable i to 5 and loop until i*i is less than or equal to or not and every time increment i with 6. 13 | Step 6: Check if the number if completely divisible by i or i+2 or not. If it is completely divisible then return false. 14 | Step 7: If the control reach to end then return true. 15 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 7 - Prime/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Solution 3 | { 4 | public static boolean isPrime(int no) 5 | { 6 | if(no<=1) 7 | return false; 8 | if(no<=3) 9 | return true; 10 | if((no%2==0) || (no%3==0)) 11 | return false; 12 | for(int i=5;i*i<=no;i=i+6) 13 | if((no%i==0) || (no%(i+2)==0)) 14 | return false; 15 | return true; 16 | } 17 | public static void main(String[] args) 18 | { 19 | Scanner sc=new Scanner(System.in); 20 | System.out.println("Enter a Number "); 21 | int no=sc.nextInt(); 22 | if(isPrime(no)) 23 | System.out.println(no+" is a Prime Number"); 24 | else 25 | System.out.println(no+" is not a Prime Number"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 8 - Distinct elements of Array/Problem Statement.txt: -------------------------------------------------------------------------------- 1 | Distinct Elements of an Array in Java 2 | Distinct elements of are nothing but a unique (non-duplicate) elements present in the given array. In this article, we will discuss how to find the distinct elements of an array in java. 3 | 4 | Sample Input : 1, 3, 7 11, 3, 8, 11 5 | Sample Output : 1, 3, 7, 11, 8 6 | Distinct elements of an array in java 7 | Steps to find distinct elements of an array 8 | Take the size of array as input n. 9 | Take n space separated elements of an array as input. 10 | Traverse the array from the beginning. 11 | Check if the current element is found in the array again. 12 | If it is found, then do not print that element. 13 | Else, print that element and continue. 14 | -------------------------------------------------------------------------------- /Wipro Elite NLTH/Que 8 - Distinct elements of Array/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Solution 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | System.out.println("Enter size of an array"); 8 | int n=sc.nextInt(); //size of an array 9 | System.out.println("Enter elements of an array"); 10 | int arr[]=new int[n]; 11 | 12 | for(int i=0;i