304 North Cardinal St.
Dorchester Center, MA 02124

# Equal Number of Prefix Maximums CodeChef Solution – Queslers

## Problem: Equal Number of Prefix Maximums CodeChef Solution

For an array [B1,B2,…,BK][B1,B2,…,BK] of distinct integers, let’s define its weight as the number of its prefix maximums −− that is, of such indexes ii, that Bi>BjBi>Bj for all 1≤j<i1≤j<i.

You are given a permutation [P1,P2,…,PN][P1,P2,…,PN] of integers from 11 to NN. Determine if it’s possible to split it into two nonempty subsequences with equal weights.

#### Input Format

• The first line will contain TT, the number of test cases. The description of test cases follows.
• The first line of each test case contains a single integer NN −− the length of the permutation.
• The second line of each test case contains NN integers P1,P2,…,PNP1,P2,…,PN −− the permutation.

#### Output Format

For each test case, if it’s possible to split the permutation into two nonempty subsequences with equal weight, output `YES`. Otherwise, output `NO`.

#### Constraints

• 1≤T≤2⋅1051≤T≤2⋅105.
• 2≤N≤2⋅1052≤N≤2⋅105
• The sum of NN over all test cases does not exceed 2⋅1052⋅105.
• 1≤Pi≤N1≤Pi≤N
• Pi≠PjPi≠Pj for i≠ji≠j

#### Sample Input 1

``````5
6
1 2 3 4 5 6
7
1 2 3 4 5 6 7
6
1 4 2 5 3 6
8
5 6 7 8 1 2 3 4
9
5 6 7 8 9 1 2 3 4
``````

#### Sample Output 1

``````YES
NO
YES
YES
NO
``````

#### Explanation

Explanation of test case 1: We can split the permutation into two subsequences [1,3,4][1,3,4] and [2,5,6][2,5,6], both of which have 33 prefix maximums.

Explanation of test case 2: No matter how we split, both sequences will be increasing, therefore the sum of their weights will be 77, so they can’t be equal.

Explanation of test case 3: One way to split is into [1,4,2][1,4,2] and [5,3,6][5,3,6]. 1,41,4 are prefix maximums of the first sequences, and 5,65,6 of the second one.

Explanation of test case 4: One way to split is into [5,6,7,8][5,6,7,8] and [1,2,3,4][1,2,3,4]. Both have 44 prefix maximums.

Explanation of test case 5: It’s possible to show that the required split does not exist.

## Equal Number of Prefix Maximums CodeChef Solution in C++

``````
#include <bits/stdc++.h>

using namespace std;

#define all(v) (v).begin(), (v).end()
#define val const auto&
#define st_mtimespec st_mtim
#define let auto

using ll = long long;
using ull = unsigned long long;
using li = __int128;
using uli = unsigned __int128;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

void doReplace() {
}

template <typename T, typename U, typename...Vs>
void doReplace(T& t, const U& u, Vs&&...vs) {
t = u;
doReplace(vs...);
}

template <typename T, typename...Us>
T minim(T& was, const T& cand, Us&&...us) {
if (was > cand) {
was = cand;
doReplace(us...);
}
return was;
}

template <typename T, typename...Us>
T maxim(T& was, const T& cand, Us&&...us) {
if (was < cand) {
was = cand;
doReplace(us...);
}
return was;
}

template <typename D>
D dPower(D base, ll exponent) {
if (exponent < 0) {
return dPower(1 / base, -exponent);
}
if (exponent == 0) {
return 1;
}
if ((exponent & 1) == 1) {
return dPower(base, exponent - 1) * base;
} else {
D res = dPower(base, exponent >> 1);
return res * res;
}
}

class NumberIterator : iterator<forward_iterator_tag, int> {
public:
int v;

NumberIterator(int v) : v(v) {}

operator int &() { return v; }

int operator*() { return v; }
};

class range : pii {
public:
range(int begin, int end) : pii(begin, max(begin, end)) {}

range(int n) : pii(0, max(0, n)) {}

NumberIterator begin() {
return first;
}

NumberIterator end() {
return second;
}
};

range rangeIncl(int begin, int end) {
return range(begin, end + 1);
}

range rangeIncl(int n) {
return range(n + 1);
}

