Programming Articles - Page 2016 of 3363

Bitwise ORs of Subarrays in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:59:30

851 Views

Suppose we have an array A of non-negative integers. For every (contiguous) subarray say B = [A[i], A[i+1], ..., A[j]] (with i

Possible Bipartition in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:54:50

241 Views

Suppose we have a set of N people (they are numbered 1, 2, ..., N), we would like to split everyone into two subgroups of any size. Now each person may dislike some other people, and they should not go into the same group. So, if dislikes[i] = [a, b], it indicates that it is not allowed to put the people numbered a and b into the same group. We have to find if it is possible to split everyone into two groups in this way.So if the input is like N = 4 and dislike = [[1, 2], [1, ... Read More

Spiral Matrix III in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:50:56

446 Views

Suppose we have a 2 dimensional grid with R rows and C columns, we start from (r0, c0) facing east. Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the grid is at the last row and column. We will walk in a clockwise spiral shape to visit every position in this grid. When we are at the outside the boundary of the grid, we continue our walk outside the grid (but may return to the grid boundary later.). We have to find a list of coordinates representing the positions ... Read More

Boats to Save People in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:45:23

725 Views

Suppose we have an array called people. Now the i-th person has weight people[i], and each boat can carry a maximum weight of limit. If each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. We have to find the minimum number of boats to carry every given person. So if the input is like [3,2,1,2], and limit is 3, then we need three boats. [(1,2), (2), (3)].To solve this, we will follow these steps −sort the people arrayi := 0, j := size of people array – 1, ret := 0while i

Stone Game in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:41:58

597 Views

Suppose we have two players Alex and Lee they play a game with piles of stones. There are an even number of piles that are arranged in a row, and each pile has some number of stones piles[i]. The objective of the game is to end with the most stones. When the total number of stones is odd, there are no ties. Alex and Lee take turns, with Alex starting first. In each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This will be continued until there are no ... Read More

Koko Eating Bananas in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:36:31

485 Views

Suppose we have N piles of bananas, the i-th pile has piles[i] bananas. Here the guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed is K. Each hour, she takes some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, then she consumes all of them instead, and won't eat any more bananas during this hour. Consider Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back. We have to find the minimum integer ... Read More

Length of Longest Fibonacci Subsequence in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:33:46

177 Views

Suppose we have a sequence X_1, X_2, ..., X_n is fibonacci-like if −n >= 3X_i + X_{i+1} = X_{i+2} for all i + 2 = 3 otherwise return 0.Let us see the following implementation to get better understanding −Example Live Demo#include using namespace std; class Solution {    public:    int lenLongestFibSubseq(vector & A) {       int ret = 0;       unordered_map m;       int n = A.size();       vector < vector > dp(n, vector (n));       for(int i = 0; i < n; i++){       ... Read More

Reordered Power of 2 in C++

Arnab Chakraborty
Updated on 19-Jul-2020 19:00:29

276 Views

Suppose we have a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is non-zero. We have to check whether we can do this in a way such that the resulting number is a power of 2. So if the number is like 46, then the answer will be true.To solve this, we will follow these steps −Define a method called count, this will take x as inputret := 0while x is not 0ret := ret + 10 ^ last digit of xx := x / 10return retFrom the main ... Read More

Prime Palindrome in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:27:11

1K+ Views

Suppose we have to find the smallest prime palindrome that is greater than or equal to N. So if the N is 13, then the smallest palindrome will be 101.To solve this, we will follow these steps −If N is in range 8 to 11, then return 11for i in range 1 to 99999s := i as a stringr := sreverse rnum := concatenate s and substring of r from index 1, then convert to numberif num >= N and num is prime, then return numreturn 0Let us see the following implementation to get better understanding −Example Live Demo#include using ... Read More

Score After Flipping Matrix in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:22:45

203 Views

Suppose we have a two dimensional matrix A where each value is 0 or 1. Here a move consists of choosing any row or column, and toggling each value in that row or column: changing all 0s to 1s, and all 1s to 0s. Now after making any number of moves, every row of this matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers. So our task is to find the highest possible score. If the input is like −001110101100The output will be 39 as after toggling, the matrix will ... Read More

Advertisements