TCS Coding Questions 2022 Day 2 Slot 1

Coding Question 2 for 2022 (September slot)

In this article, we will discuss about the TCS Coding Question which is asked in the TCS placement test. This type of Coding Questions will help you to crack your upcoming TCS exam as well as during your inteview process.

tcs nqt coding question

 

TCS Coding Question Day 2 Slot 1 – Question 2

At a fun fair, a street vendor is selling different colours of balloons. He sells N number of different colours of balloons (B[]). The task is to find the colour (odd) of the balloon which is present odd number of times in the bunch of balloons.

Note: If there is more than one colour which is odd in number, then the first colour in the array which is present odd number of times is displayed. The colours of the balloons can all be either upper case or lower case in the array. If all the inputs are even in number, display the message “All are even”.

Example 1:

  • 7  -> Value of N
  • [r,g,b,b,g,y,y]  -> B[] Elements B[0] to B[N-1], where each input element is sepārated by ṉew line.

Output :

  • r -> [r,g,b,b,g,y,y]  -> “r” colour balloon is present odd number of times in the bunch.

Explanation:

From the input array above:

  • r: 1 balloon 
  • g: 2 balloons
  • b:  2 balloons
  • y : 2 balloons 

Hence , r is only the balloon which is odd in number.

Example 2:

Input:

  • 10 -> Value of N
  • [a,b,b,b,c,c,c,a,f,c] -> B[], elements B[0] to B[N-1] where input each element is separated by new line.

Output :

b-> ‘b’ colour balloon is present odd number of times in the bunch.

Explanation:

From the input array above:

  • a: 2 balloons
  • b: 3 balloons 
  • c: 4 balloons 
  • f: 1 balloons 

Here, both ‘b’ and ‘f’ have odd number of balloons. But ‘b’ colour balloon occurs first.

Hence , b is the output.

Input Format for testing

The candidate has to write the code to accept: 2 input 

  • First input: Accept value for number of N(Positive integer number).
  • Second Input : Accept N number of character values (B[]), where each value is separated by a new line.

Output format for testing

The output should be a single literal (Check the output in example 1 and example 2)

Constraints:

  • 3<=N<=50
  • B[i]={{a-z} or {A-Z}}
import java.util.*;
class Solution
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
char arr[] = new char[n];
for (int i = 0; i < n; i++)
arr[i] = sc.next ().charAt (0);
int lower[] = new int[26];
int upper[] = new int[26];

for (int i = 0; i < n; i++)
{
if ((arr[i] >= 'A') && (arr[i] <= 'Z'))
upper[arr[i] - 'A']++;
else if ((arr[i] >= 'a') && (arr[i] <= 'z'))
lower[arr[i] - 'a']++;
}
boolean flag = false;
char ch = '\0';
for (int i = 0; i < n; i++)
{

if ((arr[i] >= 'A') && (arr[i] <= 'Z'))
{

if (upper[arr[i] - 'A'] % 2 == 1)

{
ch = (char) (arr[i]);
flag = true;
break;
}
}
else if ((arr[i] >= 'a') && (arr[i] <= 'z'))
{
if (lower[arr[i] - 'a'] % 2 == 1)
{
ch = (char) (arr[i]);
flag = true;
break;
}

}

}
if (flag == true)
System.out.println (ch);
else
System.out.println ("All are even");
}
}