template <typename T>
class arr {
T* b;
int n;

void allocate(int sz) {
#ifdef LOCAL
if (sz < 0) {
}
#endif
n = sz;
if (sz > 0) {
b = (T*)(::operator new(sz * sizeof(T)));
} else {
b = nullptr;
}
}

public:
arr(int n = 0) {
allocate(n);
#ifdef LOCAL
view();
#endif
}

arr(int n, const T& init) {
allocate(n);
for (int i : range(n)) {
::new((void*)(b + i)) T(init);
}
#ifdef LOCAL
view();
#endif
}

arr(T* b, int n) : arr(b, b + n) {}
arr(T* b, T* e) : b(b), n(e - b) {}

arr(initializer_list<T> l) : arr(l.size()) {
if (n > 0) {
memcpy(b, l.begin(), n * sizeof(T));
}
}

int size() const {
return n;
}

T* begin() {
return b;
}

const T* begin() const {
return b;
}

T* end() {
return b + n;
}

const T* end() const {
return b + n;
}

arr<T> clone() const {
arr<T> res(n);
copy(b, b + n, res.begin());
return res;
}

T& operator[](int at) {
#ifdef LOCAL
if (at < 0 || at >= n) {
throw "Out of bounds";
}
#endif
return b[at];
}

const T& operator[](int at) const {
#ifdef LOCAL
if (at < 0 || at >= n) {
throw "Out of bounds";
}
#endif
return b[at];
}

bool operator ==(const arr& other) const {
if (n != other.n) {
return false;
}
for (int i : range(n)) {
if (b[i] != other.b[i]) {
return false;
}
}
return true;
}

vector<T> view() {
return vector<T>(b, b + n);
}

arr<T> subArray(int from, int to) const {
return arr<T>(b + from, b + to);
}
};

typedef arr<int> arri;

void decreaseByOne() {}

template <typename T, class...Vs>
void decreaseByOne(arr<T>& array, Vs&...vs) {
int n = array.size();
for (int i : range(n)) {
array[i]--;
}
decreaseByOne(vs...);
}

template <typename T>
class arr2d {
T* b;
int d1;
int d2;
int sz;

void allocate(int n) {
#ifdef LOCAL
if (n < 0) {
}
#endif
if (n > 0) {
b = (T*)(::operator new(n * sizeof(T)));
} else {
b = nullptr;
}
}

public:
explicit arr2d(int d1 = 0, int d2 = 0) : d1(d1), d2(d2), sz(d1 * d2) {
#ifdef LOCAL
if (d1 < 0 || d2 < 0) {
}
#endif
allocate(sz);
#ifdef LOCAL
view();
#endif
}

arr2d(int d1, int d2, const T& init) : d1(d1), d2(d2), sz(d1 * d2) {
#ifdef LOCAL
if (d1 < 0 || d2 < 0) {
}
#endif
allocate(sz);
for (int i : range(sz)) {
::new((void*)(b + i)) T(init);
}
#ifdef LOCAL
view();
#endif
}

arr2d(T* b, int d1, int d2) : b(b), d1(d1), d2(d2), sz(d1 * d2) {}

arr2d<T> clone() {
arr2d<T> res(d1, d2);
copy(all(*this), res.b);
return res;
}

int size() const {
return sz;
}

int dim1() const {
return d1;
}

int dim2() const {
return d2;
}

T* begin() {
return b;
}

T* end() {
return b + sz;
}

T& operator()(int i1, int i2) {
#ifdef LOCAL
if (i1 < 0 || i1 >= d1 || i2 < 0 || i2 >= d2) {
throw "Out of bounds";
}
#endif
return b[i1 * d2 + i2];
}

const T& operator()(int i1, int i2) const {
#ifdef LOCAL
if (i1 < 0 || i1 >= d1 || i2 < 0 || i2 >= d2) {
throw "Out of bounds";
}
#endif
return b[i1 * d2 + i2];
}

T& operator[](const pii& p) {
return operator()(p.first, p.second);
}

const T& operator[](const pii& p) const {
return operator()(p.first, p.second);
}

arr<T> operator[](int at) {
#ifdef LOCAL
if (at < 0 || at >= d1) {
throw "Out of bounds";
}
#endif
return arr<T>(b + at * d2, d2);
}

vector<vector<T>> view() {
vector<vector<T>> res(min(d1, 50));
for (int i : range(res.size())) {
res[i] = (*this)[i].view();
}
return res;
}

};

