class Result {
/*
* Complete the 'getMaxValue' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
public static int getMaxValue(List<Integer> arr) {
// Write your code here
Collections.sort(arr);
if(arr.get(0)!=1){
arr.set(0,1);
}
for(int i=1;i<arr.size();i++){
if(arr.get(i)-arr.get(i-1)>1){
arr.set(i,arr.get(i-1)+1);
}
}
return arr.get(arr.size()-1);
}
}
SELECT CUSTOMER_ID
FROM (
SELECT CUSTOMER_ID, COUNT(ID) AS order_count
FROM ORDERS
GROUP BY CUSTOMER_ID
ORDER BY order_count DESC, CUSTOMER_ID ASC
) AS temp LIMIT 1;
class Result {
/*
* Complete the 'getProductsInRange' function below.
*
* URL for cut and paste
* https://jsonmock.hackerrank.com/api/inventory?category=<category>&page=<pageNumber>
*
* The function is expected to return an Integer value.
* The function accepts String category, Integer minPrice and Integer maxPrice as arguments.
*
*/
public static int getProductsInRange(String category, int minPrice, int maxPrice)
{
int totalItemsInRange = 0;
int currentPage = 1;
int totalPages = Integer.MAX_VALUE;
while (currentPage <= totalPages) {
String url = "https://jsonmock.hackerrank.com/api/inventory?category=" + category + "&page=" + currentPage;
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
JSONParser parser = new JSONParser();
JSONObject jsonResponse = (JSONObject) parser.parse(response.toString());
JSONArray data = (JSONArray) jsonResponse.get("data");
totalPages = Integer.parseInt(jsonResponse.get("total_pages").toString());
for (Object obj : data) {
JSONObject item = (JSONObject) obj;
double price = Double.parseDouble(item.get("price").toString());
if (price >= minPrice && price <= maxPrice) {
totalItemsInRange++;
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
currentPage++;
}
return totalItemsInRange;
}
}