0% found this document useful (0 votes)
28 views17 pages

Softsquare

The document outlines various programming problems related to arrays, including finding subarrays with specific sums, counting distinct triplets, finding maximum subarray sums, identifying missing elements in permutations, merging sorted arrays, rearranging elements, and more. Each problem includes example inputs and outputs, along with constraints on the size and values of the arrays. The problems are designed to test algorithmic skills and understanding of data structures.

Uploaded by

vinupriyatpc
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)
28 views17 pages

Softsquare

The document outlines various programming problems related to arrays, including finding subarrays with specific sums, counting distinct triplets, finding maximum subarray sums, identifying missing elements in permutations, merging sorted arrays, rearranging elements, and more. Each problem includes example inputs and outputs, along with constraints on the size and values of the arrays. The problems are designed to test algorithmic skills and understanding of data structures.

Uploaded by

vinupriyatpc
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/ 17

ARRAY

1.Given an array arr[] containing only non-negative integers, your task is to find a continuous
subarray (a contiguous sequence of elements) whose sum equals a specified value target. You
need to return the 1-based indices of the leftmost and rightmost elements of this subarray. You
need to find the first subarray whose sum is equal to the target.

Note: If no such array is possible then, return [-1].

Examples:

Input: arr[] = [1, 2, 3, 7, 5], target = 12


Output: [2, 4]
Explanation: The sum of elements from 2nd to 4th position is 12.
Input: arr[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], target = 15
Output: [1, 5]
Explanation: The sum of elements from 1st to 5th position is 15.
Input: arr[] = [5, 3, 4], target = 2
Output: [-1]
Explanation: There is no subarray with sum 2.
Constraints:
1 <= arr.size()<= 106
0 <= arr[i] <= 103
0 <= target <= 109

2.Given an array arr, count the number of distinct triplets (a, b, c) such that:

 a+b=c
 Each triplet is counted only once, regardless of the order of a and b.
 Even if the same number appears multiple times in the array, a valid triplet should be
counted only once per unique combination.
Examples:

Input: arr[] = [1, 5, 3, 2]


Output: 2
Explanation: There are 2 triplets: 1 + 2 = 3 and 3 +2 = 5
Input: arr[] = [2, 3, 4]
Output: 0
Explanation: No such triplet exits
Constraints:
1 ≤ arr.size() ≤ 103
1 ≤ arr[i] ≤ 105
3.Given an integer array arr[]. You need to find the maximum sum of a subarray.

Examples:

Input: arr[] = [2, 3, -8, 7, -1, 2, 3]


Output: 11
Explanation: The subarray {7, -1, 2, 3} has the largest sum 11.
Input: arr[] = [-2, -4]
Output: -2
Explanation: The subarray {-2} has the largest sum -2.
Input: arr[] = [5, 4, 1, 7, 8]
Output: 25
Explanation: The subarray {5, 4, 1, 7, 8} has the largest sum 25.
Constraints:
1 ≤ arr.size() ≤ 105
-109 ≤ arr[i] ≤ 104

4.You are given an array arr[] of size n - 1 that contains distinct integers in the range from 1 to
n (inclusive). This array represents a permutation of the integers from 1 to n with one element
missing. Your task is to identify and return the missing element.

Examples:

Input: arr[] = [1, 2, 3, 5]


Output: 4
Explanation: All the numbers from 1 to 5 are present except 4.

Input: arr[] = [8, 2, 4, 5, 3, 7, 1]


Output: 6
Explanation: All the numbers from 1 to 8 are present except 6.
Input: arr[] = [1]
Output: 2
Explanation: Only 1 is present so the missing element is 2.

Constraints:
1 ≤ arr.size() ≤ 106
1 ≤ arr[i] ≤ arr.size() + 1

5.Given two sorted arrays a[] and b[] of size n and m respectively, the task is to merge them in
sorted order without using any extra space. Modify a[] so that it contains the first n elements
and modify b[] so that it contains the last m elements.
Examples:

Input: a[] = [2, 4, 7, 10], b[] = [2, 3]