inline bool isWhitespace(int c) {
return isspace(c) || c == EOF;
}

class Input {
private:
bool exhausted = false;
int bufSize = 4096;
char* buf = new char[bufSize];
int bufAt = 0;

public:
inline int get() {
if (exhausted) {
#ifdef LOCAL
throw "Input exhausted";
#endif
return EOF;
}
bufAt = 0;
}
exhausted = true;
return EOF;
}
return buf[bufAt++];
}

inline int peek() {
if (exhausted) {
return EOF;
}
bufAt = 0;
}
exhausted = true;
getchar();
return EOF;
}
return buf[bufAt];
}

private:
template<typename T>
int c = skipWhitespace();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = get();
} else if (c == '+') {
c = get();
}
T res = 0;
do {
if (!isdigit(c)) {
#ifdef LOCAL
throw "Number format error";
#endif
return sgn * res;
}
res *= 10;
res += c - '0';
c = get();
} while (!isWhitespace(c));
return res * sgn;
}

void initArrays(int) {}

template <typename T, class...Vs>
void initArrays(int n, arr<T>& array, Vs&...vs) {
array = arr<T>(n);
initArrays(n, vs...);
}

template <typename T, class...Vs>
void initArrays(int n, vector<T>&, Vs&...vs) {
initArrays(n, vs...);
}

template <typename T, class...Vs>
void readImpl(int i, arr<T>& arr, Vs&...vs) {
}

template <typename T, class...Vs>
void readImpl(int i, vector<T>& arr, Vs&...vs) {
}

public:
inline int skipWhitespace() {
int c;
while (isWhitespace(c = get()) && c != EOF);
return c;
}

inline int peekNext() {
while (isWhitespace(peek()) && peek() != EOF) {
get();
}
return peek();
}

}

}

}

int c = skipWhitespace();
if (c == EOF) {
#ifdef LOCAL
throw "Input exhausted";
#endif
return "";
}
string res;
do {
res.push_back(c);
} while (!isWhitespace(c = get()));
return res;
}

}

}

}

}

}

template<typename T>
throw "Operation not supported";
}

template<typename U, typename V>
return make_pair(first, second);
}

template<typename T>
arr<T> res(n, T());
for (int i : range(n)) {
}
return res;
}

template <class...Vs>
initArrays(n, vs...);
for (int i : range(n)) {
}
}

template<typename U, typename V>
arr<pair<U, V> > readArray(int n) {
arr<pair<U, V> > res(n);
for (int i : range(n)) {
}
return res;
}

template<typename T>
arr2d<T> readTable(int rows, int cols) {
arr2d<T> result(rows, cols);
for (int i : range(rows)) {
for (int j : range(cols)) {
}
}
return result;
}

arr2d<int> readIntTable(int rows, int cols) {
}

arr2d<char> readCharTable(int rows, int cols) {
}

int c = get();
if (c == EOF) {
#ifdef LOCAL
throw "Input exhausted";
#endif
return "";
}
string res;
while (c != '\n' && c != '\r' && c != EOF) {
res.push_back(c);
c = get();
}
if (c == '\r' && peek() == '\n') {
get();
}
return res;
}

int c = skipWhitespace();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = get();
}
double res = 0;
do {
if (tolower(c) == 'e') {
return sgn * res * dPower(double(10), readInt());
}
if (!isdigit(c)) {
#ifdef LOCAL
throw "Number format error";
#endif
return sgn * res;
}
res *= 10;
res += c - '0';
c = get();
} while (!isWhitespace(c) && c != '.');
if (c == '.') {
int length = 0;
c = get();
do {
if (tolower(c) == 'e') {
return sgn * (res + add * dPower(double(10), -length)) * dPower(double(10), readInt());
}
if (!isdigit(c)) {
#ifdef LOCAL
throw "Number format error";
#endif
res += add * dPower(10, -length);
return res * sgn;
}
length++;
c = get();
} while (!isWhitespace(c));
res += add * dPower(double(10), -length);
}
return res * sgn;
}

int c = skipWhitespace();
if (c == EOF) {
#ifdef LOCAL
throw "Input exhausted";
#endif
return 0;
}
return c;
}

inline bool isExhausted() { return exhausted; }

