Crack TCS Coding Round By solving These Questions

In this post we will see TCS Off Campus Coding Question with complete step by step solution. We will write code in java language with algorithm.

TCS Off Campus Coding Question

As a weather broadcaster you need to record the temperature for few days let us say T days. Once you get the temperature you need to find the maximum difference of temperature from all days. Then you need to print in how many ways we can select the two days giving maximum temperature difference.

Example 1:

Input: 5 days

Array =[3,5,6,7,2,8]

Ouput : 1

Explanation:

If we sort the array we get [2,3,5,6,7,8] and our task is to find in how many ways we can select two days which gives maximum temprature difference. So if we do consider (2,8) which given maximum temperature difference. Now as they are not repeating there is only one way to select that is (2,8). Hence the ouput is 1

Example 2:

Input: 10 days

Array =[2,3,4,5,6,7,8,9,4,2]

Output: 2

Explanation:

Here also same rules apply but the sorted array is [2,2,3,4,4,5,6,7,8,9]. Here you can observe days are repeating so we should find all possible combinations. In order to find total combination we can use the following formula

Total possibilites = Number of time lowest number repeated * Number of times highest number repeated

So if we apply above formula we get

Total possibilites = 2*1 = 2 , which is the required output. So the combinations are (2,9) and (2,9). note 2 and 2 are temperatures of two different days !!!!.

Algorithm

  • Initialy Read the number of days from keyboard ie N value
  • Next read all the values N terms from keyboard
  • Then sort the array in ascending order
  • Then find how many time loewst and highest value are repeated in array.
  • Apply formula mentioned above ie no of time lowest repeated * no of times highest repeated, which will give total combinations of selecting the days.
  • Congrats you just solved an off campus coding question, for more such questions Click Here

Java Code for TCS Off Campus Coding Problem

import java.util.*;
import java.util.Scanner;

public class problem11 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of days:");
        int T = sc.nextInt();
        int arr[] = new int[T];
        System.out.println("Enter array elements:");
        int temp;
        for (int i = 0; i < T; i++) {
            arr[i] = sc.nextInt();
        }
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1; j++) {
                if (arr[j] < arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
        int h = 0;
        int l = 0;
        int res = 0;
        for (int i = 0; i <= arr.length - 1; i++) {
            if (arr[0] == arr[i]) {
                h = h + 1;
            } else if (arr[arr.length - 1] == arr[i]) {
                l = l + 1;
            }
        }
        res = h * l;
        System.out.println("possible ways of selecting are/is " + res);
    }
}

Get complete Explanation for Above Code in our chanel

3 thoughts on “Crack TCS Coding Round By solving These Questions”

  1. Pingback: Xoriant off campus coding question and solution - Is It Actually

  2. Pingback: Complete Solution For Rob And Mars Problem In Java - Is It Actually

  3. Pingback: Count Buildings Hacker Earth coding question with solution - Is It Actually

Leave a Comment

Your email address will not be published. Required fields are marked *