0% found this document useful (0 votes)
72 views

Tcs Q

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Tcs Q

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Problem Statement – An automobile company manufactures both a two wheeler (TW) and

a four wheeler (FW). A company manager wants to make the production of both types
of vehicle according to the given data below:

1st data, Total number of vehicle (two-wheeler + four-wheeler)=v


2nd data, Total number of wheels = W
The task is to find how many two-wheelers as well as four-wheelers need to
manufacture as per the given data.
Example :

Input :
200 -> Value of V
540 -> Value of W

Output :
TW =130 FW=70

Explanation:
130+70 = 200 vehicles
(70*4)+(130*2)= 540 wheels

Constraints :

2<=W
W%2=0
V<W
Print “INVALID INPUT” , if inputs did not meet the constraints.

The input format for testing


The candidate has to write the code to accept two positive numbers separated by a
new line.

First Input line – Accept value of V.


Second Input line- Accept value for W.
The output format for testing

Written program code should generate two outputs, each separated by a single space
character(see the example)
Additional messages in the output will result in the failure of test case

sol:

import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int v=sc.nextInt();
int w=sc.nextInt();
float res=((4*v)-w)/2;
if(w>=2 && (w%2==0) && v<w)
System.out.println("TW= "+(int)(res)+" FW= "+(int)(v-res));
else
System.out.println("INVALID INPUT");
}
}

-------------------------------------------
Problem Statement – Given a string S(input consisting) of ‘*’ and ‘#’. The length
of the string is variable. The task is to find the minimum number of ‘*’ or ‘#’ to
make it a valid string. The string is considered valid if the number of ‘*’ and ‘#’
are equal. The ‘*’ and ‘#’ can be at any position in the string.
Note : The output will be a positive or negative integer based on number of ‘*’ and
‘#’ in the input string.

(*>#): positive integer


(#>*): negative integer
(#=*): 0
Example 1:
Input 1:

###*** -> Value of S


Output :

0 → number of * and # are equal

sol:
import java.util.*;
public class Main
{
public static void main(String[] args)
{

String str="Hello";
int count1=0,count2=0;
for(int i=0;i< str.length();i++)
{
if(str.charAt(i)=='*')
count1++;##
else if(str.charAt(i)=='#')
count2++;
}
System.out.println(count1-count2);
}
}

-----------------------------------
Given an integer array Arr of size N the task is to find the count of elements
whose value is greater than all of its prior elements.

Note : 1st element of the array should be considered in the count of the result.

For example,
Arr[]={7,4,8,2,9}
As 7 is the first element, it will consider in the result.
8 and 9 are also the elements that are greater than all of its previous elements.
Since total of 3 elements is present in the array that meets the condition.
Hence the output = 3.
Example 1:

Input
5 -> Value of N, represents size of Arr
7-> Value of Arr[0]
4 -> Value of Arr[1]
8-> Value of Arr[2]
2-> Value of Arr[3]
9-> Value of Arr[4]
Output :
3

Example 2:
5 -> Value of N, represents size of Arr
3 -> Value of Arr[0]
4 -> Value of Arr[1]
5 -> Value of Arr[2]
8 -> Value of Arr[3]
9 -> Value of Arr[4]

Output :
5

Constraints

1<=N<=20
1<=Arr[i]<=10000

sol:
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;imax)
{
max=arr[i];
count++;
}
}
System.out.println(count);
}
}
------------------------------------------
A parking lot in a mall has RxC number of parking spaces. Each parking space will
either be empty(0) or full(1). The status (0/1) of a parking space is represented
as the element of the matrix. The task is to find index of the prpeinzta row(R) in
the parking lot that has the most of the parking spaces full(1).

Note :
RxC- Size of the matrix
Elements of the matrix M should be only 0 or 1.

Example 1:
Input :
3 -> Value of R(row)
3 -> value of C(column)
[0 1 0 1 1 0 1 1 1] -> Elements of the array M[R][C] where each element is
separated by new line.
Output :
3 -> Row 3 has maximum number of 1’s

Example 2:
input :
4 -> Value of R(row)
3 -> Value of C(column)
[0 1 0 1 1 0 1 0 1 1 1 1] -> Elements of the array M[R][C]
Output :
4 -> Row 4 has maximum number of 1’s

sol:

import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int row=sc.nextInt();
int col=sc.nextInt();
int arr[][]=new int[row][col];
for(int i=0;i< row;i++)
for(int j=0;j< col;j++)
arr[i][j]=sc.nextInt();

int max=0,count=0,index=0;
for(int i=0;i< row;i++)
{
count=0;
for(int j=0;j< col;j++)
{
if(arr[i][j]==1)
count++;
}
if(count>max)
{
max=count;
index=i+1;
}
}
System.out.println(index);
}
}

You might also like