inline void setBufSize(int newBufSize) {
if (newBufSize > bufSize) {
char* newBuf = new char[newBufSize];
memcpy(newBuf, buf, bufSize);
buf = newBuf;
}
bufSize = newBufSize;
}
};

template<>
}

template<>
}

template<>
}

template<>
}

template<>
}

template<>
}

Input in;

class Output {
private:
ostream* out;

template<typename T>
inline void printSingle(const T& value) {
*out << value;
}

template<typename T>
void printSingle(const vector<T>& array) {
size_t n = array.size();
for (int i : range(n)) {
*out << array[i];
if (i + 1 != n) {
*out << ' ';
}
}
}

template<typename T>
void printSingle(const arr<T>& array) {
int n = array.size();
for (int i : range(n)) {
*out << array[i];
if (i + 1 != n) {
*out << ' ';
}
}
}

template<typename T>
void printSingle(const arr2d<T>& array) {
size_t n = array.dim1();
size_t m = array.dim2();
for (int i : range(n)) {
for (int j : range(m)) {
*out << array(i, j);
if (j + 1 != m) {
*out << ' ';
}
}
if (i + 1 != n) {
*out << '\n';
}
}
}

template<typename T, typename U>
inline void printSingle(const pair<T, U>& value) {
*out << value.first << ' ' << value.second;
}

public:
bool autoflush;

Output(ostream& out, bool autoflush) : out(&out), autoflush(autoflush) {
setPrecision(20);
}

void setOut(ostream& nOut) {
out = &nOut;
setPrecision(20);
}

inline void print() {}

template<typename T, typename...Targs>
inline void print(const T& first, const Targs... args) {
printSingle(first);
if (sizeof...(args) != 0) {
*out << ' ';
print(args...);
}
if (autoflush) {
flush();
}
}

template<typename...Targs>
inline void printLine(const Targs... args) {
print(args...);
*out << '\n';
if (autoflush) {
flush();
}
}

inline void flush() {
out->flush();
}

inline void setPrecision(int digs) {
*out << fixed << setprecision(digs);
}
};

Output out(cout, false);
Output err(cerr, true);

#ifdef LOCAL
#define debug(x) err.printLine(#x, '=', x);
#else
#define debug(x)
#endif

template <typename T>
inline void unique(vector<T>& v) {
v.resize(unique(all(v)) - v.begin());
}

arri createOrder(int n) {
arri order(n);
for (int i = 0; i < n; i++) {
order[i] = i;
}
return order;
}

template <typename T>
arri createOrder(const T& v) {
auto order = createOrder(v.size());
sort(all(order), [&](int a, int b) -> bool {
return v[a] < v[b];
});
return order;
}

arri inverse(const arri& p) {
arri res(p.size());
for (int i : range(p.size())) {
res[p[i]] = i;
}
return res;
}

template <typename T, typename Iterator>
inline void addAll(vector<T>& v, Iterator begin, Iterator end) {
v.insert(v.end(), begin, end);
}

template <class Collection, typename Iterator>
inline void addAll(Collection& v, Iterator begin, Iterator end) {
v.insert(begin, end);
}

template <typename Iterator>
arri getQty(Iterator begin, Iterator end, int length) {
arri res(length, 0);
for (Iterator it = begin; it != end; it++) {
res[*it]++;
}
return res;
}

template <typename Iterator>
arri getQty(Iterator begin, Iterator end) {
return getQty(begin, end, *max_element(begin, end) + 1);
}

template <class Collection>
void collect(Collection&) {}

template <class Collection, class Other, class ...Vs>
void collect(Collection& all, Other& a, Vs&...vs) {
collect(all, vs...);
}

void replace(const vi&) {}

template <class ...Vs>
void replace(const vi& all, vi& a, Vs&...vs) {
for (int& i : a) {
i = lower_bound(all(all), i) - all.begin();
}
replace(all, vs...);
}

template <class ...Vs>
void replace(const vi& all, arri& a, Vs&...vs) {
for (int& i : a) {
i = lower_bound(all(all), i) - all.begin();
}
replace(all, vs...);
}

template <class ...Vs>
vi compress(Vs&...vs) {
vi vals;
collect(vals, vs...);
sort(all(vals));
unique(vals);
replace(vals, vs...);
return vals;
}