Output:
2234
7 10
Explanation: After merging the two non-decreasing arrays, we get, 2 2 3 4 7 10
Input: a[] = [1, 5, 9, 10, 15, 20], b[] = [2, 3, 8, 13]
Output:
123589
10 13 15 20
Explanation: After merging two sorted arrays we get 1 2 3 5 8 9 10 13 15 20.
Input: a[] = [0, 1], b[] = [2, 3]
Output:
01
23
Explanation: After merging two sorted arrays we get 0 1 2 3.
Constraints:
1 <= a.size(), b.size() <= 105
0 <= a[i], b[i] <= 107

6.Given an array of positive integers. Your task is to rearrange the array elements alternatively
i.e. first element should be the max value, the second should be the min value, the third should be
the second max, the fourth should be the second min, and so on.
Note: Modify the original array itself. Do it without using any extra space. You do not have to
return anything.

Examples:

Input: arr[] = [1, 2, 3, 4, 5, 6]


Output: [6, 1, 5, 2, 4, 3]
Explanation: Max element = 6, min = 1, second max = 5, second min = 2, and so on... The
modified array is: [6, 1, 5, 2, 4, 3]
Input: arr[]= [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
Output: [110, 10, 100, 20, 90, 30, 80, 40, 70, 50, 60]
Explanation: Max element = 110, min = 10, second max = 100, second min = 20, and so on...
Modified array is : [110, 10, 100, 20, 90, 30, 80, 40, 70, 50, 60]
Input: arr[]= [1]
Output: [1]
Constraints:
1 <= arr.size <= 106
1 <= arr[i] <= 106
7.Given two positive integer arrays arr and brr, find the number of pairs such that xy >
yx (raised to power of) where x is an element from arr and y is an element from brr.

Examples :

Input: arr[] = [2, 1, 6], brr[] = [1, 5]


Output: 3
Explanation: The pairs which follow xy > yx are: 21 > 12, 25 > 52 and 61 > 16 .
Input: arr[] = [2 3 4 5], brr[] = [1 2 3]
Output: 5
Explanation: The pairs which follow xy > yx are: 21 > 12 , 31 > 13 , 32 > 23 , 41 > 14 , 51 > 15 .
Expected Time Complexity: O((N + M)log(N)).
Expected Auxiliary Space: O(1).

Constraints:
1 ≤ arr.size(), brr.size() ≤ 105
1 ≤ brr[i], arr[i] ≤ 103

8.Given an array of integers arr[]. Find the Inversion Count in the array.
Two elements arr[i] and arr[j] form an inversion if arr[i] > arr[j] and i < j.

Inversion Count: For an array, inversion count indicates how far (or close) the array is from
being sorted. If the array is already sorted then the inversion count is 0.
If an array is sorted in the reverse order then the inversion count is the maximum.
Examples:

Input: arr[] = [2, 4, 1, 3, 5]


Output: 3
Explanation: The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3).
Input: arr[] = [2, 3, 4, 5, 6]
Output: 0
Explanation: As the sequence is already sorted so there is no inversion count.
Input: arr[] = [10, 10, 10]
Output: 0
Explanation: As all the elements of array are same, so there is no inversion count.
Constraints:
1 ≤ arr.size() ≤ 105
1 ≤ arr[i] ≤ 104

9.Given an array arr[] containing only 0s, 1s, and 2s. Sort the array in ascending order.
You need to solve this problem without utilizing the built-in sort function.

Examples:

Input: arr[] = [0, 1, 2, 0, 1, 2]


Output: [0, 0, 1, 1, 2, 2]
Explanation: 0s 1s and 2s are segregated into ascending order.
Input: arr[] = [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]
Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2]
Explanation: 0s 1s and 2s are segregated into ascending order.

Follow up: Could you come up with a one-pass algorithm using only constant extra space?
Constraints:
1 <= arr.size() <= 106
0 <= arr[i] <= 2

10.Given an array of integers arr[], the task is to find the first equilibrium point in the array.

The equilibrium point in an array is an index (0-based indexing) such that the sum of all
elements before that index is the same as the sum of elements after it. Return -1 if no such point
exists.

Examples:

Input: arr[] = [1, 2, 0, 3]


Output: 2
Explanation: The sum of left of index 2 is 1 + 2 = 3 and sum on right of index 2 is 3.
Input: arr[] = [1, 1, 1, 1]
Output: -1
Explanation: There is no equilibrium index in the array.

Input: arr[] = [-7, 1, 5, 2, -4, 3, 0]


Output: 3
Explanation: The sum of left of index 3 is -7 + 1 + 5 = -1 and sum on right of index 3 is -4 + 3 +
0 = -1.
Constraints:
3 <= arr.size() <= 105
-104 <= arr[i] <= 104

11.You are given an array arr of positive integers. Your task is to find all the leaders in the
array. An element is considered a leader if it is greater than or equal to all elements to its right.
The rightmost element is always a leader.
Examples:

Input: arr = [16, 17, 4, 3, 5, 2]


Output: [17, 5, 2]
Explanation: Note that there is nothing greater on the right side of 17, 5 and, 2.
Input: arr = [10, 4, 2, 4, 1]
Output: [10, 4, 4, 1]
Explanation: Note that both of the 4s are in output, as to be a leader an equal element is also
allowed on the right. side
Input: arr = [5, 10, 20, 40]
Output: [40]
Explanation: When an array is sorted in increasing order, only the rightmost element is leader.
Input: arr = [30, 10, 10, 5]
Output: [30, 10, 10, 5]
Explanation: When an array is sorted in non-increasing order, all elements are leaders.
Constraints:
1 <= arr.size() <= 106
0 <= arr[i] <= 106

12.You are given the arrival times arr[] and departure times dep[] of all trains that arrive at a
railway station on the same day. Your task is to determine the minimum number of platforms
required at the station to ensure that no train is kept waiting.

At any given time, the same platform cannot be used for both the arrival of one train and the
departure of another. Therefore, when two trains arrive at the same time, or when one arrives
before another departs, additional platforms are required to accommodate both trains.

Examples:

Input: arr[] = [900, 940, 950, 1100, 1500, 1800], dep[] = [910, 1200, 1120, 1130, 1900, 2000]
Output: 3
Explanation: There are three trains during the time 9:40 to 12:00. So we need a minimum of 3
platforms.
Input: arr[] = [900, 1235, 1100], dep[] = [1000, 1240, 1200]
Output: 1
Explanation: All train times are mutually exclusive. So we need only one platform
Input: arr[] = [1000, 935, 1100], dep[] = [1200, 1240, 1130]
Output: 3
Explanation: All 3 trains have to be there from 11:00 to 11:30
Constraints:
1≤ number of trains ≤ 50000
0000 ≤ arr[i] ≤ dep[i] ≤ 2359
Note: Time intervals are in the 24-hour format(HHMM) , where the first two characters
represent hour (between 00 to 23 ) and the last two characters represent minutes (this will be <=
59 and >= 0).

13.Given an array arr of positive integers. Reverse every sub-array group of size k.

Note: If at any instance, k is greater or equal to the array size, then reverse the entire array. You
shouldn't return any array, modify the given array in place.

Examples:

Input: arr[] = [1, 2, 3, 4, 5], k = 3


Output: [3, 2, 1, 5, 4]
Explanation: First group consists of elements 1, 2, 3. Second group consists of 4,5.
Input: arr[] = [5, 6, 8, 9], k = 5
Output: [9, 8, 6, 5]
Explnation: Since k is greater than array size, the entire array is reversed.
Constraints:
1 ≤ arr.size(), k ≤ 107
1 ≤ arr[i] ≤ 1018

14.Given an array arr[] and an integer k where k is smaller than the size of the array, your task is
to find the kth smallest element in the given array.

Follow up: Don't solve it using the inbuilt sort function.

Examples :

Input: arr[] = [7, 10, 4, 3, 20, 15], k = 3


Output: 7
Explanation: 3rd smallest element in the given array is 7.
Input: arr[] = [2, 3, 1, 20, 15], k = 4
Output: 15
Explanation: 4th smallest element in the given array is 15.
Constraints:
1 <= arr.size <= 106
1<= arr[i] <= 106
1 <= k <= n
15.Given an array arr[] with non-negative integers representing the height of blocks. If the width
of each block is 1, compute how much water can be trapped between the blocks during the rainy
season.

