Is this algorithm for the function correct? - algorithm

The question I'm trying to solve is...
What is the value returned by the following function? Express your answer as a function of n.
int v = 0;
int n = 100;
for (int i = 1; i <= n ; i++)
{
for( int j = n + 1; j < 2 * n; j++)
{
v = v + 1;
}
}
System.out.println(v);
Seems like I'm missing something but I don't know what. =/ Thank you.

I don't want to give the whole answer away.
First of, our function looks like this.
int v = 0;
for (int i = 1; i <= n; i++) {
for (int j = (n + 1); j <= (n*2); j++) {
v++;
}
}
System.out.println(v);
So, depending on n what will v give us?
Let's try it, let's try for n = 1 to 20
for (int n = 1; n < 20; n++) {
int v = 0;
for (int i = 1; i <= n; i++) {
for (int j = (n + 1); j <= (n*2); j++) {
v++;
}
}
System.out.println(v);
}
Try that! If you still are curious give a poke here.
Hint #2. It is a very specific function, a very common.

Related

Is declaring a new intger inside a loop changes the space complexity?

Is declaring a new intger inside a loop changes the space complexity of the metohd?
for exampe if i'm looking at the follwoing 2 methods, is both of the methods space complexity is O(1)? or in the first method becuase I'm declaring the variable c over and over until the loop end it's space complexity is O(n)?
public static int what (int []a) {
int temp = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
**int c = f(a, i, j);**
if (c % 2 == 0) {
if (j - i + 1 > temp)
temp = j - i + 1;
}
}
}
return temp;
}
public static int what (int []a) {
int temp = 0;
**int c;**
for (int i = 0; i < a.length; i++) {
for (int j = i; j < a.length; j++) {
**c = f(a, i, j);**
if (c % 2 == 0) {
if (j - i + 1 > temp)
temp = j - i + 1;
}
}
}
return temp;
}
Not sure if it's relevant to the question but also attahced the f method.
private static int f (int[]a, int low, int high)
{
int res = 0;
for (int i=low; i<=high; i++)
res += a[i];
return res;
}
When you declare a variable inside the for loop it goes out of scope when the iteration ends and gets re declared in the next iteration so you are not declaring n variables, you are declaring a variable n times

Time complexity single loop with two variables

What will be the time complexity of below code and why?
public static int[] Shuffle(int[] nums, int n)
{
int len = nums.Length;
int[] final = new int[2 * n];
int counter = 0;
for (int i = 0, j = n; i < n; i++, j++)
{
final[counter++] = nums[i];
final[counter++] = nums[j];
}
return final;
}
If we will have two loops as below then it will be considered as time complexity of O(n^2)
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
}
}
Complexity is O(n) because the cursor is looping from i = 0 until i = n-1. Number of variables doesn't matter when it comes to time complexity. (there is space complexity as well) However care,
for (int i = 0, j = n; i < n; i++, j++)
is completely different from
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{

Coin making problem in DP - getting wrong answer using 2dimensional memo table

When I am passing this Input I am getting wrong answer
coin[] = {5,6}
Amount(W) = 10
my answer = 1
Correct Answer should be 2 i.e {5,5}
void coin_make(int W, vector<int> coin){
int n = coin.size();
int dp[n+1][W+1];
for(int i = 0; i <=W; i++){
dp[0][i] = INT_MAX;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= W; j++){
if(coin[i-1] == j){
dp[i][j] = 1;
}
else if(coin[i-1] > j){
dp[i][j] = dp[i-1][j];
}
else {
dp[i][j] = min(dp[i-1][j],
1 + dp[i][j-coin[i-1]]);
}
}
}
cout<<dp[n][W];}
You're overflowing on dp[1][6], since you try to calculate 1 + INT_MAX. This error propagates further and finally the answer is not correct. When I ran it on my machine, I got -2147483648. You should use some other constant as "infinity" to prevent overflows (e.g. 2e9 (or -1, but this would require some additional if statements)). Then the code will work fine on your provided test case.

what's wrong in this heapifying algorithm

i'm not sure what wrong with my code and its not converting the array into heap.please help!!!
pointer a is the pointer to the array passing to the function(you must have figured out that by now) and z is the length of the array.
please do explain me why i'm wrong.
i'm noob at coding(you must have figured out that also by my code for sure).
thank you for your precious time.
int heapy(int *a,int z)
{
for(i = 0; i<z ;i++)
{ c[i] = a[i];
for(j = i; j >= 0; --j)
{ y = (j-1)/2;
if(c[j] > c[y])
{ temp = c[y];
c[y] = c[j];
c[j] = temp;
j = y;}
else
break;
}
}
}
First point: You don't need the loop over j and that is where you have your problem. That is true, that you should assign y value to j, but just after that you decrement j in loop, so finally you get y - 1.
What you should do is either just change line j = y; to j = y + 1, or change the loop to
y = (j - 1) / 2
while (c[j] > c[y]){
temp = c[y];
c[y] = c[j];
c[j] = temp;
j = y;
y = (j - 1) / 2;
}
Second point: please do not compress your code like this. New line after bracket is much more readable.
EDIT:
Full implementation in C++ looks like this:
int heapy(int *a, int *c, int z)
{
for (int i = 0; i < z; i++){
c[i] = a[i];
int j = i;
int y = (j - 1) / 2;
while(c[j] > c[y]){
int temp = c[y];
c[y] = c[j];
c[j] = temp;
j = y;
y = (j - 1) / 2;
}
}
}
If array of i elements is a heap than you should add element on its end and swap it with it's parents as long as they are less than it.
In short: your program is too long by three characters: just remove --j from it.

How do I find the time complexity of these 3 nested loops?

The task is to analyze the following algorithm and calculate its time complexity.
I solved it as taking nested loops are 3 so O(n^3).
How do I solve this problem?
MSS (A[], N) //Where N is size of array A[]
{
int temp = 0, MS = 0;
For (int i = 0; i < N; i++)
{
for(int j = i; j < N; j++)
{
temp = 0;
for(int k = i; k <= j; k++)
temp = temp + A[k];
if(temp > MS)
MS = temp;
}
}
return(MS);
}
Well, you can proceed formally as such:

Resources