template <typename T>
class RecursiveFunction {
T t;

public:
RecursiveFunction(T&& t) : t(forward<T>(t)) {}

template <typename... Args>
auto operator() (Args&&... args) const {
return t(*this, forward<Args>(args)...);
}
};

class ReverseNumberIterator : public NumberIterator {
public:
ReverseNumberIterator(int v) : NumberIterator(v) {}

ReverseNumberIterator& operator++() {
--v;
return *this;
}
};

class RevRange : pii {
public:
RevRange(int begin, int end) : pii(begin - 1, min(begin, end) - 1) {}

RevRange(int n) : pii(n - 1, min(n, 0) - 1) {}

ReverseNumberIterator begin() {
return first;
}

ReverseNumberIterator end() {
return second;
}
};

RevRange RevRangeIncl(int begin, int end) {
return RevRange(begin + 1, end);
}

RevRange RevRangeIncl(int n) {
return RevRange(n + 1);
}

class EqualNumberOfPrefixMaximums {
public:
void solve() {

decreaseByOne(p);
let q = inverse(p);
arr<bool> special(n, false);
let so_far = -1;
let num_special = 0;
for (let i : range(n)) {
if (so_far < p[i]) {
so_far = p[i];
special[i] = true;
num_special++;
}
}
if (num_special % 2 == 0) {
out.printLine("YES");
return;
}
arri max_tail(4 * n, 0);
RecursiveFunction get_max_tail = [&](const auto& self, int root, int left, int right, int from, int to) -> int {
if (left >= to || right <= from) {
return 0;
}
if (left >= from && right <= to) {
return max_tail[root];
}
int mid = (left + right) / 2;
auto lRes = self(2 * root + 1, left, mid, from, to);
auto rRes = self(2 * root + 2, mid, right, from, to);
return max(lRes, rRes);
};
RecursiveFunction update_max_tail = [&](const auto& self, int root, int left, int right, int at, int v) -> void {
maxim(max_tail[root], v);
int mid = (left + right) / 2;
if (left + 1 == right) {
return;
}
if (at < mid) {
self(2 * root + 1, left, mid, at, v);
} else {
self(2 * root + 2, mid, right, at, v);
}
};
arri tail(n);
for (let i : RevRange(n)) {
tail[q[i]] = get_max_tail(0, 0, n, q[i], n);
update_max_tail(0, 0, n, q[i], tail[q[i]] + (special[q[i]] ? 2 : 1));
}
so_far = -1;
int qty = 0;
for (let i : range(n)) {
if (special[i]) {
continue;
}
if (so_far < p[i]) {
qty++;
so_far = p[i];
}
}
arri special_to_right(n);
qty = 0;
for (let i : RevRange(n)) {
if (special[i]) {
qty++;
} else {
special_to_right[i] = qty;
}
}
arri special_less(n);
qty = 0;
for (let i : q) {
if (special[i]) {
qty++;
} else {
special_less[i] = qty;
}
}
let to_left = 0;
for (let i : range(n)) {
if (!special[i]) {
if (2 * special_less[i] + tail[i] + 1 >= num_special) {
out.printLine("YES");
return;
}
//            } else {
//                let to_right = num_special - to_left;
//                if (to_left >= to_right && head[i] + to_right >= to_left) {
//                    out.printLine("YES");
//                    return;
//                }
//                to_left++;
}
}
out.printLine("NO");
}
};

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
#ifdef LOCAL_RELEASE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
auto time = clock();
#endif
EqualNumberOfPrefixMaximums solver;

int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
solver.solve();
}

fflush(stdout);
#ifdef LOCAL_RELEASE
cerr << setprecision(3) << double(clock() - time) / CLOCKS_PER_SEC << endl;
#endif
return 0;
}
``````
##### Equal Number of Prefix Maximums CodeChef Solution Review:

In our experience, we suggest you solve this Equal Number of Prefix Maximums CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Equal Number of Prefix Maximums Problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Equal Number of Prefix Maximums CodeChef Solution.

##### Conclusion:

I hope this Equal Number of Prefix Maximums CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More CodeChef Solutions >>

Olympics Ranking CodeChef Solution

Problem Difficulties CodeChef Solution

Chef and Bulb Invention CodeChef Solution

Array Filling CodeChef Solution

Special Triplets CodeChef Solution