Examples:

Input: arr[] = [3, 0, 1, 0, 4, 0 2]


Output: 10
Explanation: Total water trapped = 0 + 3 + 2 + 3 + 0 + 2 + 0 = 10 units.

Input: arr[] = [3, 0, 2, 0, 4]


Output: 7
Explanation: Total water trapped = 0 + 3 + 1 + 3 + 0 = 7 units.
Input: arr[] = [1, 2, 3, 4]
Output: 0
Explanation: We cannot trap water as there is no height bound on both sides.
Input: arr[] = [2, 1, 5, 3, 1, 0, 4]
Output: 9
Explanation: Total water trapped = 0 + 1 + 0 + 1 + 3 + 4 + 0 = 9 units.
Constraints:
1 < arr.size() < 105
0 < arr[i] < 103

16.Given an array arr, return true if there is a triplet (a, b, c) from the array (where a, b, and c
are on different indexes) that satisfies a2 + b2 = c2, otherwise return false.
Examples:

Input: arr[] = [3, 2, 4, 6, 5]


Output: true
Explanation: a=3, b=4, and c=5 forms apythagorean triplet.
Input: arr[] = [3, 8, 5]
Output: false
Explanation: No such triplet possible.
Input: arr[] = [1, 1, 1]
Output: false
Constraints:
1 <= arr.size() <= 105
1 <= arr[i] <= 103

17.Given an array arr[] of positive integers, where each value represents the number of
chocolates in a packet. Each packet can have a variable number of chocolates. There
are m students, the task is to distribute chocolate packets among m students such that -
i. Each student gets exactly one packet.
ii. The difference between maximum number of chocolates given to a student and minimum
number of chocolates given to a student is minimum and return that minimum possible
difference.

Examples:

Input: arr = [3, 4, 1, 9, 56, 7, 9, 12], m = 5


Output: 6
Explanation: The minimum difference between maximum chocolates and minimum chocolates
is 9 - 3 = 6 by choosing following m packets :[3, 4, 9, 7, 9].
Input: arr = [7, 3, 2, 4, 9, 12, 56], m = 3
Output: 2
Explanation: The minimum difference between maximum chocolates and minimum chocolates
is 4 - 2 = 2 by choosing following m packets :[3, 2, 4].
Input: arr = [3, 4, 1, 9, 56], m = 5
Output: 55
Explanation: With 5 packets for 5 students, each student will receive one packet, so the
difference is 56 - 1 = 55.
Constraints:
1 ≤ m <= arr.size ≤ 105
1 ≤ arr[i] ≤ 109
String

1.Given a string s, reverse the string without reversing its individual words. Words are separated
by spaces.

Note: The string may contain leading or trailing spaces, or multiple spaces between two words.
The returned string should only have a single space separating the words, and no extra
spaces should be included.

Examples :

Input: s = " i like this program very much "


Output: "much very program this like i"
Explanation: After removing extra spaces and reversing the whole string (not individual words),
the input string becomes "much very program this like i".
Input: s = " pqr mno "
Output: "mno pqr"
Explanation: After removing extra spaces and reversing the whole string, the input string
becomes "mno pqr".
Input: s = " a "
Output: "a"
Explanation: The input string contains only one word with extra spaces around it. After
removing the extra spaces, the output is "a".Constraints:
Constraints:
1 <= s.size() <= 106
String s contains only lowercase English alphabets and spaces.
2.Given a string s, which may contain duplicate characters, your task is to generate and return an
array of all unique permutations of the string. You can return your answer in any order.

Examples:

Input: s = "ABC"
Output: ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"]
Explanation: Given string ABC has 6 unique permutations.
Input: s = "ABSG"
Output: ["ABGS", "ABSG", "AGBS", "AGSB", "ASBG", "ASGB", "BAGS", "BASG",
"BGAS", "BGSA", "BSAG", "BSGA", "GABS", "GASB", "GBAS", "GBSA", "GSAB",
"GSBA", "SABG", "SAGB", "SBAG", "SBGA", "SGAB", "SGBA"]
Explanation: Given string ABSG has 24 unique permutations.
Input: s = "AAA"
Output: ["AAA"]
Explanation: No other unique permutations can be formed as all the characters are same.
Constraints:
1 <= s.size() <= 9
s contains only Uppercase english alphabets

