Found 26504 Articles for Server Side Programming

Rotate Function in C++

Arnab Chakraborty
Updated on 02-May-2020 08:29:25

330 Views

Suppose we have Given an array of integers A and let n is the length of array A. Now assume Bk to be an array obtained by rotating the array A, k positions clock-wise. Here the rotation can be defined as −F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1].Now find the maximum value of F(0), F(1), ..., F(n-1).So if the input is like A = [4, 3, 2, 6], then −F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 ... Read More

UTF-8 Validation in C++

Arnab Chakraborty
Updated on 02-May-2020 08:26:28

3K+ Views

Suppose we have a list integers representing the data. We have to check whether it is valid UTF-8 encoding or not. One UTF-8 character can be 1 to 4-byte long. There are some properties −For 1-byte character, the first bit is a 0, followed by its unicode code.For n-bytes character, the first n-bits are all 1s, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.So the encoding technique is as follows −Character Number RangeUTF-8 octet sequence0000 0000 0000 007F0xxxxxxx0000 0080 0000 07FF110xxxxx 10xxxxxx0000 0800 0000 FFFF1110xxxx 10xxxxxx 10xxxxxx0001 0000 0010 FFFF11110xxx 10xxxxxx 10xxxxxx ... Read More

Elimination Game in C++

Arnab Chakraborty
Updated on 02-May-2020 08:19:56

881 Views

Suppose we have a list of sorted integers from 1 to n. That is starting from left and ending at right, we have to remove the first number and every other number afterward until we reach the end of the list. We will repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers. We will repeat the steps again, alternating left to right and right to left, until one single number remains. We have to find the last number that remains starting with a list ... Read More

Linked List Random Node in C++

Arnab Chakraborty
Updated on 02-May-2020 08:17:15

402 Views

Suppose we have a singly linked list, we have to find a random node's value from the linked list. Here each node must have the same probability of being chosen. So for example, if the list is [1, 2, 3], then it can return random node in range 1, 2, and 3.To solve this, we will follow these steps −In the getRandom() method, do the following −ret := -1, len := 1, v := xwhile v is not nullif rand() is divisible by len, then ret := val of vincrease len by 1v := next of vreturn retExample(C++)Let us see ... Read More

Wiggle Subsequence in C++

Arnab Chakraborty
Updated on 02-May-2020 08:14:05

304 Views

Suppose we have a sequence of numbers that is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference may be either positive or negative. A sequence with less than two elements is trivially a wiggle sequence. So for example, [1, 7, 4, 9, 2, 5] is a wiggle sequence because if you see, the differences (6, -3, 5, -7, 3) are alternately positive and negative. But, [1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not wiggle sequences, the first one because its first two differences are ... Read More

Super Pow in C++

Arnab Chakraborty
Updated on 02-May-2020 08:11:59

538 Views

Suppose we have to calculate a^b mod 1337 where a is one positive integer and b is an extremely large positive integer given in the form of an array. So if a = 2 and b = [1, 0] then the output will be 1024To solve this, we will follow these steps −Define powerMod() method this takes base and powerm := 1337, ret := 1while power is not 0if power is odd, then ret := ret * base mod mbase := base^2 mod mpower := power / 2return retDefine superPower(), this takes a and bif size of b = 0, ... Read More

Largest Divisible Subset in C++

Arnab Chakraborty
Updated on 02-May-2020 08:08:35

126 Views

Suppose we have a set of distinct positive integers, we have to find the largest subset such that every pair like (Si, Sj) of elements in this subset satisfies: Si mod Sj = 0 or Sj mod Si = 0.So if the input is like [1, 2, 3], the possible result may come like [1, 2] or [1, 3]To solve this, we will follow these steps −Create an array ret, set endpoint := 0, retLen := 1, n := size of numsif n is 0, then return empty setsort nums arraycreate two arrays len and par of size n, initialize ... Read More

Water and Jug Problem in C++

Arnab Chakraborty
Updated on 02-May-2020 08:04:37

2K+ Views

Suppose we have two jugs with capacities x and y liters. There is an infinite amount of water supply available to us. Now we need to determine whether it is possible to measure exactly z liters using these two jugs. If z liters of water are measurable, we must have z liters of water contained within one or both buckets by the end.We can do these few operations −Fill any of the jugs fully with water.Empty any of the jugs.Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.So ... Read More

Count Numbers with Unique Digits in C++

Arnab Chakraborty
Updated on 02-May-2020 08:01:38

1K+ Views

Suppose we have a non-negative integer n. We have to count all numbers with unique digits x, where x is in range 0 to 10^n. So if the number n is 2, then the result will be 91, as we want to find numbers from 0 to 100 without 11, 22, 33, 44, 55, 66, 77, 88, 99.To solve this, we will follow these steps −if n is 0, then return 1n := min of 10 and nif n is 1, then return 10ans := 9 and ret := 10for i in range 2 to nans := ans * (9 ... Read More

Integer Break in C++

Arnab Chakraborty
Updated on 02-May-2020 07:56:49

294 Views

Suppose we have a positive integer n, we have to break it into the sum of at least two positive numbers and maximize the product of those integers. We have to find the maximum product we can get. So if the number is 10, then the answer will be 36, as 10 = 3 + 3 + 4, 3 * 3 * 4 = 36To solve this, we will follow these steps −Define a method solve(), this will take n, array dp and flagif n is 0, then return 1if dp[n] is not -1, then return dp[n]end := n – ... Read More

Advertisements