Math, asked by jungkookie15, 3 months ago

Ram has an integer A. He needs to find two integers x and y such that their product is strictly greater than A and x+y is minimum Return the sum of x and y​

Answers

Answered by achus33
0

Examples :

Input: {34, 8, 10, 3, 2, 80, 30, 33, 1}

Output: 6 (j = 7, i = 1)

Input: {9, 2, 3, 4, 5, 6, 7, 8, 18, 0}

Output: 8 ( j = 8, i = 0)

Input: {1, 2, 3, 4, 5, 6}

Output: 5 (j = 5, i = 0)

Input: {6, 5, 4, 3, 2, 1}

Output: -1 Method 1 (Simple but Inefficient)

Run two loops. In the outer loop, pick elements one by one from left. In the inner loop, compare the picked element with the elements starting from the right side. Stop the inner loop when you see an element greater than the picked element and keep updating the maximum j-i so far. Method 2 –

Improvising the Brute Force Algorithm and looking for BUD, i.e Bottlenecks, unnecessary and duplicated works. A quick observation actually shows that we have been looking to find the first greatest element traversing from the end of the array to the current index. We can see that we are trying to find the first greatest element again and again for each element in the array. Let’s say we have an array with us for example [1, 5, 12, 4, 9] now we know that 9 is the element which is greater than 1, 5, and 4 but why do we need to find that again and again. We can actually keep a track of the maximum number moving from the end to the start of the array. The approach will help us understand better and also this improvisation is great to come up within an interview.

Approach :

Traverse the array from the end and keep a track of the maximum number to the right of the current index including self

Now we have a monotonous decreasing array, and we know we can use binary search to find the index of the rightmost greater element

Now we will just use binary search for each of the elements in the array and store the maximum difference of the indices and that’s it we are done.Time complexity : O(N*log(N))

Space complexity : O(N)

Method 3 O(nLgn)

Use hashing and sorting to solve this problem in less than quadratic complexity after taking special care of the duplicates.

Approach :

Traverse the array and store the index of each element in a list (to handle duplicates).

Sort the array.

Now traverse the array and keep track of the maximum difference of i and j.

For j consider the last index from the list of possible index of the element and for i consider the first index from the list. (As the index were appended in ascending order).

Keep updating the max difference till the end of the array.Method 4 (Efficient)

To solve this problem, we need to get two optimum indexes of arr[]: left index i and right index j. For an element arr[i], we do not need to consider arr[i] for left index if there is an element smaller than arr[i] on left side of arr[i]. Similarly, if there is a greater element on right side of arr[j] then we do not need to consider this j for right index. So we construct two auxiliary arrays LMin[] and RMax[] such that LMin[i] holds the smallest element on left side of arr[i] including arr[i], and RMax[j] holds the greatest element on right side of arr[j] including arr[j]. After constructing these two auxiliary arrays, we traverse both of these arrays from left to right. While traversing LMin[] and RMa[] if we see that LMin[i] is greater than RMax[j], then we must move ahead in LMin[] (or do i++) because all elements on left of LMin[i] are greater than or equal to LMin[i]. Otherwise we must move ahead in RMax[j] to look for a greater j – i value.

Thanks to celicom for suggesting the algorithm for this method.

Answered by cu18bcs1394
0

Answer:

#include<iostream>

using namespace std;

int main()

{

   int A;

   cin>>A;

   int x,y,temprod,temp;

   for(x=0;x<1000;x++)

   {

     for(y=0;y<1000;y++)

     {

         temprod=x*y;

         if(temprod>A && temp>(x + y))

         {

                 temp=x+y;

         }

     }

   }

   cout<<temp;

}

Step-by-step explanation:

this is one of the simple solution, but it has some error with time complexity.

Similar questions