3.Given a string s, your task is to find the longest palindromic substring within s.

A substring is a contiguous sequence of characters within a string, defined as s[i...j] where 0 ≤ i


≤ j < len(s).

A palindrome is a string that reads the same forward and backward. More formally, s is a
palindrome if reverse(s) == s.
Note: If there are multiple palindromic substrings with the same length, return the first
occurrence of the longest palindromic substring from left to right.

Examples :

Input: s = “forgeeksskeegfor”
Output: “geeksskeeg”
Explanation: There are several possible palindromic substrings like “kssk”, “ss”, “eeksskee”
etc. But the substring “geeksskeeg” is the longest among all.
Input: s = “Geeks”
Output: “ee”
Explanation: "ee" is the longest palindromic substring of "Geeks".
Input: s = “abc”
Output: “a”
Explanation: "a", "b" and "c" are longest palindromic substrings of same length. So, the first
occurrence is returned.
Constraints:
1 ≤ s.size() ≤ 103
s consist of only lowercase English letters.

4.Given a string s, remove all its adjacent duplicate characters recursively, until there are no
adjacent duplicate characters left.

Note: If the resultant string becomes empty, return an empty string.

Examples:

Input: s = "geeksforgeek"
Output: "gksforgk"
Explanation: g(ee)ksforg(ee)k -> gksforgk
Input: s = "abccbccba"
Output: ""
Explanation: ab(cc)b(cc)ba->abbba->a(bbb)a->aa->(aa)->""(empty string)

Input: s = "abcd"
Output: "abcd"
Explanation: There are no adjacent duplicate characters
Constraints:
1<= s.size() <= 105

5.Given two strings s1 and s2. Return true if the string s2 can be obtained by rotating (in any
direction) string s1 by exactly 2 places, otherwise, false.

Note: Both rotations should be performed in same direction chosen initially.

Examples:

Input: s1 = "amazon", s2 = "azonam"


Output: true
Explanation: "amazon" can be rotated anti-clockwise by two places, which will make it as
"azonam".
Input: s1 = "geeksforgeeks", s2 = "geeksgeeksfor"
Output: false
Explanation: If we rotate "geeksforgeeks" by two place in any direction, we won't get
"geeksgeeksfor".

Input: s1 = "ab", s2 = "ab"


Output: true
Explanation: If we rotate "ab" by two place in any direction, we always get "ab".
Challenge: Try doing it in O(1) space complexity

Constraints:
1 ≤ s1.length, s2.length ≤ 105

6.Given a string in Roman number format (s), your task is to convert it to an integer. Various
symbols and their values are given below.
Note: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000

Examples:

Input: s = "IX"
Output: 9
Explanation: IX is a Roman symbol which represents 10 – 1 = 9.
Input: s = "XL"
Output: 40
Explanation: XL is a Roman symbol which represents 50 – 10 = 40.
Input: s = "MCMIV"
Output: 1904
Explanation: M is 1000, CM is 1000 – 100 = 900, and IV is 4. So we have total as 1000 + 900 +
4 = 1904.
Constraints:
1<= roman number <=3999
s[i] belongs to [I, V, X, L, C, D, M]

7.Given two strings s1 and s2 consisting of lowercase characters. The task is to check whether
two given strings are an anagram of each other or not. An anagram of a string is another string
that contains the same characters, only the order of characters can be different. For example,
"act" and "tac" are an anagram of each other. Strings s1 and s2 can only contain lowercase
alphabets.

Note: You can assume both the strings s1 & s2 are non-empty.

Examples :

Input: s1 = "geeks", s2 = "kseeg"


Output: true
Explanation: Both the string have same characters with same frequency. So, they are anagrams.
Input: s1 = "allergy", s2 = "allergic"
Output: false
Explanation: Characters in both the strings are not same, so they are not anagrams.
Input: s1 = "g", s2 = "g"
Output: true
Explanation: Character in both the strings are same, so they are anagrams.
Constraints:
1 ≤ s1.size(), s2.size() ≤ 105

