Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let input = [
- '7 4',
- '1 5 4 7',
- ]
- let print = this.print || console.log;
- let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- const arr1 = gets().split(' ').map(Number);
- const arr2 = gets().split(' ').map(Number);
- const n = arr1[0];
- const k = arr1[1];
- class List {
- constructor() {
- this.items = []
- }
- add(item) {
- this.items.push(item);
- }
- get(index) {
- return this.items[index];
- }
- set(index, item) {
- return this.items[index] = item;
- }
- insert(index, item) {
- this.items.splice(index, 0, item);
- }
- remove(index) {
- this.items.splice(index, 1);
- }
- size() {
- return this.items.length;
- }
- values() {
- return [...this.items];
- }
- index(item) {
- return this.items.indexOf(item)
- }
- lastIndex(item) {
- return this.items.lastIndexOf(item)
- }
- }
- let res = new List();
- for (let i = 1; i <= n; i++) {
- res.add(i);
- }
- for (let i = 0; i < k; i++) {
- let currNum = arr2[i];
- let finalNum = 0;
- let ind = 0;
- if (currNum % 2 === 0) {
- finalNum = currNum / 2;
- ind = res.index(finalNum);
- res.insert(ind+1, currNum);
- res.remove(res.lastIndex(currNum))
- } else if (currNum % 2 === 1) {
- if (currNum * 2 > n) {
- if (currNum !== n) {
- ind = res.index(n);
- res.remove(res.index(currNum));
- res.insert(ind, currNum);
- }
- } else if (currNum * 2 < n) {
- finalNum = currNum * 2;
- ind = res.index(finalNum);
- res.remove(res.index(currNum));
- res.insert(ind, currNum);
- }
- }
- }
- print(res.items.join(' '));
Advertisement
Add Comment
Please, Sign In to add comment