Found 26504 Articles for Server Side Programming

Longest Arithmetic Sequence in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:39:57

359 Views

Suppose we have an array A of integers, we have to return the length of the longest arithmetic subsequence in A. As you know that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0

Video Stitching in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:30:27

277 Views

Suppose we have a series of video clips from a sporting event that lasted T seconds. Now these video clips can be overlapping with each other and have varied lengths. Here each video clip clips[i] is an interval − it starts at clips[i][0] time and ends at clips[i][1] time. We can cut these clips into segments freely − We have to find the minimum number of clips needed so that we can cut the clips into segments that cover the entire sporting event ([0, T]). If the task is impossible, return -1. So if the input is like [[0, 2], ... Read More

Number of Enclaves in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:24:45

217 Views

Suppose we have given a 2D array A, now each cell is 0 (representing sea) or 1 (representing land) Here a move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid. We have to find the number of land squares in the grid for which we cannot walk off the boundary of the grid in any number of moves. So if the grid is like −0000101001100000The answer will be 3, as there are three ones enclosed by 0s, and one 1 is not enclosed.To solve this, we will follow these ... Read More

Convert to Base -2 in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:19:24

1K+ Views

Suppose we have a number N, we have to find a string consisting of "0"s and "1"s that represents its value in base -2 (negative two). The returned string should have no leading zeroes, unless the string is exactly "0". So if the input is like 2, then the output will be “110”, as (-2)^2 + (-2)^1 + (-2)^0 = 2.To solve this, we will follow these steps −ret := an empty stringif N = 0, then return “0”while N is non 0rem := N mod (– 2)N := N / (-2)if rem < 0 and rem := rem + ... Read More

Binary String With Substrings Representing 1 To N in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:16:22

268 Views

Suppose we have a binary string S and a positive integer N, we have to say true if and only if for every integer X from 1 to N, the binary representation of X is a substring of the given S. So if S = “0110” and N = 3, then the result will be true, as 1, 10 and 11 all are present in 0110.To solve this, we will follow these steps −Define a method to convert(), that will take n as inputret := an empty stringwhile n is not 0ret := ret concatenate n mod 2n := n ... Read More

Best Sightseeing Pair in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:12:21

244 Views

Suppose we have an array A of positive integers, now A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i. Now the score of a pair (i < j) of sightseeing spots is follows this formula (A[i] + A[j] + i - j): We have to find the maximum score of a pair of sightseeing spots. So if the input is like [8, 1, 5, 2, 6], then the output will be 11, as i = 0, j = 2, the value of A[0] + A[2] + 0 – ... Read More

Satisfiability of Equality Equations in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:08:53

245 Views

Suppose we have an array if equations that represent relationships between variables, now each string equations[i] has the length 4 and takes one of two different forms: "a==b" or "a!=b". Here, a and b are lowercase letters, that are representing one-letter variable names. So we have to find true if and only if it is possible to assign integers to variable names so as to satisfy all the given equations.If the input is like: ["a==b", "b==c", "a==c"], then the answer will be true.To solve this, we will follow these steps −Define a method called getParent(), this will take character x ... Read More

String Without AAA or BBB in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:04:45

277 Views

Suppose we have two integers A and B, we have to return any string S, such that −S has length A + B and contains exactly A number of letter ‘a’ and B number of ‘b’ letters.Substring “aaa” and “bbb” will not be in the string SSo if the given integers are A = 4, B = 1, then the result will be “aabaa”.To solve this, we will follow these steps −Define a string ret, initially this is emptywhile |A – B| >= 2, if A > B, thenret := ret concatenate ‘aa’decrease A by 2if B is non-zero concatenate ... Read More

Time Based Key-Value Store in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:59:02

833 Views

Suppose we have to make a timebased key-value store class called TimeMap, that supports two operations.set(string key, string value, int timestamp): This will store the key and value, along with the given timestamp.get(string key, int timestamp): This will return a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev

Longest Turbulent Subarray in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:54:39

416 Views

Consider a subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent when it meets these conditions −For i A[k+1] when k is odd, and A[k] < A[k+1] when k is even;Otherwise, for i A[k+1] when k is even, and A[k] < A[k+1] when k is odd.So the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray. Now find the length of a maximum size turbulent subarray of A. So if the input is like [9, 4, 2, 10, 7, 8, 8, 1, 9], output is 5. This ... Read More

Advertisements