we use runlength encode ie RLE encode sequence integer in runlength encode array length encoding 0indexe encodingi tell number time nonnegative integer value encodingi 1 repeat sequencefor example sequence arr 88855 encode encoding 3825 encode 380925 encode 281825 valid RLE arrgiven runlength encode array design iterator iterate itImplement RLEIterator classrleiteratorint encode Initializes object encode array encodedint nextint n Exhausts n element return element exhausted way if element leave exhaust return 1 instead Example 1inputrleiterator next3 8 0 9 2 5 2 1 1 2outputnull 8 8 5 1explanationrleiterator rleiterator new RLEIterator3 8 0 9 2 5 this map sequence 88855rleiteratornext2 exhaust 2 term sequence return 8 the remain sequence 8 5 5rleiteratornext1 exhaust 1 term sequence return 8 the remain sequence 5 5rleiteratornext1 exhaust 1 term sequence return 5 the remain sequence 5rleiteratornext2 exhaust 2 term return 1 this term exhaust 5but second term exist since term exhaust exist return 1 constraints2 encodinglength 1000encodinglength even0 encodingi 1091 n 109At 1000 call