#### Category : algorithm

I’m trying to solve the following algorithmic problem: I’m given an array (it could be a standard C/C++ array, a vector<vector<T> >, or something else… whatever is most suited to this problem is fine) populated with random non-zero values. I know the size of the array, m x n. What I want to do is ..

I hava a linked list: struct Node { bool isEmpty; int size; struct Node *next; } the nodes are like a tree in the way for example : 4->4->8->16->16->16->64 How can i check for example that if nodes in bold are empty combine them together. Nodes can be murged only if : are empty have ..

question sample input/output first line is N K A B second line is s[i] for(int i=1;i<=N;i++){ for(int j=1;j<=K;j++){ long long tmp=A*max(0,B-abs(i-dp[i-1][j-1].path[dp[i-1][j-1].path.size()-1]); if(s[i]+dp[i-1][j-1]+tmp>dp[i-1][j]){ dp[i][j]=s[i]+dp[i-1][j-1]+tmp; }else{ dp[i][j]=dp[i-1][j]; } } I solve this problem using dp like 0/1 knapsack problem. But the case A*max(0,B-(xi+1-xi)) makes dp can’t work. I think my substructure of dp is wrong. Is there ..

Given F(0) = 0, F(1) = 1, F(2) = 2, F(3)=3 and F(n) = (F(n−1)−F(n−3) +F(n−4)))%m. Task is to implement an efficient algorithm to compute F(n) where n is a 10^5 bit number given in the binary representation and m < 10^6 #include<bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { /* input ..

I am currenly learning the Sprague-Grundy theorem of Game theory. But I am not able to understand how to apply it in questions. For example in the following question GAME3 Two girls – Ivica and Marica – play an interesting game. First, they randomly choose a natural number N. They also define M = 1. ..

I need to draw the closest thing to a major diagonal on a picture(irregular matrix). I already know that for a regular one the format is the following: for (i = 0; i < row; i++) { for (j = 0; j < column; j++) { if(i==j){ matrix[row][column] = color_of_diagonal; } } } But adapt ..

Binary-Decimal We are given a binary string S of length n, in which each character is either ‘1’ or ‘0’. And we are asked to perform several queries on the string. In each query we are given integers L and R. And we have to tell the value of sub-string S[l..r], in decimal representation. Sample ..