GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Saturday 25 June 2022

[Solution] NIT Destroys the Universe Codeforces Solution


B. NIT Destroys the Universe
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

For a collection of integers S, define mex(S) as the smallest non-negative integer that does not appear in S.

NIT, the cleaver, decides to destroy the universe. He is not so powerful as Thanos, so he can only destroy the universe by snapping his fingers several times.

The universe can be represented as a 1-indexed array a of length n. When NIT snaps his fingers, he does the following operation on the array:

  • He selects positive integers l and r such that 1lrn. Let w=mex({al,al+1,,ar}). Then, for all lir, set ai to w.

We say the universe is destroyed if and only if for all 1inai=0 holds.

Solution Click Below:-  👉CLICK HERE👈
👇👇👇👇👇


Find the minimum number of times NIT needs to snap his fingers to destroy the universe. That is, find the minimum number of operations NIT needs to perform to make all elements in the array equal to 0.

Input


Each test contains multiple test cases. The first line contains the number of test cases t (1t104). Description of the test cases follows.

The first line of each test case contains one integer n (1n105).

The second line of each test case contains n integers a1a2an (0ai109).

It is guaranteed that the sum of n over all test cases does not exceed 2105.

Output

For each test case, print one integer — the answer to the problem.

No comments:

Post a Comment