Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Question: Given an amount m and an array of numbers b[] print "Yes" if m can be formed by addition of any number of elements from the array without repeating them.
This is what I've come up with; how can I add a memoization matrix into it?
#include<stdio.h>
int check(int b[],int n,int m){
if(m==0){
return 1;
}
if(m<0){
return 0;
}
if(n<=0 && m>0){
return 0;
}
return check(b,n-1,m-b[n-1]) + check(b,n-1,m);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d",&n);
scanf("%d",&m);
int b[n];
for(int i=0;i<n;i++){
scanf("%d",&b[i]);
}
if(check(b,n,m)>0){
printf("Yes\n");
}else{
printf("No\n");
}
}
return 0;
}
Can any one help me put a memorization matrix into it ?
The key to the memoization cache consists of the first n elements of b, plus m. Form a string from these, and use it as a key to a hash table.
...But what's the point? With n decreasing in each recursion, there are only two situations where memoization will help:
When a non-recursive call check is made for exactly the same list of numbers and m as an earlier call to check.
When m-b[n-1] == m, which is to say when b[n-1] == 0.
The first situation is unlikely (and decreasingly so as the size of the list increases, which is when memoization would help the most), and the second is easy to avoid. Just replace
return check(b,n-1,m-b[n-1]) + check(b,n-1,m);
with
return b[n-1] == 0
? check(b,n-1,m) * 2
: check(b,n-1,m) + check(b,n-1,m-b[n-1]);
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have to do a code that print the average of the numbers in an array. For sum, I use a function that I used in another time to calculate the recursive sum, and only divide by n later. But it doesn't work.
Why should I do?
int aveg(int *a, int n){
if (n == 0){
return 0;
}
else{
return ((aveg(a, n - 1) + a[n-1]) /n);
}
}
Because (a + b + c)/3 != ((a/1 + b)/2 + c)/3.
Instead, you can calculate the sum recursively, then divide by n at the very end.
You can recursively compute the mean (with zero based indices) as follows:
A possible implementation is:
#include <stdio.h>
#include <stddef.h>
double mean(const double a[],const size_t n)
{
if(n==0)
return 0;
return (a[n-1]+(n-1)*mean(a,n-1))/n;
}
int main()
{
double a[]={1,2,3,4,5};
size_t n = sizeof(a)/sizeof(*a);
printf("mean: %f\n",mean(a,n));
}
Compared to your solution:
return ((aveg(a, n - 1) + a[n-1]) /n);
you forgot a (n-1) factor:
return (a[n-1]+(n-1)*mean(a,n-1))/n;
It works like calling itself (function) again and again until you get the true or your desire output.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hello I am beginner in C and I am reading 1.7 functions in K&R book. Below code is taken as is from the book.
int power(int m, int n);
int main()
{
int i;
for (i=0;i<10;++i)
printf("%d %d %d \n",i,power(2,i),power(-3,i));
return 0;
}
int power(int base, int n)
{
int i, p;
p=1;
for(i=1;i<=n;++i)
p=p*base;
return p;
}
I can not understand how this code works, especially this part:
int power(int base, int n)
{
int i, p;
p=1;
for(i=1;i<=n;++i)
p=p*base;
return p;
}
Here, where is returned p?
How this whole code raises number in power? And, relationship between these two parts of the code?
Any help is appreciated.
The code multiplies the base n times, which is essentially the definition of an integer exponent. What's important to recognize is that the loop executes n times, and each time it is multiplying p by the base.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
PROBLEM
How to make an array which could increase its length when needed during run-time.
PROBLEM SOLVED
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int *arr;
int count=0,i=3,j,n;
arr=(int*)malloc(count+1*sizeof(int)); /*here i set array size to 1*/
arr[count++]=2; /*stored 2 as array first element*/
printf("Find all prime numbers upto :: ");
scanf("%d",&n); /*n is the number up to which prime numbers are required*/
here:
{
while(i<=n) /*start with i=3*/
{
j=0;
while(arr[j]<=sqrt(i)) /*till array element value less than or equal to root of number under checking*/
{
if(i%arr[j]!=0) /*if remainder is not zero check*/
j++; /*divisibility with next array element*/
else
{
i++; /*if remainder is zero then*/
goto here; /*start checking for another number*/
}
}
printf("%d, ",arr[count-1]); /*printing the number which was proved to be prime last time*/
arr=(int *)realloc(arr,(count+1)*sizeof(int)); /*increasing array size by 1*/
arr[count++]=i; /*newly proved prime is stored as next array element*/
i++;
}
printf("%d, ",arr[count-1]); /*print last number proved to be prime*/
}
}
THANK YOU! STACKOVERFLOW
That was my first question in C here and this platform helped me a lot in solving my problem, understanding new concepts and having the proper code.
Your Logic is correct, but it's not so efficient, and also the code isn't correct.
I did some change to your code and now it works, and the changes were as follows:
scanf("%d",n) ==> scanf("%d",&n);
while (j == count) ==> while (j <= count)
and now it work!!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a c code
int foo( int *p, int n){
int a[81];
int i,v;
for( i = 0; i < n ; i++){
a[i]=p[i]
if( n == 1 ) return a[0];
v=foo ( a, n-1);
if( v > a[n-1] )
return v;
else
return a[n-1];
}
int main ( void ){
int b[81], i;
for( i = 0; i < 81; i++){
b[i]=rand();
foo(b,81);
return 0;
}
And i need to find out how many instances of variable a will exist( max number) my answer was 81 , but it was wrong and i cant find out what number it should be. How could i determine it?
The main will call the function 82 times and each time the func will call recursively itself 80 times in a loop of decreasing n items.
So altogether it will be 81*81.
EDIT: I didn't notice the return after first iteration so actually it's pretty small number.
Set the number of elements in a to be arbitrarily high.
Build a function to assign elements to a, and only use that function to set elements of a. You could use this function to make sure that you don't attempt to write outside the bounds of a. (Else the behaviour of your program will be undefined and so any results will be meaningless).
Encode a "watermark" static int in that function to record the largest index accessed.
Run your program and note the value of the variable set up for (3).
Declare a global counter - it will be initialized to zero. In the function, at the same scope as 'a' is declared, increment the counter.
Printf() it out at the end of main().
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am solving a problem on calculation of factorial and the challenge is as follows!
You are asked to calculate factorials of some small positive integers.
Input
An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines,
each containing a single integer n, 1<=n<=100.
Output
For each integer n given at input, display a line with the value of n!
My Code is giving me the correct solution but Time limit is exceeded which is 2 seconds:
The code is as follows:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void factorial(int N)
{
printf("\n\n");
int q,i,j,t,d,z;
float p=0.0;
for(i=2;i<=N;i++)
p=p+log10(i);
d=(int)p+1;//No of terms in the factorial
int *b;
//initialization of an array
b=(int *)malloc(d*sizeof(int));
b[0]=1;
for(i=1;i<N;i++)
b[i]=0;
//calculation of factorial
p=0.0;
for(j=2;j<=N;j++)
{
q=0;
p=p+log10(j);
z=(int)p+1;
for(i=0;i<N;i++)
{
t=(b[i]*j)+q;
q=t/10;
b[i]=t%10;
}
}
for(i=d-1;i>=0;i--)
printf("%d",b[i]);
}
int main()
{
int n,i,j;
scanf("%d",&n);
int *b;
b=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
factorial(b[i]);
return 0;
}
How can i make my program more efficient and produce the output in the given time limit?
This challenge is from HackerEarth
Since N is small, an efficient algorithm would be to pre-calculate all factorials:
BigNumberType fact[101]; // The numbers will become big, so you need (to create) a type to store it
fact[0] = 1.0;
for (i=0; i < 100; i++) {
fact[i+1] = multiply(fact[i], i);
}
After that, looking up the value is trivial.
Note:
It may be even more efficient to scan the input vector for the highest number and only calculate factorials up to that number.