Posts

Swap Lex Order - CodeSignal

Given a string str and an array of pairs that indicate which indices in the string can be swapped, the function solution(str, pairs) returns the lexicographically largest string that results from doing the allowed swaps. Indices can be swapped any number of times. Example: For str = "abdc" and pairs = [[1, 4], [3, 4]] , the output should be solution(str, pairs) = "dbca" . By swapping the given indices, you get the strings: "cbda", "cbad", "dbac", "dbca". The lexicographically largest string in this list is "dbca". Input/Output: Execution time limit: 4 seconds (py3) str : a string consisting only of lowercase English letters with guaranteed constraints of 1 ≤ str.length ≤ 104 . pairs : an array containing pairs of indices that can be swapped in str (1-based). This means that for each pairs[i] , you can swap elements in str that have the indices pairs[i][0] and pairs[i][1] with guaranteed constraints of 0 ≤ pair

Maximum Sum Rectangle in a 2D array

Image
           Sample Test case:                                   Answer                                                                  Naive Approach:           The naive approach to find the maximum sum rectangle in a 2D array involves generating all possible submatrices and calculating their sum, and then selecting the maximum sum submatrix among them.  Each submatrix is generated by selecting its top-left corner and bottom-right corner.            Although this approach is simple to implement, it is not efficient for large inputs since its time complexity is very high. Therefore, more efficient algorithms like Kadane's algorithm for 2D arrays are used to solve this problem. Code: (C++)           #include < iostream > #include < bits/stdc++.h > using namespace std ; int maxSumRectangle ( vector < vector < int >> & arr , int n , int m ) { int nrow = arr . size (); int ncol = arr [ 0 ] . size (); int maxSum = INT_MIN ; fo

Product Sum - Interviewbit

Product Sum Understanding the problem We are given a "special" array, which contains integers and optionally other "special" arrays. We are asked to write a function that is going to return the sum of the elements in the "special" array. If the element is a "special" array, we need to sum up the elements in the "special" array and then multiply the sum by the depth of the "special" array. For instance, if the input array is  [3, [4, 5]] , then the result should be  3 + 2 * (4 + 5) = 21 ; if the input array is  [6, [4, [5]]] , then the result should be  6 + 2 * (4 + 3 * 5) = 44 . Approach One might notice that the "special" array has a recursive structure. Therefore, the problem can be solved recursively. We are going to have a recursive function which takes in a "special" array and the depth of the "special" array as arguments. The depth of the initial input array is going to be  1 . The recursive

SPECIAL STRING ARRAY

  Consider a non-empty string array inarr and a string instr. Identify and print outarr, based on the below logic: • For the string instr, generate a list of special strings specialstringlist. o List of special strings A special string for a string stris generated by right shiftingthe characters by one position. Last character will be shifted as the first character The newly generated special string is used as str for generating the next special string in the list Repeat the generation until the original string stris reached. o List of special strings would contain the string str. • Starting from the leftmost element, for each element in inarr. o Add the element of inarr to outarr if it is a subsequence in specialstringlist § Subsequence: A subsequence of a string str in the possible set of characters formed from string starting from left to right • If none of the elements from inarr is a subsequence of specialstringlist print -1 Note:  Perform case-insensitive comparison Input Format