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 2 months ago.
Improve this question
result = getResult( 2, 5)
int getResult(int m, int n)
{
int ans;
if (m < n)
if (n <= 10)
ans = m + n;
else
ans = m * n;
else
ans = n / m;
return (ans);
}
I am stuck between 10 and 2,
does the second else apply because the second if is true? or do i still go with the first else?
For m = 2, n = 5, the first two if conditions are valid: m < n and n <= 10. Thus, ans = m + n = 7 and it's not modified later on, so we expect 7 as the answer.
This can be much easier deduced if you properly format your code (I did it for you in this case). Also, if you use {} in if/else, that's way easier and less error-prone.
Related
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 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Can anyone explain to me what's happening in line number 4, and how to understand these types of loops in future.
I was solving this problem. I have used basic approaches like pow(2,n) and (1<<n), but it overflows. Then I got this solution, but I'm unable to understand that fourth line. I know how to use for() loops in C++, but I'm a bit confused because of starting, i.e. nothing is there i.e. for(; e > 0; e >>= 1).
long long modpow(long long b, int e)
{
long long ans = 1;
for (; e > 0; e >>= 1)
{
if (e & 1)
ans = (ans * b) % mod;
b = (b * b) % mod;
}
return ans;
}
The for loop has 3 components:
for (a; b; c) {
}
a runs at the start. The loop will break when b is no longer true, and after each iteration c is executed.
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 2 years ago.
Improve this question
Suppose you are given a list of numbers like this
[11,2,7,6,17,13,8,9,3,5,12]
The result will be a group of numbers list containing sub-list i.e.,
[[2,3],[5,6,7,8,9],[11,12,13],[17]]
Here is my solution to the problem:
List<List<int>> consecutive_groups(List<int> a) {
a.sort();
List<List<int>> result = [];
List<int> temp = [];
temp.add(a[0]);
for (int i = 0; i < a.length - 1; i++) {
if (a[i + 1] == a[i] + 1) {
temp.add(a[i + 1]);
} else {
result.add(temp);
temp = [];
temp.add(a[i + 1]);
}
}
result.add(temp);
return result;
}
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 6 years ago.
Improve this question
I'm having like an assesment exercise.
So given a Number, for example 12345, I must find out the sum sequence of the digits of the given number (1 + 2 +3 + 4 +5) and then add to it the result (15), and repeat this till the sum sequence of the last number is a digit (in this case is 6).
Example : 12345 + 15 + 6 = 12366;
666 + 24 + 6 = 696;
I've been thinkig to store the digits in an array, but then I realized the array's size is static. Now I'm thinking to make a linked list, but I'm not really sure. Does it involve linked lists?
Just guide me to the right path. What should I use?
There's no magic needed here. Just do the obvious computation on integers:
int reduce(int n)
{
int result = 0;
while (n != 0) { result += n % 10; n /= 10; }
return result;
}
int your_problem(int n)
{
int result = n;
while (n >= 10) { n = reduce(n); result += n; }
return result;
}
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 7 years ago.
Improve this question
For f(n) = f(n – 1) + f(n – 2^10), when 0 <= n < 2^10, f(n)=1 ,
write a function to compute f(n).(not using recursive method)
int compute_f(int n)
{
int result = 0;
...
return result
}
wondering is there any efficient way to do?
You can follow the same idea of fibonacci and do Dynamic Programming.
Pseudo code:
if n < 0:
//throw some exception
arr = new int[max(1024,n+1)]
for i = 0 to 1024:
arr[i] = 1
for i = 1024 to n+1:
arr[i] = arr[i-1] + arr[i-1024]
return arr[n]
Converting it to actual code is left for you.
Bonus: You can do it with O(1) extra space by holding an array of size 1024 and manipulating it and remembering your current place (use modolus operator) without changing the time complexity.
Here is my version, similar to the recursive version of Fibonacci.
int compute_f(int n)
{
if( n < 0)
return -1; //Error
if(n <= 1024)
return 1;
return (compute_f(n-1) + compute_f(n-1024));
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is the equation I'm working with (it's from a past exam question that I got wrong):
void foo(float[] array, int start, int end){
if((end-start) <= 1) return;
int x = (end-start) / 5;
int y = 2*x;
int z = 4*x;
foo(array,start,start+y);
for(index = y; index <z; index++){
array[index]++;
}
foo(array,start+z,end);
}
How would I go about coming up with a recurrence equation for this?
I'm not sure of the notation I should use since the function #recurrences is depending on the value of end-start...
T(1) = 1
T(N) = ____ + ____ + _____
for notation simplicity, lets call N = end-start
then:
foo(array,start,start+y); // T(2/5 * N)
for(index = y; index <z; index++) // 2/5 * N
foo(array,start+z,end); // T(N/5)
T(N) = T(2/5 * N) + 2/5 * N + T(N/5)
is that close enough?