0% found this document useful (0 votes)
24 views23 pages

Arrays Frequently Asked Question in MNC

The document outlines various programming tasks involving arrays, including summing odd and even elements, finding maximum values, summing elements, checking if two arrays are the same, sorting arrays, inserting and deleting elements, and calculating distances. It provides input and output formats along with example cases for each task. Additionally, it includes problems related to stock prices, energy calculations, and sorting based on risk severity.

Uploaded by

Himabindu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views23 pages

Arrays Frequently Asked Question in MNC

The document outlines various programming tasks involving arrays, including summing odd and even elements, finding maximum values, summing elements, checking if two arrays are the same, sorting arrays, inserting and deleting elements, and calculating distances. It provides input and output formats along with example cases for each task. Additionally, it includes problems related to stock prices, energy calculations, and sorting based on risk severity.

Uploaded by

Himabindu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Sum of odd and even

Write a program to sum of even and odd elements in an array.

INPUT & OUTPUT FORMAT:


Input consists of 1 integer and 1 array.
Integer corresponds to the size of the array.
The output consists of integers

Case 1
Input (stdin)
3
31
28
13

Output (stdout)
44
28

1
Maximum value
Write a program to find the maximum element in an array.

INPUT FORMAT:
Input consists of 1 integer and 1 array.
Integer corresponds to the size of the array.

Case 1
Input (stdin)
5
1
2
33
4
5

Output (stdout)
33

Case 2
Input (stdin)
3
101
210
90

Output (stdout)
210

2
Sum of elements
QUESTION: Write a program to find the sum of elements in an array.

INPUT & OUTPUT FORMAT:


Input consists of 1 integer and 1 array.
Integer corresponds to the size of the array
The output consists of an integer.

Case 1
Input (stdin)
3
1
2
3

Output (stdout)
6

Case 2
Input (stdin)
5
1
2
3
4
5

Output (stdout)
15

3
Same or Not
Write a program to find whether the two arrays are the same or not. Two
arrays are said to be the same if the sum of both the arrays is the same and
the size of arrays is the same.

INPUT FORMAT:
Input consists of 2 integers and 2 arrays.
Integers correspond to the size of arrays.
If two arrays are same, display "Same" else display "Not Same"

Case 1
Input (stdin)
3
3
1
2
3
1
2
3

Output (stdout)
Same

Case 2
Input (stdin)
3
3
1
2
1

4
2
3
6

Output (stdout)
Not Same

5
Ascending order
Write a program to sort the given array in ascending order.

INPUT FORMAT:
Input consists of 1 integer and 1 array.
Integer corresponds to the size of the array.

Case 1
Input (stdin)
5
54
68
25
14
74

Output (stdout)
Sorted array is:
14
25
54
68
74

Case 2
Input (stdin)
4
51
-3
5
-6

6
Output (stdout)
Sorted array is:
-6
-3
5
51

7
Insert an element
Write a program to insert an element in an array at the given position. If the
position where the element is to be inserted is greater than the size of the
array display “Invalid Input”.

INPUT FORMAT:
Input consists of 3 integers and 1 array.
The first integer corresponds to the size of the array.
The second integer corresponds to the position where the element is to be
inserted.
The third integer corresponds to the element to be inserted.

Case 1
Input (stdin)
5
1
2
3
4
5
4
10
Output (stdout)
Array after insertion is:
1
2
3
10
4
5

8
Delete an element
Write a program to delete an element from the given location in an array..

Case 1
Input (stdin)
5
1
2
3
4
5
4

Output (stdout)
Array after deletion is:
1
2
3
5

9
Wipro 2022
A company wishes to provide cab service for their N employees. The
employees have distance ranging from 0 to N-1. The company has calculated
the total distance from an employee’s residence to the company, considering
the path to be followed by the cab is a straight path. The distance of the
side of the company is represented with a negative sign. The distance for the
employees who live to the right side of the company is represented with a
positive sign. The cab will be allotted a range of distance. The company
wishes to find the distance for the employees who live within the particular
distance range. Write an algorithm to find the distance for the employees who
live within the distance range.
Input
The first line of the input consists of three space-separated integers-num, start
and end representing the size of the list (N); the starting value of the range:
and the ending value of the range, respectively. The second line of the input
consists of N space-separated integers representing the distances of the
employees from the company.
Output
Print space-separated integers representing the distance for employees
whose distance lies within the given range else return -1.
Constraints
NA
Example
Input:
6 size of the list
29 38 12 48 39 55 list elements
30 low limit
50 high limit
Output:
38 48 39

