0% found this document useful (0 votes)
17 views5 pages

PFR192 Pe Su2023

The document contains descriptions of 7 problems related to arrays and strings. The problems involve tasks like finding a subarray that sums to a given number, finding leaders in an array, checking balanced brackets, finding equilibrium points in an array, printing the second largest element, reversing an array of strings, and printing a crown pattern.

Uploaded by

tunahe186804
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)
17 views5 pages

PFR192 Pe Su2023

The document contains descriptions of 7 problems related to arrays and strings. The problems involve tasks like finding a subarray that sums to a given number, finding leaders in an array, checking balanced brackets, finding equilibrium points in an array, printing the second largest element, reversing an array of strings, and printing a crown pattern.

Uploaded by

tunahe186804
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/ 5

PFR192: Practice at the end of the semester

Problem 1:
Given an unsorted array A of size N that contains only positive integers, find a continuous sub-
array that adds to a given number S and return the left and right index(1-based indexing) of
that subarray.
In case of multiple subarrays, return the subarray indexes which come first on moving from left
to right.
Note:- You have to return an ArrayList consisting of two elements left and right. In case no
such subarray exists return an array consisting of element -1.
Example 1: Example 2:

Input: Input:
N = 5, S = 12 N = 10, S = 15
A[] = {1,2,3,7,5} A[] = {1,2,3,4,5,6,7,8,9,10}
Output: 2 4 Output: 1 5
/*Explanation: The sum of elements /* Explanation: The sum of elements
from 2nd position to 4th position from 1st position to 5th position
is 12. */ is 15. */

Problem 2:
Given an array A of positive integers. Your task is to find the leaders in the array. An element of
array is leader if it is greater than or equal to all the elements to its right side. The rightmost
element is always a leader.
Example 1:

Explanation: The first leader is 17


Input:
as it is greater than all the elements
n=6
to its right. Similarly, the next
A[] = {16,17,4,3,5,2}
leader is 5. The right most element
Output: 17 5 2
is always a leader so it is also included.
Example 2:
Your Task: The task is to complete the
Input:
function leader() which takes array A and n as input
n=5 parameters and returns an array of leaders in order of
A[] = {1,2,3,4,0} their appearance.
Output: 4 0
Problem 2:
Given an array A of N elements. Find the majority element in the array. A majority element in
an array A of size N is an element that appears more than N/2 times in the array.
Example 1:

Explanation:
Input:
Since, each element in {1,2,3} appears only once so there is no
N=3
majority element.
A[] = {1,2,3}
Output:
-1

Example 2:

Input: Explanation:

N=5 Since, 3 is present more than N/2 times, so it is the majority


A[] = {3,1,3,3,2} element.
Output: Your Task:

3 The task is to complete the function majorityElement() which returns the


majority element in the array. If no majority exists, return -1.

Problem 3:
Given an expression string x. Examine whether the pairs and the orders of {,},(,),[,] are correct
in exp.
For example, the function should return 'true' for exp = [()]{}{[()()]()} and 'false' for exp = [(]).

Note: The drive code prints "balanced" if function return true, otherwise it prints "not
balanced".

Example 1:

Input: Explanation:
{([])} { ( [ ] ) }. Same colored brackets can form
Output: balanced pairs, with 0 number of
true unbalanced bracket.

Example 2:
Explanation:
(). Same bracket can form balanced pairs,
and here only 1 type of bracket is
present and in balanced way.

Example 3:

Input: Explanation:
([] ([]. Here square bracket is balanced but
Output: the small bracket is not balanced and
false Hence , the output will be unbalanced.

Your Task:
This is a function problem. You only need to complete the function ispar() that takes a string as
a parameter and returns a boolean value true if brackets are balanced else returns false.
The printing is done automatically by the driver code.

Problem 4:

Given an array A of n positive numbers. The task is to find the first Equilibrium Point in an
array.
Equilibrium Point in an array is a position such that the sum of elements before it is equal to
the sum of elements after it.

Note: Retun the index of Equilibrium point. (1-based index)

Example 1:

Input: Explanation:

n=5 equilibrium point is at position 3


A[] = {1,3,5,2,2} as elements before it (1+3) =
Output: 3 elements after it (2+2).

Example 2:
Input: Explanation:

n=1 Since its the only element hence


A[] = {1} its the only equilibrium point.
Output: 1

Your Task:
The task is to complete the function equilibriumPoint() which takes the array and n as input
parameters and returns the point of equilibrium. Return -1 if no such point exists.

Problem 4:
Given an array Arr of size N, print second largest distinct element from an array.

Example 1:

Input: Explanation: The largest element of the

N=6 array is 35 and the second largest element


Arr[] = {12, 35, 1, 10, is 34.
34, 1}
Output: 34
Example 2:

Input: Explanation: The largest element of

N=3 the array is 10 and the second


Arr[] = {10, 5, 10} largest element is 5.
Output: 5

Your Task:

You don't need to read input or print anything. Your task is to complete the
function print2largest() which takes the array of integers arr and n as parameters and returns an
integer denoting the answer. If 2nd largest element doesn't exist then return -1.
Problem 5:
Given an array of string literals, reverse the array.
Examples:
Input : arr[] = {"Coding", "Never", "Fail", "Me"}
Output : arr[] = {"Me", "Fail", "Never", "Coding"}

Input : arr[] = {"welcome", "to", "FptU"}


Output : arr[] = {"FptU", "to", "welcome"}

Problem 6:
Floyd’s triangle is a triangle with first natural numbers. Task is to print reverse of Floyd’s
triangle.

Examples:
Input : 5
Input : 4
Output :
Output :
15 14 13 12 11
10 9 8 7
10 9 8 7
6 5 4
6 5 4
3 2
3 2
1
1

Problem 7:
Given the value of length, print the crown pattern using ‘*’ and ‘#’.
Note : In order to print a perfect crown, take the value of length as an odd number greater
than 15.
Examples: Input: length = 19
Output:
* * *
Input: length = 25
# # #
Output: ###
## ##
*
### #####* ###
*
#### ####### ####
# #
###################
#
###################
## ###
##
###################
### #####
*******************
###
#### #######
####

You might also like