8.Given a string s without spaces, the task is to remove all duplicate characters from it, keeping
only the first occurrence.

Note: The original order of characters must be kept the same.

Examples :

Input: s = "zvvo"
Output: "zvo"
Explanation: Only keep the first occurrence
Input: s = "gfg"
Output: "gf"
Explanation: Only keep the first occurrence
Constraints:
1 <= s.size() <= 105
str contains lowercase English alphabets

9.Given a string, find the minimum number of characters to be inserted to convert it to


a palindrome string.

Examples :

Input: str = "abcd"


Output: 3
Explanation: Inserted character marked with bold characters in dcbabcd, here we need
minimum three characters to make it palindrome.
Input: str = "aa"
Output: 0
Explanation: "aa" is already a palindrome.

Constraints:
1 ≤ |str| ≤ 500
str contains only lowercase alphabets
10.Given a string s, find the length of the longest substring with all distinct characters.

Examples:

Input: s = "geeksforgeeks"
Output: 7
Explanation: "eksforg" is the longest substring with all distinct characters.
Input: s = "aaa"
Output: 1
Explanation: "a" is the longest substring with all distinct characters.

Input: s = "abcdefabcbb"
Output: 6
Explanation: The longest substring with all distinct characters is "abcdef", which has a length of
6.
Constraints:
1<= s.size()<=3*104
All the characters are in lowercase.

11.Given a string s, the objective is to convert it into integer format without utilizing any built-in
functions. Refer the below steps to know about atoi() function.

Cases for atoi() conversion:

1. Skip any leading whitespaces.


2. Check for a sign (‘+’ or ‘-‘), default to positive if no sign is present.
3. Read the integer by ignoring leading zeros until a non-digit character is encountered or
end of the string is reached. If no digits are present, return 0.
4. If the integer is greater than 231 – 1, then return 231 – 1 and if the integer is smaller than -
231, then return -231.
Examples:

Input: s = "-123"
Output: -123
Explanation: It is possible to convert -123 into an integer so we returned in the form of an
integer

Input: s = " -"


Output: 0
Explanation: No digits are present, therefore the returned answer is 0.

Input: s = " 1231231231311133"


Output: 2147483647
Explanation: The converted number will be greater than 231 – 1, therefore print 231 – 1 =
2147483647.
Input: s = "-999999999999"
Output: -2147483648
Explanation: The converted number is smaller than -231, therefore print -231 = -2147483648.
Input: s = " -0012gfg4"
Output: -12
Explanation: Nothing is read after -12 as a non-digit character ‘g’ was encountered.
Constraints:
1 ≤ |s| ≤ 15

12.Find the first occurrence of the string pat in the string txt. The function returns an integer
denoting the first occurrence of the string pat in txt (0-based indexing).

Note: You are not allowed to use the inbuilt function. If there is no occurrence then return -1.

Examples :

Input: txt = "GeeksForGeeks", pat = "Fr"


Output: -1
Explanation: Fr is not present in the string GeeksForGeeks as substring.
Input: txt = "GeeksForGeeks", pat = "For"
Output: 5
Explanation: For is present as substring in GeeksForGeeks from index 5 (0 based indexing).

Input: txt = "GeeksForGeeks", pat = "gr"


Output: -1
Explanation: gr is not present in the string GeeksForGeeks as substring.
Constraints:
1 <= txt.size(),pat.size() <= 1000

13.Given an array of strings arr[]. Return the longest common prefix among each and every
strings present in the array. If there's no prefix common in all the strings, return "".

Examples :

Input: arr[] = ["geeksforgeeks", "geeks", "geek", "geezer"]


Output: "gee"
Explanation: "gee" is the longest common prefix in all the given strings.

Input: arr[] = ["hello", "world"]


Output: ""
Explanation: There's no common prefix in the given strings.
Constraints:
1 ≤ |arr| ≤ 103
1 ≤ |arr[i]| ≤ 103

You might also like