10
Explanation:
There are three employees with distances 38, 48 and 39 whose distance from
the office lies within the given range.

11
Wipro 2022
In an online maths tutorial a student is given a list of N numbers. From this list,
the student is required to sum each element in the list such that the ith
element in the output list will be equal to the sum to the first element until the
ith element in the list.

Input:
The first line of input consists of an integer – numSize representing the count
of total numbers in the list(N).
The next line consists of N space separated integers.
element1, element2,…… element-n
representing the numeric value in the list.

Example:
Input:
5
14263
Output:
1 5 7 13 16
Explanation:
The sum of each element 1, (1+4=5), (1+4+2=7), (1+4+2+6=13),
(1+4+2+6+3=16). Hence the output is 1 5 7 13 16.

12
Wipro 2022
Write a program to calculate and return the sum of distances between the
adjacent numbers In an array of positive integers.
Note:
You are expected to write code in the find Total distance function only which
receive the first parameter as the number of items in the array, and second
parameter as the array itself. You are not requested to take input from the
console.
Constraints
NA
Example
Finding the total distance between adjacent items of a list of 5 numbers
Input:
5
10 11 7 12 14
Output:
12
Explanation
The first parameters (5) is the size of the array next is an array of integers the
total of distance is 12 as per the calculation below.
10-11=1
11-7=4
7-12=5
12-14=2
Total distance-1+4+5+2=12

13
Wipro 2022
Andrew is a stock trader who trades in N selected stocks. He has calculated
the relative stock price changes in the N stocks from the previous day stock
prices. Now, his lucky number is K, so he wishes to invest in the particular
stock that has Kth smallest relative stock value. Write an algorithm for Andrew
to find the Kth smallest stock price out of the selected N stocks.
Input
The first line of the input consists of two space-separated integers –
numOfStocks and valuek, representing the number of selected stocks (N) And
the value K for which he wishes to find the stock price, respectively. The
second line consists of N space-separated integers – stock1, stock2, ……,
stock N
representing the relative stock prices of the selected stocks.
Output
Print an integer representing the Kth smallest stock price of selected N stocks.
Constraints
0 <valueK ≤ numOfStocks ≤ 106
0 ≤ stocki ≤ 106
0 ≤ i<numOfStocks
Example
Input:
75
9 -3 8 -6 -7 18 10
Output:
9
Explanation:
The sorted relative stock prices are [-7, -6, -3, 8, 9, 10, 18]
So, the 5th smallest stock price is 9.

14
Wipro 2022
In a science research lab, combining two nuclear chemicals produces a
maximum energy that is the product of the energy of the two chemicals. The
energy values of the chemicals can be negative or positive. The scientist
wishes to calculate the sum of the maximized energies of the two elements
when the reaction happens. Write an algorithm to find the total energy
produced by the chemicals when the reaction happens.
Input
The first line of the input consists of an integer numOfChem, representing the
number of chemicals (N). The second line consists of N space-separated
integers – enerp ener2, , enerN representing the energies of the chemicals.
Output
Print an integer representing the total energy produced by the chemicals when
the reaction happens.
Constraints
0 <_ num0fChem 106
-106<ener<106
0 < i<numOfChem
Example
Input:
7
9 -3 8 -6 -7 8 10
Output:
19
Explanation:
NA

15
Wipro 2022
The garments company Apparel wishes to open outlets at various locations.
The company shortlisted several plots in these locations and wishes to select
only plots that are square- shaped. Write an algorithm to help Apparel find the
number of plots that it can select for its outlets.
Input
The first line of the input consists of an integer num0fMots, representing the
number of plots shortlisted by the company for outlets (N). The second line
consists of N space-separated integers – areal, areal, ….., areaN representing
the area of the N plots selected for outlets.
Output
Print an integer representing the number of plots that will be selected for
outlets.
Constraints:
0 < num0fPlots < 106
0 < area < 106
0 < i < num0fPlots
Example
Input:
8
79 77 54 81 48 34 25 16
Output:
3
Explanation:
The areas that are in square form are 81, 25 and 16. So, the output is 3.

16
Students ID’s Wipro 2022
Students in a class have been assigned a unique ID. As part of a quiz
competition, the class teacher wishes to form two different groups from the N
number of students who are participating the quiz. All participants are
currently sitting a random manner, but teacher wishes all of them to sit
according their ID’s. Two groups will be formed in such a way that students
whose seat number in an odd number will be the first group and students
whose seat number is an even number will be in the second group.
Write an algorithm to find the list of which students IDs should be in the first
group, followed by the student ID’s which should be in the second group as
per the teacher’s instructions.

