Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,041 views
in Online Assessments by Expert (44,360 points) | 1,041 views

3 Answers

0 like 0 dislike
Best answer
Given A & B find count of all num which falls between A & B(both inclusive), where num = x * (x+1)
A = 6
B = 20
Answer = 3

 

2 * 3 = 6
3 * 4 = 12
4 * 5 = 20

 

A>=6,12, 20<=B
by Expert (44,360 points)
0 like 0 dislike

I'm not sure how to solve it if the input is negative, but if you assume the number is positive the answer would be easy
first, get the min factor of the first number but satisfy the condition x*(x+1) :
the factors of 6 are 1,2,3,6 so we take 2

 

            for (int i = 1; i < a; i++)
                if (a % i == 0 && i*(i+1)==a){
                    Console.Write( i );
                    break;
                }

 

then find the max factor of the second number but satisfy the condition x*(x+1)
the factors of 20 are 1,2,4,5,10,20 so take 5

 

      for (int i = b; i>=1; i--)
                if (b % i == 0 && i*(i-1)==b){
                    Console.Write( i );
                    break;
                }

 

the answer is 5-2=3

 

you can find factors of the number:

 

         for (int i = 1; i <=a; i++)
                if (a % i == 0)
                    Console.Write( i );

 

not sure about my answer, but I hope it helps.

by Expert (44,360 points)
0 like 0 dislike
it doesn't matter for negative numbers, since product of two consecutive numbers is always >=0.
On this idea , if B<0 simple return 0

 

val = sqrt(B)
now if(val*(val+1)) <= B then that's your upper bound else do val--
similarly find limits for A and all numbers between val1, val2 will define your range.
just return val1 - val2 as your answer.
by Expert (44,360 points)