├── .github └── ISSUE_TEMPLATE │ └── tcs-digital.md ├── Alphanumeric_Palindrome.java ├── Anagram.java ├── Arrays_Rotation.java ├── BetweenNumber.java ├── Common_Prefix.java ├── Count_Elimination_Character.java ├── DivideArrayByGCD.java ├── Football_Team.java ├── KeywordOrNot.java ├── MaximumDifference.java ├── MinimumEvenInArray.java ├── Minimum_Number.java ├── Network_Marketing.java ├── Palindrome_Substring.java ├── Pangram.java ├── Pipe.java ├── README.md ├── ReplaceNum.java ├── Rotate_Matrix.java ├── Strange.java ├── SubSumOfDiag2Matrix.java ├── Sum_NumberCombination.java ├── Uncommon_Element.java └── test.java /.github/ISSUE_TEMPLATE/tcs-digital.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: TCS-Digital 3 | about: Java Question soltuions 4 | title: '' 5 | labels: JAVA of TCS Digital 6 | assignees: Arijit-SE 7 | 8 | --- 9 | 10 | Java solutions of TCS digital exam is not too much difficult. If you understand the logic then you easily solve it. Follow my solutions and go through it. 11 | -------------------------------------------------------------------------------- /Alphanumeric_Palindrome.java: -------------------------------------------------------------------------------- 1 | /*Write a program to check if a given string is a valid palindrome, considering only alphanumeric characters and ignoring cases. 2 | A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. 3 | 4 | Your program should return True if the input string is a valid palindrome, and False otherwise. 5 | 6 | For example, if the input string is "A man, a plan, a canal: Panama", the program should return True, as the string is a 7 | valid palindrome when considering only alphanumeric characters ("amanaplanacanalpanama"). 8 | 9 | Write a program that takes a string as input and determines whether it is a valid palindrome or not, considering alphanumeric 10 | characters and ignoring cases. 11 | 12 | Sample input: "A man, a plan, a canal: Panama" 13 | Sample output: True 14 | 15 | Sample input: "hello world" 16 | Sample output: False 17 | */ 18 | 19 | import java.util.*; 20 | public class Alphanumeric_Palindrome { 21 | public static void main(String[] args) { 22 | Scanner sc = new Scanner(System.in); 23 | String s = sc.nextLine().toLowerCase(); 24 | char[] ch = new char[0]; 25 | for (int i = 0; i < s.length(); i++) { 26 | if(Character.isLetter(s.charAt(i))) 27 | { 28 | ch = Arrays.copyOf(ch,ch.length+1); 29 | ch[ch.length-1] = s.charAt(i); 30 | } 31 | } 32 | //System.out.println(Arrays.toString(ch)); 33 | boolean check = true; 34 | for (int i = 0; i < ch.length; i++) { 35 | if(ch[i] != ch[ch.length-1-i]) 36 | { 37 | check = false; 38 | break; 39 | } 40 | } 41 | if(check == true) 42 | { 43 | System.out.println("True"); 44 | } 45 | else 46 | { 47 | System.out.println("False"); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Anagram.java: -------------------------------------------------------------------------------- 1 | /*Write a program to check if two strings are anagrams of each other. An anagram is a word or phrase formed by rearranging 2 | the letters of another word or phrase. Your program should return True if the two input strings are anagrams, and False 3 | otherwise. 4 | 5 | For example, if the input strings are "listen" and "silent", the program should return True, as both strings can be rearranged 6 | to form the same letters. 7 | 8 | Write a program that takes two strings as input and determines whether they are anagrams or not. 9 | 10 | Sample input1: 11 | ---------------- 12 | listen 13 | silent 14 | 15 | Sample output1: 16 | --------------- 17 | True 18 | 19 | Sample input2: 20 | --------------- 21 | hello 22 | world 23 | 24 | Sample output2: 25 | --------------- 26 | False 27 | 28 | */ 29 | 30 | import java.util.*; 31 | public class Anagram { 32 | public static void main(String[] args) { 33 | Scanner sc = new Scanner(System.in); 34 | String s1 = sc.nextLine(); 35 | char[] ch1 = s1.toCharArray(); 36 | 37 | String s2 = sc.nextLine(); 38 | char[] ch2 = s2.toCharArray(); 39 | int check = 0; 40 | for (int i = 0; i < ch1.length; i++) { 41 | for (int j = 0; j < ch2.length; j++) { 42 | if(ch1[i] == ch2[j]) 43 | { 44 | check ++; 45 | break; 46 | } 47 | } 48 | } 49 | if(ch1.length == ch2.length && check == ch1.length) 50 | { 51 | System.out.println("True"); 52 | } 53 | else 54 | { 55 | System.out.println("False"); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Arrays_Rotation.java: -------------------------------------------------------------------------------- 1 | /* Take last digit of an array and placed it at front. 2 | * The process run upto n times by user input 3 | * 4 | * Input : 5 | * ---------------- 6 | * 5 7 | * 1 2 3 4 5 8 | * 3 9 | * 10 | * Output : 11 | * ---------------- 12 | * 3 4 5 1 2 13 | */ 14 | 15 | import java.util.*; 16 | public class Arrays_Rotation { 17 | public static void main(String[] args) { 18 | Scanner sc = new Scanner(System.in); 19 | int n = sc.nextInt(); 20 | int[] arr = new int[n]; 21 | for (int i = 0; i < arr.length; i++) 22 | { 23 | arr[i] = sc.nextInt(); // [1,2,3,4,5] 24 | } 25 | int k = sc.nextInt(); // k = 2 26 | for (int i = 0; i < k; i++) 27 | { 28 | int num = arr[n-1]; // num = 5,4 29 | for (int j = n-1; j>0; j--) { 30 | arr[j] = arr[j-1]; 31 | } 32 | arr[0] = num; 33 | } 34 | for (int i = 0; i < arr.length; i++) { 35 | System.out.print(arr[i]+" "); // [4,5,1,2,3] 36 | } 37 | 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /BetweenNumber.java: -------------------------------------------------------------------------------- 1 | /* Count of Nonrepeat-digited number (10, 12, 13,.............) between a range 2 | * Note: 3 | * Repeat-Digit :- 11, 22, 33, ............. 4 | * You should ignore the digit and count the other numbers 5 | * 6 | * Input : 7 | * --------------------- 8 | 10 9 | 25 10 | 11 | Output : 12 | ------------------- 13 | 14 14 | */ 15 | 16 | import java.util.*; 17 | public class BetweenNumber { 18 | public static void main(String[] args) { 19 | Scanner sc= new Scanner(System.in); 20 | int a = sc.nextInt(); 21 | int b = sc.nextInt(); 22 | int count=0; 23 | for (int i = a; i <=b; i++) { 24 | String s = Integer.toString(i); 25 | int num=0; 26 | for (int j = 0; j < s.length()-1; j++) 27 | { 28 | for (int k = j+1; k < s.length(); k++) 29 | { 30 | if(s.charAt(j)==s.charAt(k)) 31 | { 32 | num++; 33 | } 34 | } 35 | } 36 | if(num==0) 37 | { 38 | count++; 39 | } 40 | } 41 | System.out.println(count); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Common_Prefix.java: -------------------------------------------------------------------------------- 1 | /*Write a program to find the longest common prefix among an array of strings. The longest common prefix is the string that 2 | all strings in the array have in common as a prefix. 3 | 4 | For example, given the array of strings ["flower", "flow", "flight"], the program should return the string "fl" as it is the 5 | longest common prefix among all the strings. 6 | 7 | Write a program that takes an array of strings as input and returns the longest common prefix among them. */ 8 | 9 | import java.util.*; 10 | public class Common_Prefix { 11 | public static void main(String[] args) { 12 | Scanner sc = new Scanner(System.in); 13 | System.out.print("Number of words: "); 14 | int n = sc.nextInt();sc.nextLine(); 15 | String[] arr = new String[n]; 16 | for (int i = 0; i < n; i++) 17 | { 18 | arr[i] = sc.nextLine(); 19 | } 20 | 21 | String ans = ""; 22 | int min = arr[0].length(); 23 | for (int i = 1; i < arr.length; i++) { 24 | if(arr[i].length()0; k--) { 60 | if(arr[k-1] - arr[k] != diff) 61 | { 62 | c += arr[k-1] - arr[k] - diff; 63 | arr[k - 1] = arr[k] + diff; 64 | } 65 | } 66 | System.out.println(c); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /DivideArrayByGCD.java: -------------------------------------------------------------------------------- 1 | /* Take input an array, then divide the whole array by there gcd. 2 | * Input-1 3 | * ---------- 4 | * 5 5 | * 36 12 9 48 15 6 | * 7 | * Output-1 8 | * ---------- 9 | * 12 4 3 16 5 10 | * 11 | * Input-2 12 | * ---------- 13 | * 5 14 | * 31 51 47 63 24 15 | * 16 | * Output-2 17 | * ---------- 18 | * 31 51 47 63 24 19 | */ 20 | 21 | import java.util.*; 22 | 23 | public class DivideArrayByGCD { 24 | public static void main(String[] args) { 25 | Scanner sc = new Scanner(System.in); 26 | int n = sc.nextInt(); 27 | int[] arr = new int[n]; 28 | 29 | // Take array 30 | 31 | for (int i = 0; i < n; i++) { 32 | arr[i] = sc.nextInt(); 33 | } 34 | 35 | // Find minimum number 36 | 37 | int min = arr[0]; 38 | for (int i = 0; i < arr.length; i++) { 39 | if(arr[i] 17 | Here, Team1 is the name of the home team. Team2 is the name of the away team. Score represents the score of the match, (M-N), 18 | where M represents the score of the home team and N represents the score of the away team. 19 | 20 | * Output 21 | 22 | The output should have two lines. 23 | 24 | The first line should contain a character representing the name of the leader team. The second line containing an integer 25 | representing the points won by the leader team. 26 | 27 | * Time Limit 28 | 29 | 1 30 | 31 | Example 1 32 | 33 | Input : 34 | 35 | 3 36 | A B 2-1 37 | B C 5-6 38 | C A 2-1 39 | 40 | Output : 41 | 42 | C 43 | 6 44 | 45 | Explanation: 46 | 47 | A has won 1 match : 3 points 48 | B has won 0 match: 0 points 49 | C has won 2 match : 6 points 50 | 51 | */ 52 | 53 | import java.util.*; 54 | 55 | public class Football_Team { 56 | public static void main(String[] args) { 57 | Scanner sc = new Scanner(System.in); 58 | int n = sc.nextInt();sc.nextLine(); // Number of players 59 | int com = n*(n-1)/2; 60 | Player[] match = new Player[com]; 61 | for (int i = 0; i < com; i++) { 62 | char t1 = sc.next().charAt(0); // Opener 63 | char t2 = sc.next().charAt(0); // 2nd player 64 | String score = sc.nextLine(); // Score chart 65 | match[i] = new Player(t1, t2, score); 66 | } 67 | int point[] = new int[n]; 68 | for (int i = 0; i < n; i++) { 69 | point[i] = 0; 70 | } 71 | for (int i = 0; i < com; i++) { 72 | String[] num = match[i].score.trim().split("-"); 73 | int c = Integer.parseInt(num[0]) - Integer.parseInt(num[1]); 74 | if (c > 0) { 75 | char ch = match[i].a; 76 | point[(int) ch - 65] += 3; 77 | } 78 | else if (c < 0) 79 | { 80 | char ch = match[i].b; 81 | point[(int) ch - 65] += 3; 82 | } 83 | else 84 | { 85 | char ch1 = match[i].a; 86 | point[(int) ch1 - 65] += 1; 87 | char ch2 = match[i].b; 88 | point[(int) ch2 - 65] += 1; 89 | } 90 | } 91 | int max = point[0]; 92 | char Ch = 'A'; 93 | for (int i = 1; i < n; i++) { 94 | if (point[i] > max) { 95 | max = point[i]; 96 | Ch = (char) (i + 65); 97 | } 98 | } 99 | System.out.println(Ch); 100 | System.out.println(max); 101 | } 102 | } 103 | 104 | class Player { 105 | char a; 106 | char b; 107 | String score; 108 | 109 | public Player(char a, char b, String score) 110 | { 111 | this.a = a; 112 | this.b = b; 113 | this.score = score; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /KeywordOrNot.java: -------------------------------------------------------------------------------- 1 | /* Find a taken String is a keyword or not 2 | * Note : 3 | * "break", "case", "continue", "default", "defer", "else", "for", "func", "goto", "if", "map", "range","return", 4 | "struct", "type", "var" --- these are the keywords 5 | */ 6 | 7 | import java.util.*; 8 | public class KeywordOrNot { 9 | public static void main(String[] args) { 10 | Scanner sc = new Scanner(System.in); 11 | String[] keywords = {"break", "case", "continue", "default", "defer", "else", "for", "func", 12 | "goto", "if", "map", "range","return", "struct", "type", "var"}; 13 | String str = sc.nextLine(); 14 | int c=0; 15 | for (int i = 0; i < keywords.length; i++) { 16 | if(keywords[i].equalsIgnoreCase(str)) 17 | { 18 | System.out.println("Keyword"); 19 | break; 20 | } 21 | else{ 22 | c++; 23 | } 24 | } 25 | if(c==keywords.length) 26 | { 27 | System.out.println("Not a keyword"); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /MaximumDifference.java: -------------------------------------------------------------------------------- 1 | /*You are given an array of integers. Write a program to find the maximum difference between any two numbers in the array. 2 | 3 | For example, given the array [2, 9, 5, 1, 7, 4], the maximum difference between any two numbers is 9 - 1 = 8. 4 | 5 | Write a program that takes an array as input and returns the maximum difference between any two numbers in the array. */ 6 | 7 | import java.util.*; 8 | public class MaximumDifference { 9 | public static void main(String[] args) { 10 | Scanner sc = new Scanner(System.in); 11 | System.out.print("Size of array: "); 12 | int n = sc.nextInt(); 13 | int[] arr = new int[n]; 14 | for (int i = 0; i < arr.length; i++) { 15 | arr[i] = sc.nextInt(); 16 | } 17 | Arrays.sort(arr); 18 | int min = arr[0]; 19 | int max = arr[arr.length-1]; 20 | System.out.println("The maximum difference: "+(max-min)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /MinimumEvenInArray.java: -------------------------------------------------------------------------------- 1 | /* Find the even number in an array which is minimum. If not found then print -1. 2 | * 3 | * Input: 4 | * --------------- 5 | * 5 6 | * 5 4 7 3 9 12 7 | * 8 | * Output: 9 | * --------------- 10 | * 4 11 | */ 12 | 13 | import java.util.*; 14 | public class MinimumEvenInArray { 15 | public static void main(String[] args) { 16 | Scanner sc = new Scanner(System.in); 17 | int n = sc.nextInt(); 18 | int[] arr = new int[n]; 19 | for (int i = 0; i < arr.length; i++) { 20 | arr[i] = sc.nextInt(); 21 | } 22 | Arrays.sort(arr); 23 | boolean f = true; 24 | for (int i = 0; i < arr.length; i++) { 25 | if(arr[i]%2==0) 26 | { 27 | System.out.println(arr[i]); 28 | f = false; 29 | break; 30 | } 31 | } 32 | if(f==true) 33 | { 34 | System.out.println(-1); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Minimum_Number.java: -------------------------------------------------------------------------------- 1 | /* Write a program to find the minimum value of B for a given value of A. If we multiply the digits of B we get the exact 2 | value of A. 3 | 4 | Result B must be contains more than 1 digit. 5 | 6 | * Input : 10 7 | * Output : 25 8 | * Explanation : 2*5 = 10 and this is minimum value of B 9 | 10 | * Input : 100 11 | * Output : 455 12 | * Explanation : 4*5*5 = 100 13 | */ 14 | 15 | import java.util.*; 16 | public class Minimum_Number 17 | { 18 | public static void main(String args[]) 19 | { 20 | Scanner sc = new Scanner(System.in); 21 | int A = sc.nextInt(); 22 | int B = findMinB(A); 23 | if(B>0) 24 | { 25 | System.out.println(B); 26 | } 27 | else 28 | { 29 | System.out.println("No value found"); 30 | } 31 | } 32 | public static int findMinB(int A) 33 | { 34 | int B = 10; 35 | while (true) { 36 | String b = Integer.toString(B); 37 | int product = 1; 38 | for (int i = 0; i < b.length(); i++) 39 | { 40 | int c = Integer.parseInt(String.valueOf(b.charAt(i))); 41 | if (c!=0 && A % c == 0) 42 | { 43 | product = product * c; 44 | } 45 | else 46 | { 47 | break; 48 | } 49 | } 50 | if (product == A) 51 | { 52 | return B; 53 | } else if (product > A) { 54 | return 0; 55 | } 56 | B++; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Network_Marketing.java: -------------------------------------------------------------------------------- 1 | /*In network marketing a person who sells some merchandise becomes the root node of a so-called network marketing tree. 2 | When the first person inducts someone into the network marketing tree the first person becomes the supervisor. 3 | Likewise, if this new recruit inducts a third person into the network marketing tree the new recruit becomes 4 | supervisor of the third person. The tree grows so on and so forth. Whenever anybody in the network marketing tree 5 | makes a profit, they have to pass a certain percentage of that profit to their supervisor. The percentage of the 6 | profit passed to the superior will be rounded to the nearest integer, and hence will always be an integer at each 7 | level. Calculate the profit of the person at the root of the network marketing tree coming from a particular 8 | individual at Nth level in the tree. 9 | 10 | * Constraints : 11 | 12 | 10) 87 | { 88 | System.out.println(last); 89 | } 90 | else 91 | { 92 | System.out.println("NA"); 93 | } 94 | } 95 | public static int LastCal(int n, int a, int p) 96 | { 97 | for (int i = 0; i < n-1; i++) 98 | { 99 | a = a*p/100; 100 | } 101 | return a; 102 | } 103 | } -------------------------------------------------------------------------------- /Palindrome_Substring.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution 4 | { 5 | 6 | public static boolean isPalindrome (String s) 7 | { 8 | 9 | if (s.length () == 1) 10 | return true; 11 | StringBuilder sb = new StringBuilder (s); 12 | sb = sb.reverse (); 13 | String rev = new String (sb); 14 | return s.equals (rev); 15 | } 16 | 17 | public static void main (String[]args) 18 | { 19 | Scanner sc = new Scanner (System.in); 20 | String str = sc.next (); 21 | 22 | int len = str.length (); 23 | String str1 = "", str2 = "", str3 = ""; 24 | 25 | boolean flag = false; 26 | 27 | for (int i = 1; i < len - 1; i++) 28 | { 29 | 30 | str1 = str.substring (0, i); 31 | if (isPalindrome (str1)) 32 | { 33 | for (int j = 1; j < len - i; j++) 34 | { 35 | str2 = str.substring (i, i + j); 36 | str3 = str.substring (i + j, len); 37 | if (isPalindrome (str2) && isPalindrome (str3)) 38 | { 39 | System.out.println (str1 + "\n" + str2 + "\n" + str3); 40 | flag = true; 41 | break; 42 | } 43 | } 44 | if (flag) 45 | break; 46 | } 47 | } 48 | if (flag == false) 49 | System.out.println ("Impossible"); 50 | } 51 | } -------------------------------------------------------------------------------- /Pangram.java: -------------------------------------------------------------------------------- 1 | /* Problem Description 2 | =============================================================================================== 3 | Given the input string find out whether the string is pangram or not. 4 | 5 | Note: a string is said to be pangram if it contains all the alphabets from 'a' to 'z'. 6 | 7 | Constraints 8 | ------------------------- 9 | 1<= length <= 100000 10 | 11 | Input 12 | -------------------------- 13 | There is only one line in the input string. 14 | 15 | Output 16 | --------------------------- 17 | Yes if its pangram. No if not a pangram. 18 | 19 | Time Limit 20 | -------------------------- 21 | 1 22 | 23 | Explanation 24 | ================================================================================================ 25 | Example 1 26 | 27 | Input 28 | ------------------------- 29 | The quick brown fox jumps over the lazy dog 30 | 31 | Output 32 | ---------------------- 33 | Yes 34 | 35 | Explanation: 36 | ---------------------------- 37 | It contains all characters from 'a' to 'z'. 38 | 39 | */ 40 | import java.util.*; 41 | public class Pangram 42 | { 43 | public static void main(String[] args) { 44 | Scanner sc = new Scanner(System.in); 45 | String s = sc.nextLine().toUpperCase(); 46 | boolean ans = PangramCheck(s); 47 | if(ans==true) 48 | { 49 | System.out.println("Yes"); 50 | } 51 | else 52 | { 53 | System.out.println("No"); 54 | } 55 | } 56 | public static boolean PangramCheck(String s) 57 | { 58 | int[] ascii = new int[26]; 59 | 60 | for (int i = 0; i < ascii.length; i++) { 61 | ascii[i] = 0; 62 | } 63 | for (int i = 0; i < s.length(); i++) 64 | { 65 | char a = 'A'; char z = 'Z'; 66 | if(s.charAt(i)>=a && s.charAt(i)<=z) 67 | { 68 | ascii[(int)s.charAt(i)-65]++; 69 | } 70 | } 71 | for (int i = 0; i < ascii.length; i++) { 72 | if(ascii[i]==0) 73 | { 74 | return false; 75 | } 76 | } 77 | return true; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Pipe.java: -------------------------------------------------------------------------------- 1 | /*A plumber wants to check whether a pipe junction where N incoming pipes and M outgoing pipes are balanced, and, if not, 2 | needs to balance the junction by adding an input pipe or an output pipe of a suitable capacity. At the junction, there are a 3 | set of input pipes and a set of output pipes. Each pipe has a rated capacity and an actual capacity. The actual capacity for 4 | each pipe is lower than the rated capacity by a constant R, the "rust factor", which depends on the material of the pipe, and 5 | is the same for all the pipes at the junction. 6 | 7 | For example, if the rated capacity is 65, and R is 2, the actual capacity is 63. 8 | 9 | A junction is balanced if the sum of the actual capacities of the input pipes is the same as the sum of the actual capacities 10 | of the output pipes. If it is not balanced, the plumber needs to add one input pipe or one output pipe to balance the junction, 11 | and determine the rated capacity of that added pipe. 12 | 13 | Here we have N incoming pipes and M outgoing pipes. The incoming pipes may be of different rated capacities. Similarly, the 14 | outgoing pipes may also be of different rated capacities. 15 | 16 | Find the rated capacity of the pipe required to make the junction balanced. If the combined actual capacity of the incoming 17 | pipes is more than the combined actual capacity of the outgoing pipes then the plumber will need to add an outgoing pipe. 18 | Conversely, if the combined actual capacity of the incoming pipes is less than the combined actual capacity of the outgoing 19 | pipes then the plumber will need to add an incoming pipe. 20 | 21 | If an outgoing pipe is added then denote its rated capacity as a negative number. If an incoming pipe is added then denote its 22 | rated capacity as a positive number. 23 | 24 | Input 25 | 26 | The input has three lines 27 | The first line contains three space separated integers N M R denoting the number of incoming pipes, outgoing pipes and rust 28 | factor respectively. 29 | The second line contains N space separated integers denoting the rated capacity of each incoming pipe. 30 | The third line contains M space separated integers denoting the rated capacity of each outgoing pipe. 31 | 32 | Output 33 | 34 | Print the rated capacity of the pipe required to balance the junction OR print "BALANCED" if the junction is already balanced. 35 | 36 | 37 | Example 1 : 38 | ------------------------------------- 39 | Input 40 | ~~~~~~~~~~~ 41 | 3 3 2 42 | 85 75 95 43 | 70 80 45 44 | 45 | Output 46 | ~~~~~~~~~~~~ 47 | -62 48 | 49 | Explanation 50 | ~~~~~~~~~~~~~~~ 51 | There are 3 input pipes, 3 output pipes, and the rust factor is 2. The rated capacities of the input pip are 52 | 85,75 and 95 respectively. 53 | The-rated capacities of the output pipes are 70,80 and 45 respectively. 54 | Here, it will be required to add an outgoing pipe with rated capacity 62. Hence the output is -62. 55 | 56 | 57 | Example 2 : 58 | ------------------------------------ 59 | Input 60 | ~~~~~~~~~~~~~~ 61 | 5 6 1 62 | 10 26 33 40 50 63 | 20 7 53 25 45 10 64 | 65 | Output 66 | ~~~~~~~~~~~~~~~~ 67 | BALANCED 68 | 69 | */ 70 | 71 | import java.util.*; 72 | public class Pipe { 73 | public static void main(String[] args) { 74 | Scanner sc = new Scanner(System.in); 75 | int sum1=0, sum2=0; 76 | int M = sc.nextInt(); 77 | int N = sc.nextInt(); 78 | int R = sc.nextInt(); 79 | int[] m = new int[M]; 80 | for (int i = 0; i < M; i++) { 81 | m[i] = sc.nextInt(); 82 | sum1 += m[i]-R; 83 | } 84 | int[] n = new int[N]; 85 | for (int i = 0; i < N; i++) { 86 | n[i] = sc.nextInt(); 87 | sum2 += n[i]-R; 88 | } 89 | int val = sum1 - sum2; 90 | if(val>0) 91 | { 92 | System.out.println(-(val+R)); 93 | } 94 | else if(val == 0) 95 | { 96 | System.out.println("Balanced"); 97 | } 98 | else 99 | { 100 | System.out.println(val+R); 101 | } 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TCS-Digital-JAVACoding 2 | Here I have solved different type of Coding questions of Java programming of TCS Digital exam 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /ReplaceNum.java: -------------------------------------------------------------------------------- 1 | /* Substract any number between 0 to 1000000 each digit by 9 2 | * Input : 25843 3 | * Output : 74156 4 | * 5 | * Input : 58462314 6 | * Output : Given integer is out of range 7 | */ 8 | 9 | import java.util.*; 10 | public class ReplaceNum { 11 | public static void main(String[] args) { 12 | Scanner sc = new Scanner(System.in); 13 | int n = sc.nextInt(); 14 | int rem, val=0; 15 | if(n>=0 && n<=1000000) 16 | { 17 | int m=0; 18 | while(n>0) 19 | { 20 | rem = n%10; 21 | val = (int)((9-rem)*Math.pow(10,m)+val); 22 | n = n/10; 23 | m++; 24 | } 25 | 26 | System.out.println(val); 27 | } 28 | else 29 | { 30 | System.out.println("Given integer is out of range"); 31 | } 32 | } 33 | } 34 | 35 | /* ANOTHER SOLUTION 36 | * --------------------------- 37 | import java.util.*; 38 | public class test { 39 | public static void main(String[] args) { 40 | Scanner sc = new Scanner(System.in); 41 | String num = sc.nextLine(); 42 | int val = 0; 43 | if(Integer.parseInt(num)>=0 && Integer.parseInt(num)<=1000000) 44 | { 45 | for (int i = 0; i < num.length(); i++) 46 | { 47 | int k = 9-Integer.parseInt(String.valueOf(num.charAt(i))); 48 | val = val*10 + k; 49 | } 50 | System.out.println(val); 51 | } 52 | else 53 | { 54 | 55 | } 56 | } 57 | } 58 | 59 | */ 60 | -------------------------------------------------------------------------------- /Rotate_Matrix.java: -------------------------------------------------------------------------------- 1 | /*You are given an square 2D matrix representing an image, rotate the image by 90 degrees (clockwise). 2 | You have to rotate the image in place, which means you have to modify the input 2D matrix directly. 3 | DO NOT allocate another 2D matrix and do the rotation. 4 | 5 | Input : 6 | ---------------- 7 | 1 2 3 8 | 4 5 6 9 | 7 8 9 10 | 11 | Output : 12 | -------------- 13 | 7 4 1 14 | 8 5 2 15 | 9 6 3 16 | */ 17 | import java.util.*; 18 | public class Rotate_Matrix 19 | { 20 | public static void main(String[] args) 21 | { 22 | Scanner sc = new Scanner(System.in); 23 | System.out.print("Row : "); 24 | int row = sc.nextInt();sc.nextLine(); 25 | System.out.print("Column :"); 26 | int col = sc.nextInt();sc.nextLine(); 27 | int[][] arr = new int[row][col]; 28 | int[][] ans = new int[col][row]; 29 | int[][] ans2 = new int[col][row]; 30 | for (int i = 0; i < row; i++) { 31 | for (int j = 0; j < col; j++) 32 | { 33 | arr[i][j] = sc.nextInt(); 34 | } 35 | } 36 | System.out.println("-------------------------"); 37 | System.out.println("Transpose Matrix : "); 38 | for (int i = 0; i < col; i++) { 39 | for (int j = 0; j < row; j++) 40 | { 41 | ans[i][j] = arr[j][i]; 42 | System.out.print(ans[i][j]+" "); 43 | } 44 | System.out.println(); 45 | } 46 | System.out.println("----------------------------"); 47 | for (int i = 0; i < col; i++) { 48 | for (int j = 0; j < row; j++) 49 | { 50 | ans2[i][j] = ans[i][row-j-1]; 51 | System.out.print(ans2[i][j]+" "); 52 | } 53 | System.out.println(); 54 | } 55 | 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Strange.java: -------------------------------------------------------------------------------- 1 | /*Question: 2 | 3 | Numbers are everywhere around us. We deal with different types of numbers on a daily basis. There are real numbers, whole 4 | numbers, natural numbers, etc. Another kind of numbers is called strange numbers, which has the following properties: 5 | 6 | • A strange number is an integer number 'N' which has factors that are prime numbers. 7 | 8 | • The square root of the number 'N' should be less than the greatest prime factor of 'N'. 9 | 10 | The task here is to find out if the given number 'N' is "Strange" Or "Not Strange" 11 | 12 | Example 1: 13 | 14 | Input: 15 | ------------------- 16 | 15 17 | 18 | Output: 19 | ------------------- 20 | Strange 21 | ============================================ 22 | 23 | Example 2 : 24 | 25 | Input: 26 | ------------------------ 27 | 25 28 | 29 | Output: 30 | --------------------------- 31 | Not Strange 32 | 33 | */ 34 | 35 | import java.util.*; 36 | public class Strange 37 | { 38 | public static void main(String[] args) { 39 | Scanner sc = new Scanner(System.in); 40 | int N = sc.nextInt(); 41 | double root = Math.sqrt(N); 42 | int[] fac = new int[0]; 43 | for (int i = 1; i < N; i++) { 44 | if(N % i == 0 && isPrime(i) == true) 45 | { 46 | fac = Arrays.copyOf(fac, fac.length+1); 47 | fac[fac.length-1] = i; 48 | Arrays.sort(fac); 49 | } 50 | } 51 | if(fac[fac.length-1] > root) 52 | { 53 | System.out.println("Strange"); 54 | } 55 | else 56 | { 57 | System.out.println("Not Strange"); 58 | } 59 | } 60 | public static boolean isPrime(int n) 61 | { 62 | if(n > 2) 63 | { 64 | for (int i = 2; i < n; i++) { 65 | if(n % i == 0) 66 | { 67 | return false; 68 | } 69 | } 70 | return true; 71 | } 72 | else if(n == 2) 73 | { 74 | return true; 75 | } 76 | return false; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /SubSumOfDiag2Matrix.java: -------------------------------------------------------------------------------- 1 | /* Sum of substraction of two diagonals of a matrix 2 | 3 | Enter number of row/col :3 4 | 1 2 3 5 | 4 5 6 6 | 9 8 9 7 | The substraction of the sums of each diagonals : 2 8 | 9 | * |(1+5+9)-(3+5+9)| = 2 10 | */ 11 | 12 | import java.util.*; 13 | public class SubSumOfDiag2Matrix 14 | { 15 | public static void main(String[] args) { 16 | Scanner sc = new Scanner(System.in); 17 | System.out.print("Enter number of row/col :"); 18 | int n = sc.nextInt(); 19 | int[][] matrix = new int[n][n]; 20 | int sum1=0, sum2=0,sub; 21 | for (int r = 0; r < matrix.length; r++) 22 | { 23 | for (int c = 0; c < matrix.length; c++) 24 | { 25 | matrix[r][c]=sc.nextInt(); 26 | if(r==c) 27 | { 28 | sum1 = sum1+matrix[r][c]; 29 | } 30 | if(r==(matrix.length-1-c)) 31 | { 32 | sum2 = sum2+matrix[r][c]; 33 | } 34 | } 35 | } 36 | if(sum1>sum2) 37 | { 38 | sub = sum1-sum2; 39 | } 40 | else 41 | { 42 | sub = sum2-sum1; 43 | } 44 | System.out.println("The substraction of the sums of each diagonals : "+sub); 45 | } 46 | } -------------------------------------------------------------------------------- /Sum_NumberCombination.java: -------------------------------------------------------------------------------- 1 | /*Write a program to find the sum of all characters and all possible contiguous character combinations from the given string. 2 | 3 | Consider the string S1="321". 4 | 5 | All characters and combinations of contiguous characters from the above string are: 3,2,1,32,21,321. 6 | Adding all the numbers (3+2+1+32+21+321), we get 380 as the result. 7 | 8 | Instructions: 9 | 10 | The system does not allow any kind of hard coded input value/values. 11 | 12 | The written program code by the candidate will be verified against the inputs that are supplied from the system. 13 | 14 | For more clarification, please read the following points carefully till the end. 15 | 16 | Example 1 17 | ======================================== 18 | Input: 321 19 | Output: 380 20 | 21 | Example 2: 22 | ======================================== 23 | Input: 12 24 | Output: 15 25 | 26 | Explanation 27 | ----------------- 28 | In Example2 it possible combinations are 1,2,12. Sum=1+2+12=15 29 | 30 | */ 31 | 32 | import java.util.*; 33 | public class Sum_NumberCombination 34 | { 35 | public static void main(String[] args) 36 | { 37 | Scanner sc = new Scanner(System.in); 38 | String s = sc.nextLine(); 39 | int sum = 0 ; 40 | for (int i = 0; i < s.length(); i++) { 41 | for (int j = i; j < s.length(); j++) { 42 | String str = s.substring(i,j+1); 43 | sum += Integer.parseInt(str); 44 | } 45 | } 46 | System.out.println(sum); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Uncommon_Element.java: -------------------------------------------------------------------------------- 1 | /* Take two set of numbers of two specific lengths A and B. 2 | * Find those numbers of set A which are not in set B. 3 | * 4 | * Input 5 | * ---------------------- 6 | * 8 <-------------- Lenght of set A 7 | * 5 <-------------- Length of set B 8 | * 2 5 8 9 4 6 1 7 <-------------- set A 9 | * 1 5 3 8 4 <-------------- set B 10 | * 11 | * Output 12 | * ---------------------- 13 | * 2 9 6 7 <-------------- elements of A not in B 14 | */ 15 | 16 | import java.util.*; 17 | public class Uncommon_Element { 18 | public static void main(String[] args) { 19 | Scanner sc = new Scanner(System.in); 20 | int m = sc.nextInt(); 21 | int n = sc.nextInt(); 22 | int[] A = new int[m]; 23 | int[] B = new int[n]; 24 | for (int i = 0; i < m; i++) 25 | { 26 | A[i]=sc.nextInt(); 27 | } 28 | for (int i = 0; i < n; i++) { 29 | B[i] = sc.nextInt(); 30 | } 31 | int c=0; 32 | for (int i = 0; i < m; i++) { 33 | for (int j = 0; j < B.length; j++) { 34 | if(A[i]!=B[j]) 35 | { 36 | c++; 37 | } 38 | } 39 | if(c == n) 40 | { 41 | System.out.print(A[i]+" "); 42 | } 43 | c = 0; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /test.java: -------------------------------------------------------------------------------- 1 | public class test { 2 | public static void main(String[] args) { 3 | System.out.println("Hi Arijit, Welcome to java !!!!!!!"); 4 | } 5 | } 6 | --------------------------------------------------------------------------------