Found 33676 Articles for Programming

Number of Ways to Paint N × 3 Grid in C++ program

Arnab Chakraborty
Updated on 21-Jul-2020 09:15:13

168 Views

Suppose we have a grid whose size is n x 3 and we want to paint every cell of the grid with exactly one of the three colors. Here the colors that will be used are Red, Yellow and Green.Now there is a constraint, that is no two adjacent cells have the same color. We have n number of rows of the grid. Finally, we have to find the number of ways we can paint this grid. The answer may be very large so return it modulo 10^9 + 7.So, if the input is like 1, then the output will ... Read More

The Maze III in C++

Arnab Chakraborty
Updated on 21-Jul-2020 09:13:05

508 Views

Suppose there is a maze with empty spaces and walls and there is also a ball in that maze. The ball can go through empty spaces by rolling up (u), down (d), left (l) or right (r) directions, but it keeps rolling until hitting a wall. When the ball stops, it could choose the next direction. One hole is also in that maze. The ball will drop into the hole if it rolls on to the hole.So if we have the ball position, the hole position and the maze, we have to find out how the ball could drop into ... Read More

Substring with Concatenation of All Words in C++ Program

Arnab Chakraborty
Updated on 21-Jul-2020 09:09:36

221 Views

Suppose we have a string, s, and we also have a list of words, words present in the array are all of the same length. We have to find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.So if the input is like “barfoothefoobarman” and words are [“foo”, “bar”], then the output will be [0, 9]. This is because the substring starting at index 0 and 9 are “barfoo” and “foobar”.To solve this, we will follow these steps −Define a method called ok(), this will take ... Read More

Encode String with Shortest Length in C++

Arnab Chakraborty
Updated on 21-Jul-2020 09:06:42

672 Views

Suppose we have a non-empty string; we have to encode this string such that its encoded length will be minimum.The encoding rule is like − k[encoded_string], where the encoded_string inside [ ] is being repeated exactly k times. We have to keep in mind that k will be a positive integer and encoded string will not be empty or have extra space. We can assume that the input string contains only lowercase letters. If the encoding process does not make the string shorter, then do not encode that string.So, if the input is like "aaaaa", then the output will be ... Read More

Count The Repetitions in C++

Arnab Chakraborty
Updated on 21-Jul-2020 09:04:48

322 Views

Suppose we have two non-empty strings s1 and s2 (maximum 100 characters) and two numbers n1 and n2 both are in range 0 to 106. Now suppose the strings S1 and S2, where S1=[s1, n1] and S2=[s2, n2].S = [s, n] defines the string S which consists of n connected strings s. As an exdample, ["ab", 4] ="abababab".On the other hand, we cal also define that string s1 can be obtained from string s2 if we remove some characters from s2 such that it becomes s1. So, "abc" can be obtained from "abdbec" based on the definition, but it can ... Read More

Optimal Account Balancing in C++

Arnab Chakraborty
Updated on 21-Jul-2020 09:02:52

527 Views

Suppose a group of friends went on holiday and sometimes they lent each other money. So as an example, Amit paid for Bikram's lunch for $10. Then later Chandan gave Amit $5 for a taxi fare. We have to design a model where each transaction is taken as a tuple (x, y, z) which means person x gave person y $z.Assuming Amit, Bikram, and Chandan are person 0, 1, and 2 respectively the transactions can be represented as [[0, 1, 10], [2, 0, 5]]. If we have a list of transactions between a group of people, we have to find ... Read More

LFU Cache in Python

Arnab Chakraborty
Updated on 21-Jul-2020 09:00:43

2K+ Views

Suppose we want to design and implement a data structure for Least Frequently Used (LFU) cache system. It should support the following operations −get(key) – This will be used to get the value of the key if the key exists in the cache, otherwise return -1.put(key, value) – This will be used to set or insert the value if the key is not already present.When the cache reached its maximum capacity, it should invalidate the least frequently used element before inserting a new element.So, if the LFUCache is initialized with capacity 2 and call these methods cache.put(1, 1); cache.put(2, 2); ... Read More

Encode N-ary Tree to Binary Tree in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:57:15

588 Views

Suppose we have a N-ary tree. We have to encode that tree into one binary. We also have to make deserializer to deserialize the binary tree to N-ary tree.So, if the input is likethen the output will beTo solve this, we will follow these steps −Define a function encode(), this will take root, if root is valid, then −return nullnode = new tree node with value of rootif size of children of root is not 0, then −left of node := encode(children[0] of root)curr = left of nodefor initialize i := 1, when i < size of children of root, ... Read More

Serialize and Deserialize N-ary Tree in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:53:52

410 Views

Suppose we have one N-ary 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 an N-ary tree. The N-ary tree is a rooted tree in which each node has no more than N children.So, if the input is likethen the output will be Serialize: 1 ... Read More

Word Squares in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:49:06

326 Views

Suppose we have a set of words (all are unique), we have to find all word squares and we can build from them. Here a sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < maximum of numRows and numColumns. So as an example, the word sequence ["ball", "area", "lead", "lady"] will construct a word square because each word reads the same both horizontally and vertically.ballarealeadladySo, if the input is like ["area", "lead", "wall", "lady", "ball"], then the output will be [[ "wall", "area", "lead", "lady"], ... Read More

Advertisements