Input:
First line n denotes number of students
Second line n elements representing students IDs for all the participants.
Output:
Print N space separated integers representing the students IDs of first group
followed by the student’s IDs of the second group.

Note:
Students sit according to their IDs starting from seat number 1.
Example:
Input:
6
6 9 10 4 2 11
Output:
2 6 10 4 9 11
Explanation:
Step 1: Students should sit according to their IDs. So we get {2,4,6,9,10,11}
Step 2: Students with an odd numbered seat

17
18
TCS Ninja 2022
A chocolate factory is packing chocolates into the packets. The chocolate
packets here represent an array arrt of N number of integer values. The task
is to find the empty packets(0) of chocolate and push it to the end of the
conveyor belt(array).

For Example:
N=7 and arr = [4,5,0,1.9,0,5,0].
There are 3 empty packets in the given set. These 3 empty packets
represented as O should be pushed towards the end of the array

Example 1:
Input:
7 - Value of N
[4,5,0,1,9,0,5] - Element of arr[O] to arr[N-1],While input each element is
separated by newline
Output:
4519500
Example 2:
Input:
6
— Value of N.
[6,0,1,8,0,2] - Element of arr[0] to arr[N-1], While input each element is
separated by newline

Output:
618200

19
TCS Ninja 2022
Airport security officials have confiscated several item of the passengers at
the security check point. All the items have been dumped into a huge box
(array). Each item possesses a certain amount of risk[0,1,2]. Here, the risk
severity of the items represent an array[] of N number of integer values. The
task here is to sort the items based on their levels of risk in the array. The risk
values range from 0 to 2.

Example :
Input :
7 -> Value of N
[1,0,2,0,1,0,2]-> Element of arr[0] to arr[N-1], while input each element is
separated by new line.

Output :
0 0 0 1 1 2 2 -> Element after sorting based on risk severity

Example 2:
input : 10 -> Value of N
[2,1,0,2,1,0,0,1,2,0] -> Element of arr[0] to arr[N-1], while input each element
is separated by a new line.

Output :
0 0 0 0 1 1 1 2 2 2 ->Elements after sorting based on risk severity.

Explanation:
In the above example, the input is an array of size N consisting of only 0’s, 1’s
and 2s. The output is a sorted array from 0 to 2 based on risk severity.

20
TCS Ninja 2022
A party has been organized on cruise. The party is organized for a limited
time(T). The number of guests entering (E[i]) and leaving (L[i]) the party at
every hour is represented as elements of the array. The task is to find the
maximum number of guests present on the cruise at any given instance within
T hours.

Example 1:
Input :
5 -> Value of T
[7,0,5,1,3] -> E[], Element of E[0] to E[N-1], where input each element is
separated by new line
[1,2,1,3,4] -> L[], Element of L[0] to L[N-1], while input each element is
separate by new line.

Output :
8 -> Maximum number of guests on cruise at an instance.

Explanation:
1st hour:
Entry : 7 Exit: 1
No. of guests on ship : 6
2nd hour :
Entry : 0 Exit : 2
No. of guests on ship : 6-2=4
Hour 3:
Entry: 5 Exit: 1
No. of guests on ship : 4+5-1=8
Hour 4:
Entry : 1 Exit : 3
No. of guests on ship : 8+1-3=6
Hour 5:

21
Entry : 3 Exit: 4
No. of guests on ship: 6+3-4=5
Hence, the maximum number of guests within 5 hours is 8.

Example 2:
Input:
4 -> Value of T
[3,5,2,0] -> E[], Element of E[0] to E[N-1], where input each element is
separated by new line.
[0,2,4,4] -> L[], Element of L[0] to L[N-1], while input each element in
separated by new line

Output:
6
Cruise at an instance

Explanation:
Hour 1:
Entry: 3 Exit: 0
No. of guests on ship: 3
Hour 2:
Entry : 5 Exit : 2
No. of guest on ship: 3+5-2=6
Hour 3:
Entry : 2 Exit: 4
No. of guests on ship: 6+2-4= 4
Hour 4:
Entry: 0 Exit : 4
No. of guests on ship : 4+0-4=0
Hence, the maximum number of guests within 5 hours is 6.

The input format for testing

22
The candidate has to write the code to accept 3 input.
First input- Accept value for number of T(Positive integer number)
Second input- Accept T number of values, where each value is separated by a
new line.
Third input- Accept T number of values, where each value is separated by a
new line.

The output format for testing


The output should be a positive integer number or a message as given in the
problem statement(Check the output in Example 1 and Example 2)

Constraints:
1<=T<=25
0<= E[i] <=500
0<= L[i] <=500

23

You might also like