Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

TCS Coding Questions 2022 Day 2 Slot 1

Coding Question 1 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 questions

TCS Coding Question Day 2 Slot 1 – Question 1

Given an array Arr[] of size T, contains binary digits, where 

  • 0 represents a biker running to the north.
  • 1 represents a biker running to the south.

The task is to count crossing biker in such a way that each pair of crossing biker(N, S), where 0<=N<S<T, is passing when N is running to the north and S is running to the south.

Constraints:

0<=N<S<T

Example 1:

Input :

  • 5 -> Number of elements i.e. T
  • 0 -> Value of 1st element.
  • 1 -> Value of 2nd element
  • 0 -> Value of 3rd element.
  • 1 -> Value of 4th element.
  • 1 -> Value of 5th element

Output :

  • 5

Explanation:

The 5 pairs are (Arr[0], Arr[1]), (Arr[0], Arr[3]), (Arr[0], Arr[4]), (Arr[2],Arr[3]) and (Arr[2], Arr[4]).

#include <bits/stdc++.h>
using namespace std;

int main ()
{
int n, a, sum = 0, c = 0;
cin >> n;
for (int i = 0; i < n; i++){
cin >> a;
if (a)
sum += c;
else
c++;
}
cout << sum;
}
import java.util.*;
class Solution
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int a, sum = 0, c = 0;
int n = sc.nextInt ();
for (int i = 0; i < n; i++)
{
a = sc.nextInt ();
if (a != 0)
sum += c;
else
c++;
}
System.out.println (sum);
}
}

System.out.println (sum);
}
}
n = int (input ()) 
L =[] 
sum = 0 
c = 0 
for _ in range (n):
    a = (input ()) 
    if a:
      sum += c 
    else:
      c += 1 
print (sum)