I am using HackerRank which will input 4 numbers (3, 4, 6, 5) respectively for the parameters and the task is to be able to return the highest number, however when I compile this program it will output 3 (a) instead of 6 (b) and I am unsure where I am going wrong.
#include <iostream>
#include <cstdio>
using namespace std;
int max_of_four(int a, int b, int c, int d) {
if ((a > b) && (a > c) && (a > d)) {
return a;
}
if ((b > a) && (b > c) && (b > d)) {
return a;
}
if ((c > b) && (c > a) && (c > d)) {
return a;
}
else {
return d;
}
}
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int ans = max_of_four(a, b, c, d);
printf("%d", ans);
return 0;
}
Instead have this simpler version:
int max_of_four(int a, int b, int c, int d) {
int max = a;
if(b > max) max = b;
if(c > max) max = c;
if(d > max) max = d;
return max;
}
second and third return statements return a instead of b and c.
You are not returning right variables in 2nd and 3rd if statements. Return b and c.
Related
Example:
Input: arr = [0,1,2,3,4,5,6,7,8]
Output: [0,1,2,4,8,3,5,6,7]
Explanation: [0] is the only integer with 0 bits.
[1,2,4,8] all have 1 bit.
[3,5,6] have 2 bits.
[7] has 3 bits.
The sorted array by bits is [0,1,2,4,8,3,5,6,7]
I have tried to use custom sorting in C++ but I am not understanding where did I go wrong!
Here's my code!
class Solution {
public:
static int setbits(int temp) {
int c, n = temp;
while(n > 0) {
if(n & 1) c++;
n = n >> 1;
}
return c;
}
static bool myfun(int a, int b) {
int c1 = setbits(a);
int c2 = setbits(b);
if(c1 == c2 || c1 < c2) return a < b;
return a > b;
}
vector<int> sortByBits(vector<int>& arr) {
sort(arr.begin(), arr.end(), myfun);
return arr;
}
};
You forgot to initialize c in the setbits function:
int c = 0;
Moreover, there was a problem in the logic of the comparator. I get correct result with
if (c1 == c2) return a < b;
return c1 < c2;
Note that the code could be more efficient by first calculating the weight of all numbers and keep them in a array.
Replace
if(c1 == c2 || c1 < c2) return a < b;
return a > b;
with
return (c1 <= c2 );
when i am running below the code i am getting error but the same code run without making the function larger() code runs well. The code main purpose is to find largest of four number.
`code:
#include <iostream>
using namespace std;
int main(){
int a,b,c,d,z,;
int larger(a,b,c,d){
return a * (a >= b) * (a >= c) * (a >= d) +
b * (b > a) * (b >= c) * (b >= d) +
c * (c > a) * (c > b) * (c >= d) +
d * (d > a) * (d > b) * (d > c) ;
}
z = larger(2,10,12,5);
cout<<z;``
return 0;
}
I guess this is only an exercise otherwise you might have a look at std::max. However, there two major issues with your program:
you cannot define larger in main. If you want larger to be defined inside of main you have to use a lamda function, as suggested by #Some programmer dude in the comments.
when passing parameters to a function, c++ requires a type specifier. If you fix this, then the expression list treated as compund expression in initializer error goes away. See this question.
Here is working code:
#include <iostream>
using namespace std;
int larger(const int a, const int b, const int c, const int d){
return a * (a >= b) * (a >= c) * (a >= d) +
b * (b > a) * (b >= c) * (b >= d) +
c * (c > a) * (c > b) * (c >= d) +
d * (d > a) * (d > b) * (d > c) ;
}
int main(){
int z = larger(2,10,12,5);
cout<<z;
return 0;
}
Live demo: godbolt
If you want to use a lambda function, this is the way to go:
#include <iostream>
using namespace std;
int main(){
// define a lambda function
auto larger = [](const int a, const int b, const int c, const int d) -> int {
return a * (a >= b) * (a >= c) * (a >= d) +
b * (b > a) * (b >= c) * (b >= d) +
c * (c > a) * (c > b) * (c >= d) +
d * (d > a) * (d > b) * (d > c) ;
};
int z = larger(2,10,12,5);
cout<<z;
return 0;
}
Live demo: godbolt
As a remark, using using namespace std is typically not considered good paractice. See this question.
I have implemented this solution for finding a root of a cubic function
f(x) = ax3 + bx2 + cx + d
given a, b, c, and d, ensuring it's being monotonic.
After submitting the solution to an online judge without being shown the test cases, I am being faced by a time limit error. a, b, c, and d guarantee that the function is monotonic and we know it is being continuous. The code first finds the interval [A, B] such that f(A) * f(B) < 0; then the code moves to implement the bisection search.
What I want to know is if there is some possibility to minimize the time complexity of my code so it passes the online judge. The input is a, b, c, d, and the output should be the root with an error 0.000001.
Code:
#include <iostream>
#include <algorithm>
//#include <cmath>
//#include <string>
using namespace std;
int f(double a, double b, double c, double d, double x) {
return x*(x*(a*x + b) + c) + d;
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
double a, b, c, d, A, B, x = 1, res;
cin >> a >> b >> c >> d;
//determinning the interval
double f_x = f(a, b, c, d, x);
if (a > 0) { // strictly increasing
if (f_x > 0) { B = 0;
while (f(a, b, c, d, x) >= 0) { x -= x; }
A = x; }
else { A = 0;
while (f(a, b, c, d, x) <= 0) { x += x; }
B = x; }
}
else { //strictly decreasing
if (f_x > 0) { A = 0;
while (f(a, b, c, d, x) >= 0) { x += x; }
B = x; }
else { B = 0;
while (f(a, b, c, d, x) <= 0) { x -= x; }
A = x; }
}
// Bisection Search
double l = A;
while ((B - A) >= 0.000001)
{
// Find middle point
l = (A + B) / 2;
// Check if middle point is root
if (f(a, b, c, d, l) == 0.0)
break;
// Decide the side to repeat the steps
else if (f(a, b, c, d, l)*f(a, b, c, d, A) < 0)
B = l;
else
A = l;
}
res = l;
cout.precision(6);
cout << fixed << " " << res;
return 0;
}
There is no need to determine the initial interval, just take [-DBL_MAX, +DBL_MAX]. The tolerance can be chosen to be 1 ULP.
The following code implements these ideas:
// This function will be available in C++20 as std::midpoint
double midpoint(double x, double y) {
if (std::isnormal(x) && std::isnormal(y))
return x / 2 + y / 2;
else
return (x + y) / 2;
}
int main() {
...
const auto fn = [=](double x) { return x * (x * (x * a + b) + c) + d; };
auto left = -std::numeric_limits<double>::max();
auto right = std::numeric_limits<double>::max();
while (true) {
const auto mid = midpoint(left, right);
if (mid <= left || mid >= right)
break;
if (std::signbit(fn(left)) == std::signbit(fn(mid)))
left = mid;
else
right = mid;
}
const double answer = left;
...
}
Initially, fn(x) can overflow and return inf. No special handling of this case is needed.
So i have a function
Vector getNthRoots(double a, double b, double c, int n)
{
Vector v;
int i;
v.length = 0;
double m, a2, b2, c2;
if (n % 2 == 0)
{
a2 = a;
b2 = b;
c2 = c;
if (a<0)
a2 = a*(-1);
if (b<0)
b2 = b*(-1);
if (c<0)
c2 = c*(-1);
m = floor(pow(max(a2, b2, c2),1/n));
for (i = 1; i <= m; i++)
if (pow(i, n) >= min(a2, b2, c2) && pow(i, n) <= max(a2, b2, c2))
{
v.values[v.length] = i;
v.length++;
v.values[v.length] = (-1)*i;
v.length++;
}
return v;
}
else {
for (i = ceil(pow(min(a, b, c),1/n)); i <= floor(pow(max(a, b, c),1/n)); i++)
if (pow(i, n) >= min(a, b, c) && pow(i, n) <= max(a, b, c))
{
v.values[v.length] = i;
v.length++;
}
return v;
}
}
This function is supposed to give you the numbers at power n (number^n) which are in the interval of min(a,b,c) and max(a,b,c);
Other functions/headers
double max(double a, double b, double c)
{
if (a >= b && a >= c)
return a;
if (b >= a && b >= c)
return b;
if (c >= a && c >= b)
return c;
return a;
}
double min(double a, double b, double c)
{
if (a <= b && a <= c)
return a;
if (b <= a && b <= c)
return b;
if (c <= a && c <= b)
return c;
return a;
}
#include <iostream>
#include <cmath>
using namespace std;
#define MAX_ARRAY_LENGTH 100
struct Vector
{
unsigned int length;
int values[MAX_ARRAY_LENGTH];
};
It seems i can`t receive the good answer . For example
for getNthRoots(32,15,37,5) it should return a vector [2] because 2^5 =32 which belongs to interval [15,37] but i don`t receive anything
or getNthRoots(32,1,7,5) it should return a vector [1,2] but i only receive 1 as answer
I am guessing here is the problem for (i = ceil(pow(min(a, b, c),1/n)); i <= floor(pow(max(a, b, c),1/n)); i++)but i don`t know how i could fix it
1/n evaluates to 0, because it is evaluated as an integer expression. Try replacing all the "1/n"s with "1.0/n"s.
Take care to handle the case where n is 0.
I read the binary gcd algorithm and tried to implement it .It worked. This is my code
int gcd2(int a, int b) {
int sh;
if (a == 0)
return b;
if (b == 0)
return a;
for (sh = 0; !((a | b) & 1); sh++) {
a >>= 1;
b >>= 1;
}
while (!(a & 1)) {
a >>= 1;
}
while(b) {
while (!(b & 1)) {
b >>= 1;
}
if (a > b) {
int t = a;
a = b;
b = t;
}
b = b - a;
}
return a << sh;
}
But doesn't work if I replace the last if with
if (b > a)
{
int t = a;
a = b;
b = t;
}
b = a -b;
I just thought that both should work since they are doing the same.But it doesn't work.
Can anyone explain it please?
Thanks in advance!
it is not the same: if you choose your second way, there is the chance that a always stays bigger then b. then you never get to svap variables, and b is always less then a after b=a-b, if b is positive
i think using
a=a-b
instead of
b=a-b
could do it