Softsquare
Softsquare
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.
Examples:
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:
Examples:
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:
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:
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:
Examples :
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:
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:
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:
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:
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:
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.
Examples :
Examples:
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:
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:
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 :
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 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.
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.
Examples:
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 :
8.Given a string s without spaces, the task is to remove all duplicate characters from it, keeping
only the first occurrence.
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
Examples :
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.
Input: s = "-123"
Output: -123
Explanation: It is possible to convert -123 into an integer so we returned in the form of an
integer
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 :
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 :