KEMBAR78
Prepinsta Coding Interview | PDF | Sequence | Multiplication
0% found this document useful (0 votes)
44 views22 pages

Prepinsta Coding Interview

The document presents a series of coding problems, each with a specific problem statement, input format, and output requirements. The problems cover various topics such as array manipulation, palindrome detection, permutations, and game strategies involving matrix operations. Each problem includes examples to illustrate the expected input and output, along with constraints and explanations.

Uploaded by

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

Prepinsta Coding Interview

The document presents a series of coding problems, each with a specific problem statement, input format, and output requirements. The problems cover various topics such as array manipulation, palindrome detection, permutations, and game strategies involving matrix operations. Each problem includes examples to illustrate the expected input and output, along with constraints and explanations.

Uploaded by

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

Prepinsta coding

Question 1:Problem Statement:Given an array Arr[ ] of N integers and a positive integer K.


The task is to cyclically rotate the array clockwise by K.
Note : Keep the first of the array unaltered.
Example 1:
5 —Value of N
{10, 20, 30, 40, 50} —Element of Arr[ ]
2 —–Value of K
Output :
40 50 10 20 30
Example 2:
4 —Value of N
{10, 20, 30, 40} —Element of Arr[]
1 —–Value of K
Output :
40 10 20 30
Question 2:
Problem Statement:Given two non-negative integers n1 and n2, where n1
For example:
Suppose n1=11 and n2=15.
There is the number 11, which has repeated digits, but 12, 13, 14 and 15 have no repeated
digits. So, the output is 4.
Example1:
Input:
11 — Vlaue of n1
15 — value of n2
Output:
4
Example 2:
Input:
101 — value of n1
200 — value of n2
Output:
72
Question 3:
Problem Statement: In this 3 Palindrome, Given an input string word, split the string into
exactly 3 palindromic substrings. Working from left to right, choose the smallest split for the
first substring that still allows the remaining word to be split into 2 palindromes.
Similarly, choose the smallest second palindromic substring that leaves a third palindromic
substring.
If there is no way to split the word into exactly three palindromic substrings, print
“Impossible” (without quotes). Every character of the string needs to be consumed.
Cases not allowed –
After finding 3 palindromes using above instructions, if any character of the original string
remains unconsumed.
No character may be shared in forming 3 palindromes.
Constraints:
1 <= the length of input sting <= 1000
Input
First line contains the input string consisting of characters between [a-z].
Output
Print 3 substrings one on each line.
Time Limit
1
Examples:
Example 1
Input
nayannamantenet
Output
nayan
naman
tenet
Explanation
The original string can be split into 3 palindromes as mentioned in the output. However, if the
input was nayanamantenet, then the answer would be “Impossible”.
Example 2
Input
aaaaa
Output
a
a
aaa
Explanation
The other ways to split the given string into 3 palindromes are as follows – [a, aaa, a], [aaa, a,
a], [aa, aa, a], etc.
Since we want to minimise the length of the first palindromic substring using left to right
processing, the correct way to split is [a, a, aaa].
Example 3
Input
aaaabaaaa
Output
a
aaabaaa
a
Explanation
The other ways to split the given string into 3 palindromes are as follows – [aaaa, b, aaaa],
[aa, aabaa, aa], etc.
Since we want to minimise the length of the first palindromic substring using left to right
processing, the correct way to split is [a, aaabaaa, a].
Question 4:
Problem Statement:
In this even odd problem Given a range [low, high] (both inclusive), select K numbers from
the range (a number can be chosen multiple times) such that sum of those K numbers is even.
Calculate the number of all such permutations.
As this number can be large, print it modulo (1e9 +7).
Constraints
0 <= low <= high <= 10^9
K <= 10^6.
Input
First line contains two space separated integers denoting low and high respectively
Second line contains a single integer K.
Output
Print a single integer denoting the number of all such permutations
Time Limit
1
Examples
Example 1
Input
45
3
Output
4
Explanation
There are 4 valid permutations viz. {4, 4, 4}, {4, 5, 5}, {5, 4, 5} and {5, 5, 4} which sum up
to an even number.
Example 2
Input
1 10
2
Output
50
Explanation
There are 50 valid permutations viz. {1,1}, {1, 3},.. {1, 9} {2,2}, {2, 4},… {2, 10} . . . {10,
2}, {10, 4},… {10, 10}. These 50 permutations, each sum up to an even number.
Question 5:
Problem Statement:
Roco is an island near Africa which is very prone to forest fire. Forest fire is such that it
destroys the complete forest. Not a single tree is left.This island has been cursed by God , and
the curse is that whenever a tree catches fire, it passes the fire to all its adjacent tree in all 8
directions,North, South, East, West, North-East, North-West, South-East, and South-
West.And it is given that the fire is spreading every minute in the given manner, i.e every tree
is passing fire to its adjacent tree.Suppose that the forest layout is as follows where T denotes
tree and W denotes water.
Your task is that given the location of the first tree that catches fire, determine how long
would it take for the entire forest to be on fire. You may assume that the lay out of the forest
is such that the whole forest will catch fire for sure and that there will be at least one tree in
the forest
Input Format:
First line contains two integers, M, N, space separated, giving the size of the forest in terms
of the number of rows and columns respectively.
The next line contains two integers X,Y, space separated, giving the coordinates of the first
tree that catches the fire.
The next M lines, where ith line containing N characters each of which is either T or W,
giving the position of the Tree and Water in the ith row of the forest.
Output Format:
Single integer indicating the number of minutes taken for the entire forest to catch fire
Constrains:
3 ≤ M ≤ 20
3 ≤ N ≤ 20
Sample Input 1:
33
WTT
TWW
WTT
Sample Output 1:
5
Explanation:
In the second minute,tree at (1,2) catches fire,in the third minute,the tree at (2,1) catches
fire,fourth minute tree at (3,2) catches fire and in the fifth minute the last tree at (3,3) catches
fire.
Sample Input 2:
66
16
WTTTTT
TWWWWW
WTTTTT
WWWWWT
TTTTTT
TWWWWW
Sample Output 2:
16
Question 6:
Problem Statement: Compute the nearest larger number by interchanging its digits
updated.Given 2 numbers a and b find the smallest number greater than b by interchanging
the digits of a and if not possible print -1.
Input Format
2 numbers a and b, separated by space.
Output Format
A single number greater than b.
If not possible, print -1
Constraints
1 <= a,b <= 10000000
Example 1:
Sample Input:
459 500
Sample Output:
549
Example 2:
Sample Input:
645757 457765
Sample Output:
465577
Question 7:
Problem Statement:In a Conference ,attendees are invited for a dinner after the conference.
The Co-ordinator,Sagar arranged around round tables for dinner and want to have an
impactful seating experience for the attendees. Before finalising the seating arrangement, he
wants to analyse all the possible arrangements. These are R round tables and N attendees. In
case where N is an exact multiple of R, the number of attendees must be exactly N//R,,If N is
not an exact multiple of R, then the distribution of attendees must be as equal as
possible.Please refer to the example section before for better understanding.
For example, R = 2 and N = 3
All possible seating arrangements are
(1,2) & (3)
(1,3) & (2)
(2,3) & (1)
Attendees are numbered from 1 to N.
Input Format:
The first line contains T denoting the number of test cases.
Each test case contains two space separated integers R and N, Where R denotes the number
of round tables and N denotes the number of attendees.
Output Format:
Single Integer S denoting the number of possible unique arrangements.
Constraints:
0 <= R <= 10(Integer)
0 < N <= 20 (Integer)
Sample Input 1:
1
25
Sample Output 1:
10
Explanation:
R = 2, N = 5
(1,2,3) & (4,5)
(1,2,4) & (3,5)
(1,2,5) & (3,4)
(1,3,4) & (2,5)
(1,3,5) & (2,4)
(1,4,5) & (2,3)
(2,3,4) & (1,5)
(2,3,5) & (1,4)
(2,4,5) & (1,3)
(3,4,5) & (1,2)
Arrangements like
(1,2,3) & (4,5)
(2,1,3) & (4,5)
(2,3,1) & (4,5) etc.
But as it is a round table, all the above arrangements are same.
Question 8:
Problem Statement:Given a maximum of 100 digit numbers as input, find the difference
between the sum of odd and even position digits
Test Cases
Case 1
Input: 4567
Expected Output: 2
Explanation : Odd positions are 4 and 6 as they are pos: 1 and pos: 3, both have sum 10.
Similarly, 5 and 7 are at even positions pos: 2 and pos: 4 with sum 12. Thus, difference is 12
– 10 = 2
Case 2
Input: 5476
Expected Output: 2
Case 3
Input: 9834698765123
Expected Output: 1
Given a maximum of 100 digit numbers as input
Solution
(When using Strings as input)
Question 9:
Problem Statement:Word is Key
One programming language has the following keywords that cannot be used as identifiers:
break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type,
var
Write a program to find if the given word is a keyword or not
Test cases
Case 1
Input – defer
Expected Output – defer is a keyword
Case 2
Input – While
Expected Output – while is not a keyword
Question 10:
Problem Statement:Consider the below series:
1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17…..
This series is a mixture of 2 series fail the odd terms in this series form a Fibonacci series and
all the even terms are the prime numbers in ascending order
Write a program to find the Nth term in this series
The value N in a positive integer that should be read from mm. The Nth term that is
calculated by the program should be written to STDOUT Other than the value of Nth term ,
no other characters / string or message should be written to STDOUT.
For example, when N:14, the 14th term in the series is 17 So only the value 17 should be
printed to STDOUT
Question 11:
Problem Statement:
Consider the below series :
0,0,2,1,4,2,6,3,8,4,10,5,12,6,14,7,16,8
This series is a mixture of 2 series all the odd terms in this series form even numbers in
ascending order
Every even terms is derived from the previous term using the formula (x/2)
Write a program to find the nth term in this series.
The value n in a positive integer that should be read from STDIN the nth term that is
calculated by the program should be written to STDOUT.
Other than the value of the nth term no other characters /strings or message should be written
to STDOUT.
For example
if n=10, the 10th term in the series is to be derived from the 9th term in the series. The 9th
term is 8 so the 10th term is (8/2)=4. Only the value 4 should be printed to STDOUT.
You can assume that the n will not exceed 20,000.
Question 12:
Problem Statement:Jack and Jill are playing a string game. Jack has given Jill two strings A
and B. Jill has to derive a string C from A, by deleting elements from string A, such that
string C does not contain any element of string B. Jill needs help to do this task. She wants a
program to do this as she is lazy. Given strings A and B as input, give string C as Output.
Example 1:
Input:
tiger -> input string A
ti -> input string B
Output:
ger -> Output string C
Explanation:
After removing “t” and “i” from “tiger”, we are left with “ger”.
So, the answer is “ger”.
Example 2:
Input:
processed -> input string A
esd -> input string B
Output:
proc -> Output string C
Explanation:
After removing “e” “s” and “d” from “processed”, we are left with “proc”.
So, the answer is “proc”.
Example 3:
Input:
talent -> input string A
tens -> input string B
Output:
al -> Output string C
Explanation:
After removing “t” “e” and “n” from “talent”, we are left with “al”.
So, the answer is “al”.
Question 13:
Problem Statement: Mahesh and Suresh are playing a new game “Checkers“. This is a very
simple game but becomes challenging when more expert players are playing. Below is the
description of the game and rules: The game is played by 2 players. This game consists of an
N*M matrix. Each of the cells is background lit by lights. And these cells are either Green or
Black. The green and black cells are randomly lit and will be represented with 1’s and 0’s
respectively. Green cells are the cells that need to be captured. Black cells cannot be captured.
Everyone is in the race to capture the maximum number of cells possible.
In a single chance, a player can capture all those adjacent cells which share an edge. Once
there is no adjacent edge the chance breaks and the next player will play.
Mahesh always starts the game and Suresh is second.
Both players are playing optimally, find out how many cells Suresh captures.
Input:
N and M, size of the matrix
A[i][j] for all 1<=i<=N and 1<=j<=M
Let us try to understand it with an example
Consider the matrix below
N=4
M=4
A = 1001
0110
0110
1001
If Mahesh plays first, he will try to capture most of the 1’s, he will capture A[2][2], A[2][3],
A[3][2], and A[3][3]. Now there are no adjacent cells left.
So, the chance will be given to Suresh. Now Suresh’s turn. He can capture either A[1][1] or
A[4][1] or A[4][7] or A[4][4]. He will capture any one cell, and as there is no adjacent deft,
the chance will now be given to Mahesh.
The game proceeds and then again Suresh’s turn will come, and he will again be able to
choose only 1 cell finally Mahesh will end the game by choosing the final cell.
Like this Mahesh has captured 6 cells and Suresh has captured only 2 cells.
Hence 2 is the answer.
Example 1:
Input:
2 2 -> Input integer, N, M
1 1 -> Input integer, A[i]
1 1 -> Input integer, A[N]
Output:
0 -> Output
Explanation:
In the above scenario, it is very clear that if Mahesh plays first, he will capture all the cells as
all the cells are adjacent to each other.
There will be nothing left for Suresh. Hence the cells captured by Suresh will be 0.
Hence the answer is 0.
Example 2:
Input:
4 4 -> Input integer, N, M
1001 -> Input integer, A[i]
0110 -> Input integer, A[i+1]
0110 -> Input integer, A[i+2]
1001 -> Input integer, A[N]
Output:
2 -> Output
Explanation:
If Mahesh plays first, he will try to cover most of the 1’s, he will cover A[2][2], A[2][3], A[3]
[2], and A[3][3].
Now there are no adjacent cells left. So, the chance will be given to Suresh. Now Suresh’s
turn. He can capture either of A[1][1] or A[4][1] or A[4][1] or A[4][4]. He will capture any
one cell, and as there is no adjacent left, the chance will now be given to Mahesh.
The game proceeds and then again Suresh’s turn will come, and he will again be able to
choose only 1 cell, and finally, Mahesh will end the game by choosing the final cell.
Like this Mahesh has captured 6 cells and Suresh has captured only 2 cells.
Hence 2 is the answer.
Question 14:
Problem Statement: Joe was reading an interesting novel when all of a sudden, his 5-year-old
son came to him and started asking a few questions about functions. He tried making him
understand various functions, but his son didn’t get find it interesting. Then he created his
function Absent number function A(S) According to this function, there is always the smallest
positive integer number in a sequence that is not available.
In simple words, if you sort the given sequence, then the smallest integer number (other than
0) which is not present in the sequence is the Absent number. Consider a sequence S= [1, 2,
3], then B(S)=4. The minimum value greater than 0 which is not present here in the sequence
is 4.
Now his son found it interesting, so Joe extended this logic to sub-sequence. If there is a
given sequence S, you have to find the Absent Number for each sub-sequence and then sum it
up. If the answer is large, print the result modulo, 109 +7.
Let say there exist a sequence with N = 3, and sequence S = [1, 2, 1]
Below are the various sub-sequences of it,
It will be 2N:
[ ] : B([ ]) = 1
[1] : B([1]) = 2
[2] : B([2]) = 1
[1] : B([1]) = 2
[1, 2] : B([1, 2]) = 3
[2, 1] : B([2, 1]) = 3
[1, 1] : B([1, 1]) = 2
[1, 2, 1] : B([1, 2, 1]) = 3
Total sum of all B(S) = 1+2+1+2+3+3+2+3 = 17.
Hence the answer is 17.
Example 1:
Input:
2 -> input Integer, N
1 1 -> input Integer, S
Output:
7 -> Output
Explanation:
In the above scenario below are the various sub-sequence and their respective functions of it:
[ ] : B(l) = 1
[1]: B([1])= 2
[1]: B([1]) = 2
[1,1]: B([1,1]) = 2
Total sum of all B(S) = 1+2+2+2 = 7 Hence the answer is 7.
Example 2:
Input:
3 -> Input integer, N
1 2 1 -> Input integer, S
Output:
17->Output
Explanation:
In the above scenario below are the various sub-sequences and their respective functions of it.
[ ] : B([ ]) = 1
[1] : B([1]) = 2
[2] : B([2]) =1
[1] : B([1]) = 2
[1, 2] : B([1, 2]) = 3
[2, 1] : B([2, 1]) = 3
[1, 1] : B([1, 1]) = 2
[1, 2, 1] : B([1, 2, 1]) = 3
Total sum of all B(S) = 1 + 2 + 1 + 2 + 3 + 3 + 2 + 3 = 17.
Hence the answer is 17.
Question 15:
Problem Statement:James has a sequence of N numbers. There is also an integer X which is a
random number from other sources. He is allowed to perform a specific operation on this
sequence X number of times. Below is the operation:
Pick exactly one element from the sequence and multiply it with -1.
Your task is to find out the number of different sequences which can be formed by
performing the above operation. If the answer is large, print the result modulo 109 +7.
Let us try to understand it with an example,
N=3
X=2
S = [1, 2, 3]
There are 2 ways in which this operation can be performed.
Way 1: Either -1 should be multiplied to the same element 2 times, OR
Way 2: -1. Should be multiplied by two different elements once each.
Way 1:
If we multiply -1, to each element 2 times. It will become +1 (-1 *-1).
We will get the same sequence for each element:
Multiply -1, 2 times to S[1] : [1, 2, 3].
Multiply -1, 2 times to S[2] : [1, 2, 3].
Multiply -1, 2 times to S[3] : [1, 2, 3].
So, the unique sequence is just 1 which is [1, 2, 3].
Way 2:
If we multiply -1, by two different elements just 1 time each. We get:
Multiply -1 to S[1] & S[2] : [-1, -2, 3].
Multiply -1 to S[2] & S[3] : [1, -2, -3].
Multiply -1 to S[1] & S[3] : [-1, 2, -3].
Hence, we get a total of 3 different sequences from Way 2.
Total 1 + 3 = 4 different sequences.
Hence the answer is 4.000
Example 1:
Input:
3 1 -> Input integer, N, X
{1, 2, 1} -> Input integer, S
Output:
3 -> Output
Explanation:
In the given scenario, we have X =1. Hence, we can have this multiplication of -1 only once.
So, if we multiply -1, by different elements just 1 time. We get:
Multiply -1 to S[1] & S[2] : [-1, -2, 1].
Multiply -1 to S[2] & S[3] : [1, -2, -1].
Multiply -1 to S[1] & S[3] : [-1, 2, -1].
Hence, we get a total of 3 different sequences.
So, the answer is 3.
Example 2:
Input:
3 2 -> Input integer, N, X
{1, 2, 3} -> Input integer, S
Output:
4 -> Output
Explanation:
There are 2 ways in which this operation can be performed
Way 1: Either – 1 should be multiplied to the same element 2 times, OR
Way 2: -1 should be multiplied by different elements once.
As shown in the above Demo example, there will be a total of 4 different sequences which
can be achieved from this.
Hence the answer is 4.
Question 16:
Problem Statement:Two parallel roads separated by a river are connected from cities A and B
to an outer ring road. Both roads have a high flow of traffic throughout the day. People who
want to travel from city A to city B or vice versa have to pass through the ring road which is a
huge waste of time and money. To ease the traffic and also to make it convenient for
commuters to travel from city A to city B and vice versa, the construction of a bridge over the
river is planned.
The surveillance team submitted a report stating the bridge should be constructed in the
following manner:
The ground or soil is stronger at certain points on the road favourable for the construction of
the bridge.
The strong ground positions are given from the starting point of each road. Say, the road of
city A has strong ground at 1,4 meaning there is a strong ground at a distance of 1 unit,
another strong ground point at a distance of 4 units from the starting point of the road of city
A.
Collate the strong ground positions of both roads. Sort them in ascending order. Calculate the
middle point or median of the combined strong ground positions. The bridge should be
constructed from road A as per the middle point calculated. Given the number of strong
positions on roads A and B(N1 and N2 respectively) and the strong ground positions on each
road, the task here is to calculate the midpoint of the combined strong positions on both
roads.
NOTE: When the strong positions are combined, the repeated positions on the different roads
are dropped.
Example 1:
Input:
3 -> Value of N1
3 -> Value of N2
{3,5,2} -> a[ ], Elements a[0]to a[N1-1], where each input element is separated by new line
{1,2,3} -> b[ ], Elements b[0]to b[N2-1), where each input element is separated by new line
Output:
2.5
Explanation:
From the inputs given above:
Number of strong ground positions on road A:3
Number of strong ground positions on road B:3
The positions of strong ground from the starting point of road A are at a distance of 3,5,2
The positions of strong ground from the starting point of road B are at a distance of 1,2,3
Combining the strong ground positions of both the roads and sorting them in ascending order
1, 2, 3, 5
The Middle points are 2 and 3
2+3 = 5
5/2 = 2.5
So, the middle point from where the bridge should be constructed is 2.5.
Hence, the output is 2.5
Example 2:
Input:
2 -> Value of N1
3 -> Value of N2
{2,3} -> all, Elements a[O]to a[N1-1), where each input element is separated by new line
{5,6,4} -> b[ ], Elements b[O]to b[N2-1], where each input element is separated by new line
Output:
4
Explanation:
From the inputs given above:
Number of strong ground positions on road A: 2
Number of strong ground positions on road B: 3
The positions of strong ground from the starting point of road A are at a distance of 2, 3 The
positions of strong ground from the starting point of road B are at a distance of 5, 6, and 4
Combining the strong ground positions of both the roads and sorting them in ascending order:
2, 3, 4, 5, 6 > Middle point is 4
So, the middle point from where the bridge should be constructed is 4.
Hence, the output is 4.
Question 17:
Problem Statement:A chocolate factory is packing chocolates into 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,0,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
Question 18:
Problem Statement:Joseph is learning digital logic subject which will be for his next
semester. He usually tries to solve unit assignment problems before the lecture. Today he got
one tricky question. The problem statement is “A positive integer has been given as an input.
Convert decimal value to binary representation. Toggle all bits of it after the most significant
bit including the most significant bit. Print the positive integer value after toggling all bits”.
Constraints :
1<=N<=100
Example 1:
Input :
10 -> Integer
Output :
5 -> result- Integer
Explanation:
Binary representation of 10 is 1010. After toggling the bits(1010), will get 0101 which
represents “5”. Hence output will print “5”.
Question 19:
Problem Statement:Airport security officials confiscated several items of the passengers at
the security checkpoint. 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 represents 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.
Question 20:
Problem Statement:Given N gold wires, each wire has a length associated with it. At a time,
only two adjacent small wires are assembled at the end of a large wire and the cost of forming
is the sum of their length. Find the minimum cost when all wires are assembled to form a
single wire.
For Example:
Suppose, Arr[]={7,6,8,6,1,1,}
{7,6,8,6,1,1}-{7,6,8,6,2} , cost =2
{7,6,8,6,2}- {7,6,8,8}, cost = 8
{7,6,8,8} – {13,8,8}, cost=13
{13,8,8} -{13,16}, cost=16
{13, 16} – {29}, cost =29
2+8+13+16+29=68
Hence , the minimum cost to assemble all gold wires is 68.
Constraints
1<=N<=30
1<= Arr[i]<=100
Example 1:
Input
6 -> Value of N, represent size of Arr
7 -> Value of Arr[0], represent length of 1st wire
6 -> Value of Arr[1], represent length of 2nd wire
8 -> Value of Arr[2] , represent length of 3rd wire
6 -> Value of Arr[3], represent length of 4th wire
1 -> Value of Arr[4], represent length of 5th wire
1 -> Value of Arr[5], represent length of 6th wire
Output :
68
Example 2:
Input
4 -> Value of N, represents size of Arr
12 -> Value of Arr[0], represents length of 1st wire
2 -> Value of Arr[1], represent length of 2nd wire
2 -> Value of Arr[2], represent length of 3rd wire
5 -> Value of Arr[3], represent length of 4th wire
Output :
34
Question 21:
Problem Statement: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]).
Question 22:
Problem Statement:Given an array of integers of size N, the task is to find the first non-
repeating element in this array.
Examples:
Input: {-1, 2, -1, 3, 0}
Output: 2
Explanation: The first number that does not repeat is : 2
Input: {9, 4, 9, 6, 7, 4}
Output: 6
Question 23:
Problem Statement:Given an array of integers arr[] of size N and an integer, the task is to
rotate the array elements to the left by d positions.
Examples:
Input:
arr[] = {1, 2, 3, 4, 5, 6, 7}, d = 2
Output: 3 4 5 6 7 1 2
Input: arr[] = {3, 4, 5, 6, 7, 1, 2}, d=2
Output: 5 6 7 1 2 3 4
Question 24:
Problem Statement:Given a sequence arr[] of size n, Write a function int equilibrium(int[] arr,
int n) that returns an equilibrium index (if any) or -1 if no equilibrium index exists.
The equilibrium index of an array is an index such that the sum of elements at lower indexes
is equal to the sum of elements at higher indexes.
Examples:
Input: A[] = {-7, 1, 5, 2, -4, 3, 0}
Output: 3
3 is an equilibrium index, because:
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]
Input: A[] = {1, 2, 3}
Output: -1
Question 25:
Problem Statement:Given an Array of size N and a values K, around which we need to right
rotate the array. How to quickly print the right rotated array?
Examples :
Input: Array[] = {1, 3, 5, 7, 9}, K = 2.
Output: 7 9 1 3 5
Explanation:
After 1st rotation - {9, 1, 3, 5, 7}
After 2nd rotation - {7, 9, 1, 3, 5}
Input: Array[] = {1, 2, 3, 4, 5}, K = 4.
Output: 2 3 4 5 1
Question 26:
Problem Statement:Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a
subset of arr1[] or not. Both arrays are not in sorted order. It may be assumed that elements in
both arrays are distinct.
Examples:
Input: arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1}
Output: arr2[] is a subset of arr1[]
Input: arr1[] = {1, 2, 3, 4, 5, 6}, arr2[] = {1, 2, 4}
Output: arr2[] is a subset of arr1[]
Input: arr1[] = {10, 5, 2, 23, 19}, arr2[] = {19, 5, 3}
Output: arr2[] is not a subset of arr1[]
Question 27:
Problem Statement:Two pairs (a, b) and (c, d) are said to be symmetric if c is equal to b and a
is equal to d. For example, (10, 20) and (20, 10) are symmetric. Given an array of pairs find
all symmetric pairs in it.
It may be assumed that the first elements of all pairs are distinct.
Example:
Input: arr[] = {{11, 20}, {30, 40}, {5, 10}, {40, 30}, {10, 5}}
Output: Following pairs have symmetric pairs
(30, 40)
(5, 10)
Question 28:
Problem Statement:Given an array (or string), the task is to reverse the array/string.
Examples :
Input : arr[] = {1, 2, 3}
Output : arr[] = {3, 2, 1}
Input : arr[] = {4, 5, 1, 2}
Output : arr[] = {2, 1, 5, 4}
Question 29:
Problem Statement:Write an efficient C++/C program to find the smallest and second
smallest element in an array.
Example:
Input: arr[] = {12, 13, 1, 10, 34, 1}
Output: The smallest element is 1 and
second Smallest element is 10
Question 30:
Problem Statement:Given an array of integers, our task is to write a program that efficiently
finds the second-largest element present in the array.
Examples:
Input: arr[] = {12, 35, 1, 10, 34, 1}
Output: The second largest element is 34.
Explanation: The largest element of the array is 35 and the second largest element is 34
Input: arr[] = {10, 5, 10}
Output: The second largest element is 5.
Explanation: The largest element of the array is 10 and the second largest element is 5
Input: arr[] = {10, 10, 10}
Output: The second largest does not exist.
Explanation: Largest element of the array is 10 there is no second largest element
Question 31:
Problem Statement:Given an array which may contain duplicates, print all elements and their
frequencies.
Examples:
Input : arr[] = {10, 20, 20, 10, 10, 20, 5, 20}
Output : 10 3
20 4
51
Input : arr[] = {10, 20, 20}
Output : 10 1
20 2
A simple solution is to run two loops. For every item count number of times, it occurs. To
avoid duplicate printing, keep track of processed items.
Question 32:
Problem Statement:Given a K-increasing-decreasing array arr[], the task is to sort the given
array. An array is said to be K-increasing-decreasing if elements repeatedly increase upto a
certain index after which they decrease, then again increase, a total of K times. The diagram
below shows a 4-increasing-decreasing array.
Example:
Input: arr[] = {57, 131, 493, 294, 221, 339, 418, 458, 442, 190}
Output: 57 131 190 221 294 339 418 442 458 493
Input: arr[] = {1, 2, 3, 4, 3, 2, 1}
Output: 1 1 2 2 3 3 4
Question 33:
Problem Statement:Given an array of integers, find the sum of its elements.
Examples:
Input : arr[] = {1, 2, 3}
Output : 6
Explanation: 1 + 2 + 3 = 6
Input : arr[] = {15, 12, 13, 10}
Output : 50
Question 34:
Problem Statement:Given a sorted array arr[] of size N, the task is to remove the duplicate
elements from the array.
Examples:
Input: arr[] = {2, 2, 2, 2, 2}
Output: arr[] = {2}
Explanation: All the elements are 2, So only keep one instance of 2.
Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}
Output: arr[] = {1, 2, 3, 4, 5}
Question 35:
Problem Statement:Given an array of size n, write a program to check if it is sorted in
ascending order or not. Equal values are allowed in an array and two consecutive equal
values are considered sorted.
Examples:
Input : 20 21 45 89 89 90
Output : Yes
Input : 20 20 45 89 89 90
Output : Yes
Input : 20 20 78 98 99 97
Output : No
Question 36:
Problem Statement:Given an unsorted array of integers, print the array after removing the
duplicate elements from it. We need to print distinct array elements according to their first
occurrence.
Examples:
Input : arr[] = { 1, 2, 5, 1, 7, 2, 4, 2}
Output : 1 2 5 7 4
Explanation : {1, 2} appear more than one time.
Question 37:
Problem Statement:Given an array, the task is to find average of that array. Average is the
sum of array elements divided by the number of elements.
Examples :
Input : arr[] = {1, 2, 3, 4, 5}
Output : 3
Sum of the elements is 1+2+3+4+5 = 15
and total number of elements is 5.
So average is 15/5 = 3
Input : arr[] = {5, 3, 6, 7, 5, 3}
Output : 4.83333
Sum of the elements is 5+3+6+7+5+3 = 29
and total number of elements is 6.
So average is 29/6 = 4.83333.

You might also like