Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1848 of 3366
594 Views
Suppose we have a non-empty string s and an integer k; we have to rearrange the string such that the same characters are at least distance k from each other. Given strings are in lowercase letters. If there is no way to rearrange the strings, then we will an empty string.So, if the input is like s = "aabbcc", k = 3, then the output will be "abcabc" this is because same letters are at least distance 3 from each other.To solve this, we will follow these steps −ret := an empty stringDefine one map mn := size of sfor ... Read More
1K+ Views
Suppose we have a string; we have to calculate the length of the longest substring T that contains at most k distinct characters.So, if the input is like s = "eceba", k = 2, then the output will be 3 as T is "ece" which its length is 3.To solve this, we will follow these steps −ans := 0Define one map mn := size of sx := 0for initialize j := 0, i := 0, when j < n, update (increase j by 1), do −(increase m[s[j]] by 1)if m[s[j]] is same as 1, then −(increase x by 1)while (x > k and i k && i
585 Views
Suppose we have one matrix; we have to find the length of the longest increasing path. From each cell, we can either move to four directions − left, right, up or down. We cannot move diagonally or move outside of the boundary.So, if the input is like994668211then the output will be 4 as the longest increasing path is [3, 4, 5, 6].To solve this, we will follow these steps −Define a function solve(). This will take i, j, matrixif dp[i, j] is non-zero, thenreturn dp[i, j]dp[i, j] := 1temp := 0for r in range i-1 to i+2, dofor c in ... Read More
709 Views
Suppose we want to make a house on an empty land which reaches all buildings in the shortest amount of distance. We can only move four directions like up, down, left and right. We have a 2D grid of values 0, 1 or 2, where −0 represents an empty land which we can pass by freely.1 represents a building which we cannot pass through.2 represents an obstacle which we cannot pass through.So, if the input is like102010000000100then the output will be 7 as Given three buildings are present at (0, 0), (0, 4), (2, 2), and an obstacle is at ... Read More
500 Views
Suppose we have a 2D matrix called matrix, we have to calculate the sum of the elements inside the rectangle defined by its upper left corner and lower right corner.So, if the input is like3014256321120154101710305So will be methods to find sum, update value, if we call them likesumRegion(2, 1, 4, 3)update(3, 2, 2)sumRegion(2, 1, 4, 3) ,then the output will be 8 and 10, as the above rectangle (with the green color) is defined by (2, 1) and (4, 3), which contains sum = 8.To solve this, we will follow these steps −Define one 2D array treeDefine one 2D array ... Read More
428 Views
Suppose we have an image and that image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. Here the black pixels are connected, so there is only one black region. Pixels are connected horizontally and vertically. If we have a location (x, y) of one of the black pixels, we have to find the area of the smallest (axis-aligned) rectangle that encloses all black pixels.So, if the input is like001001100100and x = 0, y = 2, then the output will be 6To solve this, we will follow these steps −Define one ... Read More
939 Views
Suppose we have one binary tree and we have to serialize and deserialize them. As we know that the serialization is the process of converting a data structure or object into a sequence of bits so we can store them in a file or memory buffer, and that can be reconstructed later in the same or another computer environment.Here we have to devise an algorithm to serialize and deserialize binary tree. The binary tree is a rooted tree in which each node has no more than 2 children.So, if the input is likethen the output will be Serialize − 1 ... Read More
269 Views
Suppose there is a group of two or more people and they wants to meet and minimize the total travel distance. We have a 2D grid of values 0 or 1, where each 1 mark the home of someone in the group. The distance is calculated using the formula of Manhattan Distance, so distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.So, if the input is like100010000000100then the output will be 6 as from the matrix we can understand that three people living at (0, 0), (0, 4), and (2, 2): The point (0, 2) is an ideal meeting ... Read More
276 Views
Suppose we have a pattern and a string called str, we have to check whether str follows the same pattern or not. Here pattern follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str.So, if the input is like pattern is "abaa", str is "orangegreenorangeorange", then the output will be trueTo solve this, we will follow these steps −Define a function solve(), this will take i, j, ptr, s, a map m, one set called used, if i >= size of s and j >= size of ptr, ... Read More
447 Views
Suppose we have a binary search tree and a target value; we have to find k values in that BST that are closest to the target. We have to keep in mind that the target value is a floating-point number. We can assume k is always valid, and k ≤ total nodes.So, if the input is liketarget = 3.714286, and k = 2, then the output will be [4, 3]To solve this, we will follow these steps −Define a function pushSmaller(), this will take node, stack st, and target, while node is not present, do −if val of node < ... Read More