[Solved] Orchids and Azaleas

Unless one is wealthy there is no use in being a charming fellow. Romance is the privilege of the rich, not the profession of the unemployed. These are the great truths of modern life which Hughie Erskine never realised. Poor Hughie! Intellectually, we must admit, he was not of much importance. He never said a brilliant or even an ill-natured thing in his life. But then he was wonderfully good-looking, with his crisp brown hair, his clear-cut profile, and his grey eyes. He was as popular with men as he was with women and he had every accomplishment, except that of making money. He tried everything. He had been a tea merchant for a little longer, but had soon tired of pekoe and souchong. Then he had tried selling dry sherry. That did not answer: the sherry was a little too dry. Now, he started a flowristry. He grows only Orchids and Azaleas. He wants to display these flowers in a straight line in his shop. He decided to arrange the flowers in order to minimize the SKFA. The SKFA (same kind of flower arragement) is the maximum number of flowers of the same kind (all Orchids or all Azaleas) that appear consecutively. Given O (Number of Orchids) and A (Number of Azaleas), He needs your help to calculate the minimum SKFA among all possible arrangements.

Input Format:
First line contains a single interger ‘T’ representing number of test cases.
Next T lines contains, two space seperated intergers O and A.

Output Format:
Output a single integer representing the minimum SKFA.

Example:

Input:

4
5 0
5 5
5 1
5 2

Output:

5
1
3
2

Explanation:
For test case 1,
There are 5 Orchids and 0 Azaleas, so we need to arrange all of Orchids in one by one
For test case 2,
There are 5 Orchids and 5 Azaleas, so we can arrange one Orchid and one Azalea and continue
O A O A O A O A O A
For test case 3,
There are 5 Orchids and 1 Azalea. we can arrange it as follows,
O O A O O O, so the maximum number of consequetive flowers of same kind are 3.
For test case 4,
There are 5 Orchids and 2 Azaleas, we can arrange it as follows,
O O A O A O O, so the maximum number of consequestive flowers of same kind are 2.

Solution

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
  int t; 
  cin >> t; 
  while (t--) { 
    int n, m; 
    cin >> n >> m; 
    int ans = max(n, m) + 1; 
    for (int i = 1; i <= min(n, m) + 1; i++) { 
      ans = min(res, max((n + i - 1) / i, (m + i - 1) / i)); 
      // sauravhathi
    } 
    cout << ans << endl; 
  } 
  return 0; 
}
import java.util.*;
import java.lang.*;
import java.io.*;

class Test {
  public static void main(String args[] ) throws Exception {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while (t-- > 0) {
      int n = sc.nextInt();
      int m = sc.nextInt();
      int res = Math.max(n, m) + 1;
      for (int i = 1; i <= Math.min(n, m) + 1; i++) {
        res = Math.min(res, Math.max((n + i - 1) / i, (m + i - 1) / i));

// sauravhathi
      }
      System.out.println(res);
    }
  }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *