RainX_69

INFOSYS POWER PROGRAMMER | HARD PROBLEM | Binary Srch

Feb 17th, 2023 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.23 KB | Source Code | 0 0
  1. https://leetcode.com/discuss/interview-question/3196203/Infosys-Power-Programmer-or-Online-Assessment-Question
  2.  
  3. QUESTION ASKED IN INFOSYS POWER PROGRAMMER OA
  4.  
  5. In a large factory, there are N walls that the owner painted with different types of paints. Due to the humidity and the difference in the type of paints each wall i will take Xi minutes to dry.
  6.  
  7. However, there is a magic machine that we can set to work for a certain integer number of minutes and put it in front of a certain wall to speed up the drying process.
  8.  
  9. It is given the machine cannot be moved during its work or it will break. Also, for every minutes of work of this machine, it decreases the time remaining to dry the wall in front of it by K minutes. It is also given that if the remaining drying time for a wall is less than K, it will become 0 and dry up without any problems.
  10.  
  11. Your task is to find the minimum number of minutes in which all the walls of the factory can be dried.
  12.  
  13. Note:
  14. We have one machine, but after finishing a wall we can move this machine to another wall.
  15.  
  16. Input format:
  17.  
  18. The first line contains an integer N, denoting the number of elements in X.
  19. The next line contains an integer K, denoting the magic machine's acceleration number.
  20. Each line i of the N subsequent lines (where 0 <= i < N) contains an integer describing X[i].
  21.  
  22. Constraints:
  23. 1 <= N <= 10^5
  24. 1 <= K <= 10^9
  25. 1 <= X[i] <= 10^9
  26.  
  27. Test cases:
  28.  
  29. Input:
  30. 1  1
  31. 1
  32. Output:
  33. 1
  34. Explanation:
  35. We will let it dry without using the magic machine
  36.  
  37. Input:
  38. 1 2
  39. 1
  40. Output:
  41. 1
  42. Explanation:
  43. We will let it dry without using the magic machine
  44.  
  45. Input:
  46. 1 17
  47. 35
  48. Output:
  49. 3
  50. Explanation:
  51. We will use the magic machine three times in a row on the first element.
  52.  
  53.  
  54. arr={5,2,10,7}   ,  k=3
  55. arr        5     2    10    7
  56. -------------------------------
  57. t=0        4     1    7     6    Reduced 10 to 7 by machine
  58. t=1        3     0    4     5    Reduced 7  to 4 by machine
  59. t=2        2     0    3     2    Reduced 5  to 2 by machine
  60. t=3        1     0    0     1    Reduced 3  to 0 by machine
  61. t=4        0     0    0     0    
  62. Time required=5 mins
  63.  
  64.  
  65.  
  66. arr={7,9,10,3,15} , k=5
  67. index-  0   1   2     3    4  
  68. arr-    7   9   10    3   15
  69. -----------------------------
  70. t=0     6   8    9    2   10  // machine on i=4, 15->10
  71. t=1     5   7    8    1   5   // machine on i=4, 10->5
  72. t=2     4   6    3    0   4  //  machine on i=2, 8->3
  73. t=3     3   1    2    0   3  //  machine on i=1, 6->1
  74. t=4     0   0    1    0   2  //  machine on i=0, 3->0
  75. t=5     0   0    0    0   0  //  machine on i=4, 2->0
  76. Time required= 6 mins
  77.  
  78.  
  79. arr={12,11,4,2} , k=3
  80. index-  0     1     2     3      
  81. arr-    12    11    4     2  
  82. -----------------------------
  83. t=0     9     10    3     1  
  84. t=1     8     7     2     0  
  85. t=2     5     6     1     0  
  86. t=3     4     3     0     0  
  87. t=4     1     2     0     0  
  88. t=5     0     0     0     0  
  89. Time required= 6 mins
  90.  
  91. -------------------------------------------------------------------------------------------------------------------------------------
  92.    
  93. PRIORITY QUEUE CODE->
  94.    
  95. #include <bits/stdc++.h>
  96. using namespace std;
  97.  
  98. vector<int> arr;
  99. int k;
  100.  
  101. void solve(vector<int> &arr, int k){
  102.    priority_queue<pair<int,int>> pq;
  103.    for(int i=0;i<arr.size();i++){
  104.        pq.push({arr[i],0});
  105.    }
  106.  
  107.    int runningTime=0;
  108.    while(!pq.empty()){
  109.        auto tp=pq.top();
  110.        pq.pop();
  111.        int currWallTime=tp.first-(runningTime-tp.second);
  112.        if(tp.second!=runningTime){
  113.            if(currWallTime>0){
  114.                pq.push({currWallTime,runningTime});
  115.            }  
  116.            continue;
  117.        }
  118.        if(currWallTime>0){
  119.            currWallTime-=k;
  120.            runningTime++;
  121.            if(currWallTime>0){
  122.                pq.push({currWallTime,runningTime});
  123.            }
  124.        }
  125.    }
  126.    cout<<runningTime;
  127. }
  128.  
  129. int main() {
  130.    
  131.    arr={35};
  132.    k=17;
  133.    solve(arr,k); //ANSWER ->3
  134.    
  135.    
  136.    arr={6,5,4};
  137.    k=2;
  138.    solve(arr,k); //ANSWER ->4
  139.      
  140.      
  141.    arr={1,8,10};
  142.    k=1;
  143.    solve(arr,k); //ANSWER ->10
  144.    
  145.    
  146.    arr={5,2,10,7};
  147.    k=3;
  148.    solve(arr,k); //ANSWER ->5
  149.    
  150.    
  151.    arr={12,11,4,2};
  152.    k=3;
  153.    solve(arr,k); //ANSWER ->6
  154.    
  155.    
  156.    arr={10,10,10};
  157.    k=3;
  158.    solve(arr,k); //ANSWER ->6
  159.    
  160.    
  161.    arr={7,9,10,3,15};
  162.    k=5;
  163.    solve(arr,k); //ANSWER ->6
  164.    
  165.    
  166.    arr={17};
  167.    k=16;
  168.    solve(arr,k); //ANSWER ->2
  169.    
  170.    
  171.    arr={5,2,10,7,35};
  172.    k=3;
  173.    solve(arr,k); //ANSWER ->12
  174.    
  175.    
  176.    return 0;
  177. }
  178.  
  179. The above code is correct, BUT, it will fail for larger test cases. The time complexity O(ZlogN), where Z=10^9. So, this approach guarantee would not pass the larger test cases.  
  180. ---------------------------------------------------------------------------------------------------------------------------------------
  181.  
  182.  
  183. Binary Search Code->
  184.  
  185. This code below was written by someone else. But this will pass all the TC's even the larger ones. The code below uses the idea that min time to dryWalls is actually the time you used the machine on each wall. Minimizing the usage of machine on each wall to get the optimal drying time.
  186.  
  187. #include<bits/stdc++.h>
  188. using namespace std;
  189.  
  190. bool isOK(vector<int> &X, int k, int time){
  191.     /*
  192.         If y is the minimum time machine is used on ith wall.
  193.         Then k*y is the value, machine reduced and (time-y) is
  194.         the remaining value getting reduced by 1 as time passes.
  195.        
  196.         X[i]<=k*y+(time-y)
  197.         X[i]<=k(y-1)+time
  198.         X[i]-time<=y(k-1)
  199.         (X[i]-time)/(k-1)<=y
  200.        
  201.         So, min time a machine is used on wall X[i] is y=(X[i]-time)/(k-1).
  202.     */
  203.     int calculatedTime=0;
  204.     for(auto x: X){
  205.         if(x-time>0){
  206.             calculatedTime+=ceil((double)(x-time)/(double)(k-1));
  207.         }
  208.     }
  209.     return calculatedTime<=time;
  210. }
  211.  
  212. void solve(vector<int> &arr, int k){
  213.     if(k==1){
  214.         cout<<*max_element(arr.begin(),arr.end())<<endl;
  215.         return;
  216.     }
  217.    /* Binary searching for minTime. The mintime to dry wall can be obtained by minimising the time spent by the machine on drying walls */
  218.     int low=0;
  219.     int high=*max_element(arr.begin(),arr.end());
  220.     int res=-1;
  221.     while(low<=high){
  222.         int mid=(low+high)/2;
  223.         if(isOK(arr,k,mid)==true){
  224.             res=mid;
  225.             high=mid-1;
  226.         }
  227.         else{
  228.             low=mid+1;
  229.         }
  230.     }
  231.     cout<<res<<endl;
  232. }
  233.  
  234.  
  235.  
  236. int main() {
  237.     vector<int> arr;
  238.     int k;
  239.    
  240.     arr={35};
  241.     k=17;
  242.     solve(arr,k); //ANSWER ->3
  243.    
  244.    
  245.     arr={6,5,4};
  246.     k=2;
  247.     solve(arr,k); //ANSWER ->4
  248.        
  249.        
  250.     arr={1,8,10};
  251.     k=1;
  252.     solve(arr,k); //ANSWER ->10
  253.    
  254.    
  255.     arr={5,2,10,7};
  256.     k=3;
  257.     solve(arr,k); //ANSWER ->5
  258.    
  259.    
  260.     arr={12,11,4,2};
  261.     k=3;
  262.     solve(arr,k); //ANSWER ->6
  263.    
  264.    
  265.     arr={10,10,10};
  266.     k=3;
  267.     solve(arr,k); //ANSWER ->6
  268.    
  269.    
  270.     arr={7,9,10,3,15};
  271.     k=5;
  272.     solve(arr,k); //ANSWER ->6
  273.    
  274.    
  275.     arr={17};
  276.     k=16;
  277.     solve(arr,k); //ANSWER ->2
  278.    
  279.    
  280.     arr={5,2,10,7,35};
  281.     k=3;
  282.     solve(arr,k); //ANSWER ->12
  283.    
  284.    
  285.     arr={1000,500,100,10};
  286.     k=100;
  287.     solve(arr,k); //ANSWER ->16
  288.    
  289.    
  290.     return 0;
  291. }
Advertisement
Add Comment
Please, Sign In to add comment