In this HackerEarth Number of arrays problem solution, You are given an array A. You need to divide this array into exactly K non-empty segments and check whether the minimum element S amongst the maximum elements amongst all segments is less than Q or not.
In other words, if we store the maximum element of each of the segments in an array P, then you have to check if a minimum element in P is less than Q or not.


HackerEarth Number of arrays problem solution


HackerEarth Number of arrays problem solution.

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Minimum_Number solver = new Minimum_Number();
solver.solve(1, in, out);
out.close();
}

static class Minimum_Number {
public void solve(int testNumber, ScanReader in, PrintWriter out) {
int testcases = in.scanInt();
int N, K, Q;
int checker, temp;
while (testcases-- > 0) {
N = in.scanInt();
K = in.scanInt();
Q = in.scanInt();
if (K != 1) checker = Integer.MAX_VALUE;
else checker = Integer.MIN_VALUE;
for (int i = 0; i < N; i++) {
temp = in.scanInt();
if (K != 1) {
if (temp < checker) checker = temp;
} else if (temp > checker) checker = temp;

}
if (checker < Q) out.println(checker);
else out.println("NO");

}
}

}

static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;

public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}

private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0) return -1;
}
return buf[index++];
}

public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}

private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}

}
}


Second solution

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

int Check (int Q, int K, vector<int> arr) {
// write your code here
}

int main() {

ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
for(int t_i=0; t_i<T; t_i++)
{
int N;
cin >> N;
int K;
cin >> K;
int Q;
cin >> Q;
vector<int> arr(N);
for(int i_arr=0; i_arr<N; i_arr++)
{
cin >> arr[i_arr];
assert(arr[i_arr]<=1e9);
}
sort(arr.begin(),arr.end());
if(K==1)
{
if(arr[N-1]<Q)cout<<arr[N-1]<<"\n";
else cout<<"NO\n";continue;
}
if(arr[0]<Q)cout<<arr[0]<<"\n";
else cout<<"NO\n";
}
}