How could I possibly get sum of elements between two given points in array if given:
n - Array length
m - Number of questions about array
a[n] - array numbers
m questions in format x y
Example
Input
3 3
-1 2 0
3 3
1 3
1 2
Output
0
1
1
Here is my code
#include <iostream>
#include <numeric>
using namespace std;
int main()
{
int n,m,x,y;
cin>>n>>m;
int a [n];
int s [m];
for (int i = 0; i < n; i++){
cin>>a[i];
}
for (int i = 0; i < m ; i++){
cin>>x>>y;
if(x == y){
s[i] = a[x-1];
continue;
}
s[i] = accumulate(a + x - 1, a + y,0);
}
for (int i = 0; i < m; i++){
cout<<s[i]<<endl;
}
return 0;
}
This is not a school homework, just a task from internet - it will be run on test server using Standard streams for input and result check.
I need this program to be run in less than 0.2 seconds with:
0 < n,m <= 50000
-1000 <= a[i] <= 1000
1<=x<=y<=n
How can I make it run more time efficiently?
A lot of time can be spent in the accummulate function when n and m are large.
Let's use following data in our example:
const int n = 10;
int a[n] = {6, 1, 3, 2, 9, 8, 7, 2, 0, 1};
Let's create an auxiliary array and fill it with sums from a[0] to a[i] for each i. This can be made in a single loop.
int s[n+1];
void precalculate() {
s[0] = 0;
for (int i = 0; i < n; ++i) {
s[i+1] = s[i] + a[i];
}
}
Then the whole accummulate function collapses to a single subtraction:
int accummulate(int i, int j) {
return s[j] - s[i-1];
}
It works well as it can be easily demonstrated:
int main() {
precalculate();
assert(accummulate(1, 1) == 6);
assert(accummulate(10, 10) == 1);
assert(accummulate(1, 10) == 39);
return 0;
}
Related
I'm practicing myself by doing some leetcode questions, however, I don't know why that's an overflow problem right here. I knew the way I sum the subarray was terrible, any tips for the sum of the subarray?
and the run time for this code would be forever
#include <numeric>
class Solution {
public:
int sumOddLengthSubarrays(vector<int>& arr) {
int size = arr.size();//5
int ans = 0;
int sumAll = 0;
int start = 3;
int tempsum;
for(int i =0; i< size; i++){ //sumitself
sumAll += arr[i];
}
ans = sumAll; //alreayd have the 1 index
if(size%2 == 0){//even number 6
int temp = size-1; //5
if(size == 2)
ans = sumAll;
else{
while(start <= temp){//3 < 5
for(int i = 0; i< size; i++){
for(int k =0; k< start; k++){//3
tempsum += arr[i+k];
if(i+k > temp) //reach 5
break;
}
}
start+=2;
}
}
ans+= tempsum;
}
else{//odd number
if(size == 1)
ans = sumAll;
else{
while(start < size){//3
for(int i = 0; i< size; i++){
for(int k =0; k< start; k++){//3
tempsum += arr[i+k];
if(i+k > size) //reach 5
break;
}
}
start+=2;
}
ans+= tempsum;
ans+= sumAll; //size index
}
}
return ans;
}
};
The problem is with arr[i+k]. The result of i + k can be equal to, or larger, than size. You check it after you have already gone out of bounds.
You should probably modify the inner loop condition so that never happens:
for(int k =0; k < start && (i + k) < size; k++){//3
Now you don't even need the inner check.
You can use prefix sum array technique and then for each index you can calculate the sub-array sum for each odd-length array using prefix sum array. I submitted the below solution in LeetCode and it beats runtime of 100% of submissions and memory usage of 56.95%
class Solution {
public:
int sumOddLengthSubarrays(vector<int>& arr) {
int n = arr.size();
vector<int> prefix(n+1,0);
int sum = 0;
prefix[1] = arr[0];
for(int i=1;i<n;i++)
prefix[i+1]=(arr[i]+prefix[i]);
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j+=2)
sum+=prefix[j+1]-prefix[i];
}
return sum;
}
};
https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/1263893/Java-100-one-pass-O(n)-with-explanation
class Solution {
public int sumOddLengthSubarrays(int[] arr) {
// alt solution: O(n)
//for each i:
// if(n -1 - i) is odd, then arr[i] is counted (n-1-i)/2 + 1 times, each from 0 to i, total ((n-i)/2+1)*(i+1) times
// if(n -1 - i) is even, then arr[i] is counted (n-1-i)/2 + 1 times, if starting subseq index diff with i is even;
// (n-1-i)/2 times, if starting index diff with i s odd, total (n-i)/2 *(i+1) + (i+1)/2
// if i is even i - 1, i - 3, .. 1, total (i -2)/2 + 1 = i / 2 = (i+1) / 2
// if i is odd i-1, i-3, .., 0 total (i-1)/2 + 1 = (i+1) / 2
int total = 0;
int n = arr.length;
for(int i = 0; i < n; i++)
total += (((n - 1 - i) / 2 + 1) * (i + 1) - ((n-i) % 2)*((i+1) / 2)) * arr[i];
return total;
}
}
Can't complete the task and need your help. With single digits all OK, but when i trying numbers like 9865, output is wrong.
Task:
Input:
The first line contains an integer š (0 ā¤ š ā¤ 1e6). The second line contains nine separated by a space
integers š1, š2,ā¦, š9(1 ā¤ šš ā¤ 1e5) - the value in gold of each digit (from 1 to 9, respectively).
Output:
Print the maximum number for which the player has enough gold. The value of the number is equal to the sum of the values
each digit included in it. If the player does not have enough gold for any number, then output ā1
EXAMPLE:
Input:
n = 2
a[9] = {1 2 3 4 4 4 4 4 4}
Output:
11
Note:
In the example, two units of gold may only be enough for one digit 2 or two digits 1, so the maximum number that can be applied is 11.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 9;
int main()
{
int a[MAX]; // array
int b[MAX];
unsigned int n; // gold
cin >> n;
for (int i = 0; i < MAX; i++)
{
cin >> a[i];
}
for (int i = 0; i < MAX; i++)
{
int count = n / a[i];
int post = n % a[i];
if (count == 0)
{
b[i] = 0;
}
else
{
for (int k = 0; k < count; k++)
{
if (post == 0)
{
string prom = to_string(a[i]); // int to string
if (count > 1)
{
prom += to_string(a[i]);
}
b[i] = atoi(prom.c_str()); // string to int
}
else
{
string prom = to_string(a[i]); // int to string
if (count > 1)
{
prom += to_string(a[i]) + to_string(post);
}
b[i] = atoi(prom.c_str()); // string to int
}
}
}
}
int maxnum = 0;
for (int i = 0; i < MAX; i++)
{
if (b[i] > maxnum)
{
maxnum = b[i];
}
}
cout << maxnum;
return 0;
}
I faced this problem in an interview challenge
K caterpillars are eating their way through N leaves, each caterpillar
falls from leaf to leaf in a unique sequence, all caterpillars start
at a twig at position 0 and falls onto the leaves at position between
1 and N. Each caterpillar j has an associated jump number Aj. A
caterpillar with jump number j eats leaves at positions that are
multiple of j. It will proceed in the order j, 2j, 3jā¦. till it
reaches the end of the leaves and it stops and build its cocoon. Given
a set A of K elements , we need to determine the number
of uneaten leaves.
Constraints:
1 <= N <= 109
1 <= K <= 15
1 <= A[i] <= 109
Input format:
N = No of uneaten leaves.
K = No. of caterpillars.
A = Array of integer.
jump numbers Output:
The integer nu. Of uneaten leaves
Sample Input:
10
3
2
4
5
Output:
4
Explanation:
[2, 4, 5] is the 3-member set of jump numbers. All leaves which are multiple of 2, 4, and 5 are eaten. Only 4 leaves which are numbered 1,3,7,9 are left.
the naive approach for solving this question is have a Boolean array of all N numbers, and iterate over every caterpillar and remember the eaten leaves by it.
int uneatenusingNaive(int N, vector<int> A)
{
int eaten = 0;
vector<bool>seen(N+1, false);
for (int i = 0; i < A.size(); i++)
{
long Ai = A[i];
long j = A[i];
while (j <= N && j>0)
{
if (!seen[j])
{
seen[j] = true;
eaten++;
}
j += Ai;
}
}
return N - eaten;
}
this approach passed 8 out of 10 test cases and give wrong answer for 2 cases.
another approach using Inclusion Exclusion principle, explanation for it can be found here and here
below is my code for the second approach
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a%b);
}
int lcm(int i, int j)
{
return i*j / gcd(i, j);
}
vector<vector<int>> mixStr(vector<vector<int>> & mix, vector<int>& A, unordered_map<int, int> & maxStart)
{
vector<vector<int>> res;
if (mix.size() == 0)
{
for (int i = 0; i < A.size(); i++)
{
vector<int> tmp;
tmp.push_back(A[i]);
res.push_back(tmp);
}
return res;
}
for (int i = 0; i<mix.size(); i++)
{
int currSlotSize = mix[i].size();
int currSlotMax = mix[i][currSlotSize - 1];
for (int j = maxStart[currSlotMax]; j < A.size(); j++)
{
vector<int> tmp(mix[i]);
tmp.push_back(A[j]);
res.push_back(tmp);
}
}
return res;
}
int uneatenLeavs(int N, int k, vector<int> A)
{
int i = 0;
vector<vector<int>> mix;
bool sign = true;
int res = N;
sort(A.begin(), A.end());
unordered_map<int,int> maxStart;
for (int i = 0; i < A.size(); i++)
{
maxStart[A[i]] = i + 1;
}
int eaten = 0;
while (mix.size() != 1)
{
mix = mixStr(mix, A, maxStart);
for (int j = 0; j < mix.size(); j++)
{
int _lcm = mix[j][0];
for (int s = 1; s < mix[j].size(); s++)
{
_lcm = lcm(mix[j][s], _lcm);
}
if (sign)
{
res -= N / _lcm;
}
else
{
res += N / _lcm;
}
}
sign = !sign;
i++;
}
return res;
}
this approach passed only one 1/10 test case. and for the rest of test cases time limit exceeded and wrong answer.
Question:
What am I missing in first or second approach to be 100% correct.
Using Inclusion-Exclusion theorem is correct approach, however, your implementation seems to be too slow. We can use bitmasking technique to obtain a O(K*2^K) time complexity.
Take a look at this:
long result = 0;
for(int i = 1; i < 1 << K; i++){
long lcm = 1;
for(int j = 0; j < K; j++)
if(((1<<j) & i) != 0) //if bit j is set, compute new LCM after including A[j]
lcm *= A[j]/gcd(lcm, A[j]);
if(number of bit set in i is odd)
result += N/lcm;
else
result -= N/lcm;
}
For your first approach, an O(N*K) time complexity algorithm, with N = 10^9 and K = 15, it will be too slow, and can cause memory limit exceed/time limit exceed.
Notice that lcm can be larger than N, so, additional check is needed.
I was tinkering with C++ and I was like let's make a sorting algorithm :)
I did something, but it didn't sort the array but instead the array was overwritten with the max number
I don't know where is my mistake because I ran the algorithm on paper (don't ask) and it was correct.
I tried every modification possible.
any help?
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int mn = 0, mx = 0;
int a[] = { 4, 8, 5, 2, 6, 9, 0, 3, 1, 7 };
for (int i = 0; i < 10; i++)
{
mn = a[i]; mx = a[i];
for (int j = i; j < 10 - i; j++)
{
mn = min(a[j], mn);
mx = max(a[j], mx);
}
swap(a[i], mn);
swap(a[10-1-i], mx);
}
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
}
You are not swapping array elements, but basically you write the min/max values to the corresponding places within the array. Their old value is simply overwritten. You need to track the positions of the min/max elements and swap accordingly, e.g. swap(a[i], a[min_pos]). Additionally, you could run your outer loop up until the middle of the array is reached as you put two elements into their place on each iteration.
Here's the working code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int mn = 0, mx = 0;
int a[] = { 4, 8, 5, 2, 6, 9, 0, 3, 1, 7 };
for (int i = 0; i < 10 / 2; i++)
{
int min_pos = i, max_pos = i;
for (int j = i; j < 10 - i; j++)
{
if (a[j] < a[min_pos]) {
min_pos = j;
} else if (a[j] > a[max_pos]) {
max_pos = j;
}
}
int min_val = a[min_pos];
int max_val = a[max_pos];
swap(a[i], a[min_pos]);
swap(a[10-1-i], a[max_pos]);
a[i] = min_val;
a[10-1-i] = max_val;
}
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
}
Note, you need to take care of 'special cases', e.g. when the min_pos and max_pos are at the ends of the interval - they would be swapped twice staying in their original positions.
Given n items with size Ai and value Vi, and a backpack with size m. What's the maximum value can you put into the backpack?
Have you met this question in a real interview? Yes
Example
Given 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a backpack with size 10. The maximum value is 9.
Note
You cannot divide item into small pieces and the total size of items you choose should smaller or equal to m.
int knapsack(int m, vector<int> A, vector<int> V) {
int dp[m + 1], tmp[m + 1];
for (int n = 1; n <= m; n++) {
//******the problem would disappear if i change n to start with 0
dp[n] = (n < A[0]) ? 0 : V[0] ;
tmp[n] = dp[n];
}
for (int i = 1; i < A.size(); i++) {
for (int n = 1; n <= m; n++) {
tmp[n] = dp[n];
}
for (int j = 1; j <= m; j++) {
if (j >= A[i]) {
dp[j] = max(tmp[j], (V[i] + tmp[j - A[i]]));
}
}
}
return dp[m];
}
I am failing the specific testcase and all other are fine(even larger m values)
m = 10, A = [2,3,5,7], V = [1,5,2,4]
Output: 563858905 (actually random every time) Expected: 9
I know this question is some what trivial but I'm really curious about the memory allocation process in this scenario
I'm guessing that it would be dangerous to use any array that is not initialized at the first memory location, can someone confirm with me?
I tried following code, a simpler version of yours;
#include <iostream>
using namespace std;
int knapsack(int m, int A[], int V[], int size) {
int dp[m+1], tmp[m+1];
for (int n = 1; n <= m; n++) { //*1*
dp[n] = (n < A[0]) ? 0 : V[0] ;
tmp[n] = dp[n];
}
for (int i = 1; i < 4; i++) { //*2*
for (int n = 1; n <= m; n++) { //*3*
tmp[n] = dp[n];
}
for (int j = 1; j <= m; j++) { //*4*
if (j >= A[i]) {
dp[j] = (tmp[j]> (V[i] + tmp[j - A[i]])? //*5*
tmp[j] :
(V[i] + tmp[j - A[i]])
);
}
}
}
cout << "answer:" << dp[m] << endl;
return dp[m];
}
int main(){
int a[] = {2,3,5,7};
int b[] = {1,5,2,4};
knapsack(10, a, b, 4);
return 0;
}
and got 8 as the answer, rather than a random number.
I'm not sure that my code is the correct version of yours, but I luckily noticed that the expression of V[i] + tmp[j-A[i]] at the line marked by "\\*5" accesses tmp[0] when j=2 and i=1, since A[1] == 2 and 2 >= A[1]. Thus it would not be safe without initialization of tmp[0] in this logic.
So, I guess you are right; the uninitialized value of tmp[0] may change the result value, (and in some cases the flow of the logic as well, at the conditional statement of line //*5.)