Shubham Vora

Shubham Vora

793 Articles Published

Articles by Shubham Vora

Page 21 of 80

Length of longest subset consisting of A 0s and B 1s from an array of strings

Shubham Vora
Shubham Vora
Updated on 28-Jul-2023 211 Views

In this problem, we need to find the longest subset containing at most A 0s and B1s. All we need to do is find all possible subsets using the array elements and find the longest subset containing maximum A 0s and B1. In this tutorial, first, we will learn the recursive approach to solve the problem. After that, we will optimize the code using the dynamic programming approach. Problem statement − We have given an array containing N binary strings. Also, we have given A and B integers. We need to make the longest subset using the given binary strings ...

Read More

Construct a K-length binary string from an array based on given conditions

Shubham Vora
Shubham Vora
Updated on 28-Jul-2023 220 Views

In this tutorial, we require to construct a binary string of length K such that it should contain ‘1’ at the ith index if a subset-sum equal to I is possible using array elements. We will learn two approaches to solving the problem. In the first approach, we will use a dynamic programming approach to check whether the subset sum equal to index ‘I’ is possible. In the second approach, we will use a bitset to find all possible sums using array elements. Problem statement − We have given an array containing N integers. Also, we have given integer M ...

Read More

Check if a string can be split into 3 substrings such that one of them is a substring of the other two

Shubham Vora
Shubham Vora
Updated on 28-Jul-2023 215 Views

In this problem, we need to split the given string in such a way that the third substring can be a substring of the first two substrings. Let’s think about the solution. The third string can be a substring of the first two string only if the first two string contains all characters of the third string. So, we need to find at least one character in the given string with a frequency of more than 3, and we can take the third substring of the single character. Problem statement − We have given a string str containing the N ...

Read More

Check if a Binary String can be sorted in decreasing order by removing non-adjacent characters

Shubham Vora
Shubham Vora
Updated on 28-Jul-2023 242 Views

In this problem, we need to sort the given binary string in decreasing order by removing only non-adjacent elements. To solve the problem, we require to remove all zeros which are placed before ones in the binary string. If we find two consecutive ones after two consecutive zeros at any position in the string, it means we can’t sort the string in decreasing order. Otherwise, we can sort it out in each case. Problem statement − We have given binary string str with a length equal to N. We need to check whether we can sort the given string in ...

Read More

Minimum prefixes required to be flipped to convert a Binary String to another

Shubham Vora
Shubham Vora
Updated on 28-Jul-2023 330 Views

In this problem, we need to convert the first binary string to the second binary string by flipping the prefix of the first string. To get the minimum prefix flip, we require to iterate through the end of the string, and if we find mismatched characters in both strings, we need to flip the prefix of the first string. Problem statement − We have given two different binary strings called first and second. The length of both binary strings is equal, which is N. We need to convert the first string into the second string by flipping the prefixes of ...

Read More

Find the Number of \'X\' Total Shapes

Shubham Vora
Shubham Vora
Updated on 22-Jul-2023 245 Views

In this problem, we need to find the total number of ‘X’ shapes in the given matrix. We can construct the single ‘X’ shape using 1 or more adjacent ‘X’ elements. We can use the DFS (depth−first search) technique to solve the problem. For each ‘X’ element, we can find all adjacent elements using DFS and count it as a single ‘X’ shape. If we find a new ‘X’, we find its adjacent again. Here, we will use the iterative and recursive DFS to find the total number of ‘X” shapes. Problem statement − We have given a matrix[] of ...

Read More

Sum of Minimum and Maximum Elements of all Subarrays of Size K.

Shubham Vora
Shubham Vora
Updated on 22-Jul-2023 823 Views

In this problem, we need to take the maximum and minimum elements of all sub−array of length K and add them to get the answer. The first solution approach is that traverse through all sub−arrays of size K, find the minimum and maximum element of each sub−array, and add them. The optimized approach to solve the problem is using the deque data structure. We will store the index of the minimum and maximum elements of the subarray in the deque. Problem statement − We have given an array nums[] containing N positive or negative integer values. We have also ...

Read More

Reduce the Array to Atmost one Element by the Given Operations

Shubham Vora
Shubham Vora
Updated on 22-Jul-2023 189 Views

In this problem, we will reduce the array size to 1 or 0 by performing the given operations in each turn. We can sort the array in each turn to get the maximum elements in each iteration. Also, we can use the head data structure to improve the performance of the code. Problem statement − We have given a nums[] array. We need to decrease the array by performing the below operations. Choose two maximum elements of the array. If both elements are the same, remove both elements from the array. If both elements are not the same, remove ...

Read More

Rearrange and Update Array Elements as Specified by the given Queries

Shubham Vora
Shubham Vora
Updated on 22-Jul-2023 326 Views

In this problem, we will perform the given queries on the array elements. The queries contain the circular left rotation, right rotation, and updation of the array element. The logical part of solving the problem is array rotation. The naïve approach to rotating the array in the left direction is to replace each element with the next element and the last element with the first element. We can use the deque data structure to rotate the array efficiently. Problem statement − We have given an arr[] array containing the integer values. Also, we have given a queries[] array containing ...

Read More

Print all Prime Levels of a Binary Tree

Shubham Vora
Shubham Vora
Updated on 22-Jul-2023 178 Views

In this problem, we will print all prime levels of the given binary tree. We will use the level order traversal technique to traverse each binary tree level and check whether all nodes contain the prime integer for the particular level. Problem statement − We have given a binary tree and need to print all prime levels of the binary tree. It is given that if all nodes of any binary tree contain a prime integer, we can say a particular level is a prime level. Sample examples Input  ...

Read More
Showing 201–210 of 793 articles
« Prev 1 19 20 21 22 23 80 Next »
Advertisements