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)
}
}
}
}
Related
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?
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];
}
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.
I need to generate a random maze with given width and height. I could do this in Perl with Depth-first search algorithm, in which I use 2D arrays, something like this:
for my $i (0 .. $h - 1) {
for my $j (0 .. $w - 1) {
push #{ $cell[$i] }, '1';
}
push #{ $cell[$i] }, '0';
}
for my $i (0 .. $w) {
$cell[$h][$i] = '';
}
While in Objective C, there's no 2D array. I'm kind of lost now. What is the equivalent of 2D array in Objective C so that I pretty much can use the same data structure as in Perl?
Thanks.
One way is to use Objective-C style array:
NSMutableArray *cell = [NSMutableArray arrayWithCapacity:h];
for (int i=0; i<h; ++i) {
NSMutableArray *row = [NSMutableArray arrayWithCapacity:w];
for (int j=0; j<w; ++j) {
// use a random number
[row addObject:[NSNumber numberWithInt:rand()]];
}
// add one row
[cell addObject:row];
}
Another way is just to use C style array:
int **cell = malloc(h*sizeof(int *));
for (int i=0; i<h; ++i) {
cell[i] = malloc(w*sizeof(int));
for (int j=0; j<w; ++j) {
cell[i][j] = rand();
}
}
// after you used it remeber to free it
for (int i=0; i<h; ++i) {
free(cell[i]);
}
free(cell);
cell = NULL;
Use this github library to generate maze.
https://github.com/DoubleEqual/MazeGenerator-tool
The result looks like this for 24x24 maze:
A 2-Dimentional or N-Dimentional arrays are nothing but a way to store the data. If you want a 2-D array its fairly simple, but little bit tricky in obj-c, you have to creates 2nd level arrays, then insert it into first level.
See the code below :
NSMutableArray *twoDArray = [NSMutableArray new]; //this is first level
//below are second level arrays inserted in index 0 to 2.
[twoDArray insertObject:[NSMutableArray arrayWithObjects:#"00",#"01",#"02",nil] atIndex:0];
[twoDArray insertObject:[NSMutableArray arrayWithObjects:#"10",#"11",#"12",nil] atIndex:1];
[twoDArray insertObject:[NSMutableArray arrayWithObjects:#"20",#"21",#"22",nil] atIndex:2];
Am running this C program, but instead of answering "The answer is 10", it sends back the message: "The answer is 0", even though it breaks at the right time.
Can you tell me what's wrong?
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
int i;
for(int i = 0; i < 12; i++){
printf("Checking i = %d\n", i);
if(i + 90 == i * i) {
break;
}
}
printf("The answer is %d.\n", i);
}
The problem is you have two i's.
int main (int argc, const char * argv[])
{
int i; //Declares outer i
for(int i = 0; i < 12; i++) //Declares a NEW i
{
printf("Checking i = %d\n", i);
if(i + 90 == i * i)
{
break;
}
}
printf("The answer is %d.\n", i); //Uses the outer i
}
Basic scope confusion: You have two different variables called i: One in the outer scope of the main function body, and another, overriding one inside the for loop.
The outer variable is uninitialized, so in fact you have undefined behaviour.
What you mean to say is this:
int i;
for (i = 0; i < 12; i++)
/* ^^^^^ use existing variable! */
{
printf("Checking i = %d\n", i);
if (i + 90 == i * i)
{
break;
}
}
Could it be the extra "int"? You're declaring another instance of "i" in the for loop that goes out of scope when the loop exits.
for(int i = 0; i < 12; i++){
You're creating another i here, which hides the i outside the scope of the for loop.
Change to:
int i;
...
for (i = 0; i < 12; i++){
Because you've got two DIFFERENT variables "i" - the one in the inner scope (which you increment from 0..11), and the one in the outer scope. You print the one in the outer scope.
SOLUTION:
change "for (int i=...)" to "for (i=...)"