Posts

Showing posts from January, 2021

Factor Count - Integer

Factor Count - Integer    A number N is passed as input. The program should print the count of factors for the number N.  Input Format:  The first line will contain the number N.  Output Format:  The first line will contain the count of factors for the number N.  Boundary Condition(s):  1 <= N < 10^7  Example Input/Output 1:  Input:  100  Output:  9  Example Input/Output 2:  Input:  6  Output:  4 Program: n = int(input()) f = 0 for i in range(1,n+1):     if n%i == 0:         f += 1  print(f)        

Check Sorted Order

Check Sorted Order   The program must accept N integers which are sorted in ascending order except one integer. But if that single integer R is reversed, the entire array will be in sorted order. The program must print the first integer that must be reversed so that the entire array will be sorted in ascending order.  Boundary Condition(s):  2 <= N <= 20  Input Format:  The first line contains N. The second line contains N integer values separated by a space.  Output Format:  The first line contains the integer value R.  Example Input/Output 1:  Input:  5  10 71 20 30 33  Output:  71  Explanation:  When 71 is reversed the array becomes sorted as below. 10 17 20 30 33  Example Input/Output 2:  Input:  6  10 20 30 33 64 58  Output:  64  Example Input/Output 3:  Input:  6  10 20 30 33 67 58  Output:  58  Program: n = int(input()) l = list(ma...

Uncommon Integers - Two Arrays

Uncommon Integers - Two Arrays  Problem: The program must accept two integer arrays of sizes M and N as the input. The program must print all the uncommon integer(s) (ascending order) in the given two arrays. If there is no uncommon integer in the given two arrays then the program must print -1 as the output.  Boundary Condition(s):  1 <= M, N <= 10000  Input Format:  The first line contains the values of M and N separated by a space. The second line contains M integers separated by space(s). The third line contains N integers separated by space(s).  Output Format:  The first line contains either all the uncommon integer(s) separated by space(s) or -1.  Example Input/Output 1:  Input:  5 5  87 5 6 12 91  5 1 7 87 8  Output:  1 6 7 8 12 91  Explanation:  The uncommon integers in the first array are 6 12 91 The uncommon integers in the second array are 1 7 8 The uncommon integers from both the arrays are...

Smallest Substring

Smallest Substring   Problem: The program must accept a string S as the input. The program must print the smallest substring containing all the distinct characters of the string S in it. Note: If more than one such substring is found, print the first occurring substring.  Boundary Condition(s):  1 <= Length of string S <= 100  Input Format:  The first line contains the value of S.  Output Format:  The first line contains the smallest substring with all the distinct characters of the string S.  Example Input/Output 1:  Input:  aabbcccdddd  Output:  abbcccd  Explanation:  Here the distinct characters are a, b, c and d. The smallest substring with all the characters a, b, c and d is abbcccd. Hence abbcccd is printed as the output.  Example Input/Output 2:  Input:  jsajdnskjd  Output:  ajdnsk Program: import java.util.*; public class Hello {     private static int fun(String s...

Most Frequent Alphabet

Most Frequent Alphabet - CTS GenCNext  The program must accept a string S containing only lower case alphabets and Q queries as the input. Each query contains two integers representing the starting and the ending indices of a substring in S. For each query, the program must print the most frequently occurring alphabet in the specified substring. If two or more alphabets have the same frequency, then the program must print the alphabet that is least in the alphabetical order.  Boundary Condition(s):  2 <= Length of S <= 1000  1 <= Q <= 10^5  Input Format:  The first line contains S. The second line contains Q. The next Q lines, each containing two integers representing the starting and the ending indices of a substring in S.  Output Format:  The first Q lines, each containing the most frequently occurring alphabet in the specified substring.  Example Input/Output 1:  Input:  badbadbed  4  0 8  1 4  0 5...

Maximum Length - 0s or 1s

Maximum Length - 0s or 1s - CTS GenCNext  Problem: The program must accept a string S containing 0s and 1s as the input. The program must print the length of the longest substring in S consists of the maximum number of either 0s or 1s that appear consecutively. (However, the substring cannot occur at the beginning or end of the string). If there is no such substring, the program must print -1 as the output.  Boundary Condition(s):  1 <= Length of S <= 100  Input Format:  The first line contains S.  Output Format:  The first line contains the length of the longest substring in S or -1 as per the given conditions.  Example Input/Output 1:  Input: 10101000  Output:  1 Explanation:  Here the given string is 10101000. The length of the longest substring with consecutive 0s or 1s (expect the substring at the beginning and end of the string S) is 1. Hence the output is 1.  Example Input/Output 2:  Input:  01000000...

Most Favorite Place in India - TCS NQT

Most Favorite Place in India - TCS NQT  Problem: A travel agency is conducting a survey for the most favorite place to visit in India. There are 5 options given to their customers which are given below.  1 Jaipur  2 Agra  3 Rishikesh  4 Goa  5 Mysore  The program must accept a list of integers representing the options selected by the customers. The program must print the name of the most preferred favorite place to visit among the given 5 places as the output. In case of more than one preferred destinations, the program must print the name of those places in the above mentioned order.  Boundary Condition(s):  1 <= options <= 5  Input Format:  The lines, each containing an integer representing the option selected by a customer.  Output Format:  The line(s) containing a string value representing the most preferred favorite place to visit.  Example Input/Output 1:  Input:  1  4  5  2  ...

Maximum by Single Digit Replacement

Maximum by Single Digit Replacement   Problem: Two integers M and N are passed as the input to the program. The program must print the maximum value of M obtained by replacing exactly one digit in M by a digit from N.  Boundary Condition(s):  1 <= Number of digits in M <= 100  1 <= Number of digits in N <= 10  Input Format:  The first line contains M and N separated by space(s).  Output Format: The first line contains the maximum value of M.  Example Input/Output 1:  Input:  56120 21  Output:  56220  Explanation:  The maximum value is obtained by replacing 1 in 56120 by 2. Any other replacements would give smaller values.  Example Input/Output 2:  Input:  895496223 5  Output:  895596223  Program: import java.util.*; public class Hello {     public static void main(String[] args)      { Scanner sc = new Scanner(System.in);  String m = sc...

Count the Ships

Count the Ships  Problem: A sea is represented as an N*N matrix where # represents a part of a ship and - represents water. All the ships are surrounded by water. Series of # which are connected together forms a ship. The # can be connected to another # in any of the surrounding 8 cells to form a ship. The program must print the number of ships in the given map.  Boundary Condition(s):  1 <= N <= 100  Input Format:  The first line contains N. The next N lines contain N characters each.  Output Format:  The first line contains the count of the ship.  Example Input/Output 1:  Input:  6  ------  -###--  -###--  ------  -####-  -####-  Output:  2 Example Input/Output 2:  Input:  8  --#-----  --#-----  --#-----  -----#--  ------#-  --#----#  #####---  --#-----  Output:  3  Program: #include<stdio.h> #include <stdlib.h> vo...

Bunch of Room Keys TCS NQT

Image
Bunch of Room Keys TCS NQT  Problem: In a lodge, there is a bunch of room keys having key number stamped on each key. There may be few duplicate keys in the bunch (i.e., more than one key with the same number). A room can be opened with a key which must have the room's number. There is a rare chance of blank keys (-) which are the keys without numbers, and no room can be opened with those keys. The program must accept the list of key numbers available in the bunch as the input. The character q or Q in the input represents the end of the key numbers list. The program must print the output based on the following conditions.  - The program must print the string value "Blank Keys: " followed by the number of blank keys in the bunch.  - Then the program must print the string value "Total Keys: " followed by the total number of keys available in the bunch.  - Finally, the program must print the string value "Number of rooms: " followed by the total number of...

Walls Reconstruction - Strictly Increasing Order

Image
Walls Reconstruction - Strictly Increasing Order --TCS NQT  Problem: There are N walls constructed in increasing order of height. A mason wants to reconstruct the walls with minimal changes in height so that the height of the N walls are in strictly increasing order. The program must accept N integers representing the height of the N walls as the input. The program must print the total height of the N walls after the reconstruction as the output.  Boundary Condition(s):  2 <= N <= 100  1 <= Each integer value <= 1000  Input Format:  The first line contains N. The second line contains N integers separated by a space.  Output Format: The first line contains the total height of the N walls after the reconstruction. Example Input/Output 1:  Input:  5  2 2 2 5 8  Output:  22  Explanation:  Here N = 5, the heights of the 5 walls are 2, 2, 2, 5 and 8. After reconstruction of the 2nd wall and 3rd wall (with minima...

Customer & Queries CTS GenCNext

Customer & Queries  CTS GenCNext  Problem: In a shop, there are C customers stand in a queue to buy some items. Each customer has a unique ID (from 1 to C). The program must accept the value of C and Q queries as the input. Each query contains 3 integers, where the 1st integer represents the query type. The query type can be any one of the following.  - Query type 1: The 2nd and 3rd integers representing the customer ID and the number of items purchased by the customer. - Query type 2: The 2nd and 3rd integers representing the starting and ending customer ID range. The program must design a system that handles all the transaction queries based on the query type.  - Query type 1: If the customer has already purchased some items, it adds the quanity of the new sale to the previous quantity and updates the record. Else it will make a fresh entry.  - Query type 2: It prints total number of items purchased by the given range of the customer ID. Note: At least one...

Fibonacci Series - Count Odd & Even

Fibonacci Series - Count Odd & Even TCS NQT Problem: The program must accept an integer N as the input. The program must print the Fibonacci series until the given integer N. Then the program must print the number of odd integers present in the Fibonacci series as the output. Finally, the program must print the number of even integers (except 0) present in the Fibonacci series as the output.  Boundary Condition(s):   2 <= N <= 10^8  Input Format:  The first line contains N.  Output Format:  The first line contains the Fibonacci series until the given integer N. The second line contains the number of odd integers present in the Fibonacci series. The third line contains the number of even integers present in the Fibonacci series.  Example Input/Output 1:  Input:  25  Output:  0 1 1 2 3 5 8 13 21  6  2  Explanation:  Here N=25, the Fibonacci series until 25 is given below. 0 1 1 2 3 5 8 13 21 The number of...

Check Sorted Order

Check Sorted Order Problem: The program must accept N integers which are sorted in ascending order except one integer. But if that single integer R is reversed, the entire array will be in sorted order. The program must print the first integer that must be reversed so that the entire array will be sorted in ascending order.  Boundary Condition(s):  2 <= N <= 20  Input Format:  The first line contains N.  The second line contains N integer values separated by a space.  Output Format:  The first line contains the integer value R.  Example Input/Output 1:  Input:  5  10 71 20 30 33  Output:  71  Explanation:  When 71 is reversed the array becomes sorted as below.  10 17 20 30 33  Example Input/Output 2:  Input:  6  10 20 30 33 64 58  Output:  64  Example Input/Output 3:  Input:  6  10 20 30 33 67 58  Output:  58 Program: n = int(input()) l = list...

Count Unique Words

Count Unique Words - WIPRO  Problem: The program must accept a string S containing multiple words as the input. The program must print the number of unique words (i.e., the words that are not repeated) present in the string S as the output.  Boundary Condition(s):  1 <= Length of S <= 1000  Input Format:  The first line contains S.  Output Format:  The first line contains the number of unique words present in the string S.  Example Input/Output 1:  Input:  I love to code and I like to participate in coding competitions  Output:  8  Explanation:  The 8 unique words are given below. love code and like participate in coding competitions  Example Input/Output 2:  Input:  Go Back Welcome back  Output:  4 Program: s = input().strip().split() count = 0  for i in set(s):      if s.count(i) == 1:          count += 1  print(count)    ...

Barcode Number - Old ID

Barcode Number - Old ID - WIPRO  Problem: The program must accept an integer representing a barcode number of a product containing cosmetics. Each digit in the barcode number represents the ID of the cosmetic item. The oldest cosmetic item is the one with the smallest ID. There can be multiples of the same cosmetic item in the product. The program must print the ID of the oldest cosmetic item in the given product as the output.  Boundary Condition(s):  1 <= N <= 10^8  Input Format:  The first line contains an integer representing a barcode number of a product containing cosmetics.  Output Format:  The first line contains an integer representing the ID of the oldest cosmetic item in the given product.  Example Input/Output 1:  Input:  32957  Output:  2  Explanation:  Here N = 32957. The smallest ID in 32957 is 2. So it is printed as the output.  Example Input/Output 2:  Input:  98575  Output:...

Shortest & Second Shortest Distance

Shortest & Second Shortest Distance - TCS DIGITAL Problem:  There are N paths to travel from the city A to the city B. The distance of each path is passed as the input to the program. The program must print the shortest distance and the second shortest distance among the given N distances as the output. If all the distances are equal, then the program must print the string value "Equal" as the output.  Boundary Condition(s):  2 <= N <= 100  1 <= Distance of each path <= 10^8  Input Format:  The first line contains N. The next N lines, each contains an integer value representing the distance of each path from the city A to the city B.  Output Format:  The first line contains the shortest distance and the second shortest distance among the given N distances separated by a space or the first line contains a string value "Equal".  Example Input/Output 1:  Input:  5  400  100  500  200  300  ...

Minimum Profit - N days - WIPRO

Minimum Profit - N days - WIPRO  Problem: A shop has M sales each day for N days. Each day different types of items were sold and had different profits associated with them, but the number of items sold on each day was the same. The program must accept the values of M, N and the profits earned from the M sales for N days as the input. The program must print the minimum profit earned on each day as the output.  Boundary Condition(s):  2 <= M, N <= 50  1 <= Profit for each sale <= 1000  Input Format:  The first line contains M and N separated by a space. The next M lines, each contains N integers separated by a space.  Output Format:  The first line contains the minimum profit earned on each day separated by a space.  Example Input/Output 1:  Input:  5 3  5 7 10  2 14 6  1 8 11  3 9 15  12 4 7  Output:  1 4 6  Explanation:  On the first day, the profits earned from the 5 sales a...

Cricket Tournament

Cricket Tournament   Problem: TCS NQT The program must accept the list of team names participating in a cricket tournament. The character q or Q in the input represents the end of the team list. Each team must play exactly one game against the other teams. The program must print the string value "Total Matches: " followed by the total number of matches possible excluding the semi-final and the final. Then the program must print the pairing of teams in the order of their occurrence as the output.  Boundary Condition(s):  3 <= Number of teams <= 12  Input Format:   The lines, each contains a string value representing the team name participating in a cricket tournament.  Output Format:  The first line contains the string value "Total Matches: " followed by the total number of matches possible excluding the semi-final and the final. The lines from the 2nd line containing the pairing of teams in the order of their occurrence  Example Input/O...

Print Matrix - Diagonal Zig Zag

Print Matrix - Diagonal Zig Zag  Problem: An R*C matrix is passed as the input to the program. The program must print the values in zig-zag order diagonally. (Please refer Example Input/Output section for more details).  Input Format:  The first line contains R and C separated by a space. Next R lines contain C values separated by a space.  Output Format:  The first line contains all R*C elements in zig-zag order diagonally, with the elements separated by a space.  Boundary Conditions:  2 <= R, C <= 100  Example Input/Output 1:  Input:  3 3  1 2 3  4 5 6  7 8 9  Output:  1 2 4 7 5 3 6 8 9  Example Input/Output 2:  Input:  3 7  1 2 3 4 5 6 7  8 9 1 2 3 4 5  6 7 8 9 1 2 3  Output:  1 2 8 6 9 3 4 1 7 8 2 5 6 3 9 1 4 7 5 2 3 Program: r,c = map(int,input().split()) l = [] for i in range(r):     l.append(list(map(int,input().split()))) k = []  for i in range(...

Search String S2 in S1 Character Matrix

Search String S2 in S1 Character Matrix   Problem: Given two strings S1 and S2, form a R*C matrix with the string S1 (You may repeat the string S1 to fill in the rest of the matrix, if length of S1 is less than R*C). Then search for the string S2 along rows from left to right or along columns from top to bottom) and print the number of occurrence of S2.  Input Format:  The first line contains R and C separated by a space. The second line contains S1. The third line contains S2.  Output Format:  The first line contains the integer value denoting the number of occurrence of S2 in the character matrix formed.  Boundary Conditions:  2 <= R, C <= 100  2 <= Length of S1 <= 10000  2 <= Length of S2 <= 200  Example Input/Output 1:  Input:  5 4  managermetuatten  man  Output:  3  Explanation:  The 5*4 character matrix formed using S1 is mana germ etua tten mana man can be found in 1st...

Sum of Embedded Numbers

Sum of Embedded Numbers  Problem: String S is passed as the input to the program. S will have positive numbers in it along with other characters. The program must print the sum of all numbers embedded in the string.  Input Format:  The first line contains S  Output Format:  The first line contains the sum of all numbers in S.  Boundary Conditions:   5 <= Length of S <= 1000  Example Input/Output 1:  Input:  kilo100gram555dhal900  Output:  1555  Example Input/Output 2:  Input:  65rich100guys70went100to22park  Output:  357 Program: s = input().strip()  a = ""  res = 0  for i in s:      if i.isdigit():         a += i      else:          if a!="":              res += int(a)          a = "" if a!="":     res += int(a) print(res)   ...

Rectangle or Square

Rectangle or Square Problem: Vignesh is very much interested in Geometry and he was reading about Rectangles and Squares. He wanted a program which will print Rectangle or Square based on the perimeter P and the area A provided. Please help Vignesh by completing the program for him. (Even though Square is a Rectangle, if length is equal to breadth, the output must be Square).  Input Format:  First line contains total number of test cases, denoted by T Next T lines, contains P and A separated by one or more space(s).  Output Format:  Rectangle or Square  Boundary Conditions / Constraints:  1 <= T <= 25  4 <= P <= 9999999  1 <= A <= 9999999  All values will be positive integers and no floating point values. Only valid Rectangle or Square measurements will be provided as input.  Example Input/Output 1:  Input:  3  4 1  24 32  24 36  Output:  Square  Rectangle  Square  Expla...

Apples and Boxes

Apples and Boxes  Program: Alisha visits a fruit shop which has N boxes and each box has one or more apples in it. The shop keeper will not allow transfer of apples from one box to another. The number of apples in each of these N boxes is passed as the input. Suppose Alisha wishes to buy exactly K apples, the program has to print the distinct ways in which Alisha can pick and buy exactly K apples considering the fact that Alisha's bike can carry only a maximum of 2 boxes.  Input Format:  The first line contains N and K separated by a space. The second line contains the count of apples C1 to CN in each of the boxes separated by a space.  Output Format:  The first line contains the integer value representing the number of ways in which Alisha can pick boxes to buy exactly K apples.  Boundary Conditions:  2 <= N <= 99999  1 <= K <= 99999  1 <= Ci <= K  Note: Optimize the logic to find the count. Else Timeout will occur....

School Chocolate Distribution

School Chocolate Distribution   Problem: Spectrum International School is located in DonnaVille. Chocolates are distributed on certain days (like a festival, independence day) to the N students present on that day. The number of chocolates to be distributed is C. The first student F to be given the chocolate is randomly chosen and the C chocolates are distributed one each to a student in sequence from F till they are emptied. Given F, the program print the number of the student who receives the last chocolate.  Input Format:  First line contains total number of test cases, denoted by T Next T lines, contain a tuple containing 3 values delimited by space N C F,  where - N denotes the number of students - C denotes the number of chocolates - F denotes the number of the student who receives the first chocolate.  Output Format:  The number of the student who receives the last chocolate.  Boundary Conditions / Constraints:  1 <= T <= 100 1 ...

Six Blocks Two Towers

Six Blocks Two Towers Problem: Accept an input of 6 numbers which represent the height of blocks. Also accept 2 numbers which represent the height of the towers. Use the blocks to get the desired height of the towers by using 3 blocks for each tower. The result for each tower should be printed in descending order of the height of the blocks.  Boundary Condition(s):  1 <= Height of the blocks <= 1000  3 <= Height of the towers <= 3000  Input Format:  The first line contains the height of the six blocks separated by a space. The second line contains the height of the two towers separated by a space.  Output Format:  The first line contains the height of the blocks forming tower1 (separated by a space) and then an ampersand which is again followed by the height of the blocks forming tower2 (separated by a space).  Example Input/Output 1:  Input:  10 15 90 65 30 25  135 100  Output:  90 30 15 & 65 25 10  E...