int sum = 0, j;
for (j=0; j < N; j++)
for (j=0; j < 100; j++)
sum = sum +j;
I want to know how we can solve it, meaning how to find the time complexity of this code?
Related
So I have this block of code:
int sum=0;
for (int i=1; i<n; ++i){
for (int j=1; j<i*i; ++j){
if (j%i==0){
for (int k=0; k<j; ++k){
++sum;
}
}
}
}
and I figured this has a $O(n^5)$ complexity. I tried timing this to verify it but I couldn't tell if the best fit was of $n^4$ or $n^5$.
The complexity is n^4.
The reason is that the third for will run O(n^2) times instead of O(n^3) as you may have calculated. The if case will only be called (i*i)^(1/2) = O(n) times for each step of the outer for, because the number of multiples of i from 1 to i*i is exactly i = O(n).
So I have this block of code:
int sum=0;
for (int i=1; i<n; ++i){
for (int j=1; j<i*i; ++j){ // O(n)
if (j%i==0){
for (int k=0; k<j; ++k){ // O(n^2)
++sum; // O(n^4)
}
}
}
}
I am trying to solve this problem on codeforces using dynamic programming. I have made the recurrence which is of O(N^2) complexity but it is timing out. I know that the complexity of this solution can be reduced to O(N) via Convex hull optimization which is explained here. But I am not able to reduce the complexity. Please help.
This is my code.
#include <bits/stdc++.h>
using namespace std;
#define MAX 100005
typedef long long ll;
ll a[MAX],b[MAX],dp[MAX];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < n; i++)
cin >> b[i];
dp[0] = 0;
for(int i = 1; i < n; i++)
{
dp[i] = 1e18;
for(int j = 0; j < i; j++)
{
dp[i] = min(dp[i],dp[j] + a[i] * b[j]);
}
}
cout << dp[n-1];
}
I am totally stumped. I have been debugging this for hours. I am allocating a table of 100 UInt32s by 100. I am loading a table of values and writing them to the 2D array. For some bizarre reason when I get to row 67, column 0 the writes appear to wrap back around to row 0 element 0.
I have rewritten it to allocate a list of arrays rather than a single malloc. Same exact behavior. I have tried doing math for the index: _map[row * 100 + column] instead of _map[i,j] and that leads to other strange behavior. I was thinking maybe something is overflowing, but I can't see how since the data is so small. Obviously I am doing something stupid but I just... can't.. see it.
Code snippet:
_map = malloc(100 * 100 * sizeof(UInt32));
int i = 0;
for (i=0; i <_columns; i++)
{
columnList = [[lineList objectAtIndex:i] componentsSeparatedByString:#","];
int j = 0;
for (j=0; j < _rows; j++)
{
UInt32 dataInt = atoi([[columnList objectAtIndex:j] UTF8String]);
// Convert the data
NSDictionary* tDict = [fileMap objectForKey:[NSString stringWithFormat:#"%i", dataInt]];
int newVal = [[tDict objectForKey:#"convert"] integerValue];
_map[i,j] = (UInt32)newVal;
UInt32 y = _map[i,j];
// This trips at row 67 element 0
if (_map[0,0] != 1)
printf("Here\n");
}
}
Any help would be absolutely most awesomely appreciated.
As I mention below, this code gives the same problem in that it corrupts the first line. As if every row is the same row:
int** testMap = malloc(100 * 100 * sizeof(int));
int data = 0;
for (int i = 0; i<100; i++)
{
for (int j = 0; j < 100; j++)
{
testMap[i, j] = data;
data++;
printf("(%i, %i)", i,j);
}
printf ("\n");
}
This code works as intended, giving me the number from these 10 input fields. Now, I want them all added together to one number. (all the miTimesFrac results.) Any really effective way of doing this?
double fraction[10] = {methane, ethane, propane, nbutane, ibutane,npentane,ipentane,nhexane,nitrogen,oxygen};
double Mi[10] = {16.0426,30.064,44.0962,558.123,558.123,72.1498,72.1498,86.1766,28.0134,31.9988};
double miTimesFrac[10];
for (int i=0; i<10; ++i) miTimesFrac[i] = Mi[i] * fraction[i];
I've tried a few things, like this
double TMiTimesFrac;
for (int i=1; i<10; ++i) TMiTimesFrac = miTimesFrac[i]+miTimesFrac[i-1];
This returns the same value every time tho. Any functions i'm missing?
This:
double TMiTimesFrac;
for (int i=1; i<10; ++i)
TMiTimesFrac = miTimesFrac[i]+miTimesFrac[i-1];
Doesn't do the job, because you don't keep track of all the previous values of TMiTimesFrac, so that only the last value (miTimesFrac[9]+miTimesFrac[8]) contributes to the final value of TMiTimesFrac.
So first of all initialize TMiTimesFrac to zero. Then add every value to it:
double TMiTimesFrac= 0.0;
for(unsigned int i=0; i<10; i++)
{
TMiTimesFrac+=miTimesFrac[i];
}
Now you should be able to do it without pasting the code. I suggest to try it.
Let's say that we have two dimensional array of bools which represent map of simple game. Every cell of array can be set to true if cell is occupied by some game's object (else false).
We want to update map in loop regarding to objects' behaviour. Let's say (because of some reasons) we are not able to update only some regions of the map, we have to iterate every cell in the array and negate it's value if object has changed its position.
We know that in every array's update there are only few changes but array is huge. And now comes the question.
What is more optimal.
(1) This:
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Cols; j++)
{
bool newValue = update(i,j);
arr[i,j] = arr[i,j] != newValue ? newValue : arr[i,j];
}
(2) this:
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Cols; j++)
{
bool newValue = update(i,j);
if(newValue != arr[i,j])
arr[i,j] = newValue;
}
(3) or maybe this:
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Cols; j++)
{
arr[i,j] = update(i,j);
}
I think (1) is worst solution here. So (2) vs (3).
No 3. There is no reason to write the code like you did in #2. You can remove the if without any problems.