Sort by

recency

|

98 Discussions

|

  • + 0 comments
    function processData(input) {
    input = input.split(/\r?\n/)
    let array = input[1].split(' ').map((val)=>{
        return Number(val)
    })
    input.shift()
    input.shift()
    // let quariSize = Number(input[0].split(' ')[1])
    //     for(let i = 0 ; i<quariSize ; i++){
            
    //     }
    let newarr=[]
     input= input.map((val)=>{
        val = val.split(" ")
        let d = val.map(value => Number(value))
        newarr.push(d)
    })
    //now newarr is our quaries
    for(let i = 0;i<newarr.length;i++){
        let choice = newarr[i][0]
        let lb = newarr[i][1]-1
        let ub = newarr[i][2]
        let temparr = array.splice(lb,ub-lb)
        if(choice == 2){
            array = array.concat(temparr)
        }
        else{
           array = temparr.concat(array)
        }
    }
    console.log(Math.abs(array[array.length-1]-array[0]))
    let str = ''
    for(let i of array){
        str+= i+' '
    }
    console.log(str)
    }
    

    7 test case doesnt pass due to time limit exceeded

  • + 0 comments

    can anybody help in optimizing the below code in javascript? Some test cases are getting timed out.

    input = input.split('\n'); let queriesArr = input.splice(2,input.length - 2); let inputArr = input[1].split(' '); let tempArr = []; queriesArr.forEach((element)=>{ let queryType = element.split(' ')[0]; let i = parseInt(element.split(' ')[1]); let j = parseInt(element.split(' ')[2]); tempArr = inputArr.splice(i-1,j-i+1); if(queryType === '1') { inputArr = [...tempArr,...inputArr]; } if(queryType === '2') { inputArr = [...inputArr,...tempArr]; } })
    let absoluteValue = Math.abs(inputArr[0] - inputArr[inputArr.length - 1]); let allElements = inputArr.join(" "); console.log(absoluteValue + "\n" + allElements);

  • + 0 comments

    c++ style:

    void reorder(vector<int> &inp,int l,int r, int command) {
        vector<int> tmp(inp.begin()+l,inp.begin()+r);
        inp.erase(inp.begin()+l,inp.begin()+r);
        if(command==1){
            inp.insert(inp.begin(),tmp.begin(),tmp.end());
        }else if(command==2) {
            inp.insert(inp.end(),tmp.begin(),tmp.end());
        }
    }
    
    int main() {
        
        int N,M;
        // get N , M
        std::cin>>N>>M;
      vector<int> input(N);
      for(int i=0; i<N; ++i) {
          cin >> input[i];
      }
       
       while(M--){
           int r,l,c;
           cin>>c>>l>>r;
           reorder(input, l-1, r, c);
       }
        cout<<abs(input[input.size()-1]-input[0])<<endl;
        for(const auto &i: input) cout<<i<<" ";
        
       
        return 0;
    }
    
  • + 2 comments

    I have a question First I wrote this code which times out

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    n,queries=map(int,input().strip().split())
    arr=list(map(int,input().strip().split()))
    #print(arr)
    for _ in range(queries):
        q,x,y=map(int,input().strip().split())
        if q==1:
            arr=arr[x-1:y]+arr[:x-1]+arr[y:]
        else:
            arr=arr[:x-1]+arr[y:]+arr[x-1:y]
    
    print(abs(arr[-1]-arr[0]))
    print(*arr)
    
  • + 0 comments

    c solution...

    include

    include

    include

    include

    include

    // Treap without rotating operations struct Node { struct Node *left, *right; int data; int pri; // priority of this Treap Node int size; // Order Statistic Tree, used for relocating elements }; typedef struct Node Node;

    inline Node* new_Node(int v) { Node* node = (Node*) malloc(sizeof(Node)); node->left = node->right = NULL; node->data = v; node->pri = rand(); node->size = 1; return node; }

    inline int Node_get_size(Node* node) { if (node) return node->size; return 0; }

    // Merge lt & rt, lt must be on the left-side of rt Node* merge(Node* lt, Node* rt) { if (lt == NULL && rt == NULL) return NULL;

    if (lt == NULL)
        return rt;
    if (rt == NULL)
        return lt;
    
    // lt is on the top of rt
    if (lt->pri < rt->pri) {
        lt->right = merge(lt->right, rt);
        lt->size = Node_get_size(lt->left) + Node_get_size(lt->right) + 1;
        return lt;
    }
    // rt is on the top of lt
    else {
        rt->left = merge(lt, rt->left);
        rt->size = Node_get_size(rt->left) + Node_get_size(rt->right) + 1;
        return rt;
    }
    

    }

    // split k nodes to the returned tree from *ref_tree // *ref_tree will be modified during splitting // as a result, splitted_tree will have k nodes // and ref_tree will have (n-k) nodes Node split(Node** ref_tree, int k) { Node* tree = *ref_tree;

    if (tree == NULL)
        return NULL;
    
    Node* splited_tree;
    
    if (Node_get_size(tree->left) >= k) {
        splited_tree = split(&tree->left, k);
    }
    else {
        k -= (Node_get_size(tree->left) + 1);
    
        splited_tree = tree;
        *ref_tree = splited_tree->right;
    
        splited_tree->right = split(ref_tree, k);
    
        tree = *ref_tree;
    }
    
    if (splited_tree)
        splited_tree->size = Node_get_size(splited_tree->left) + Node_get_size(splited_tree->right) + 1;
    if (tree)
        tree->size = Node_get_size(tree->left) + Node_get_size(tree->right) + 1;
    
    return splited_tree;
    

    }

    // print the inorder traversal of the Treap void print_inorder(Node* root) { if (!root) return; print_inorder(root->left); printf("%d ", root->data); print_inorder(root->right); }

    int main() { srand(clock());

    int N, M;
    
    while (scanf("%d %d", &N, &M) != EOF) {
    
        Node* root = NULL;
        for (int i = 0; i < N; ++i) {
            int v;
            scanf("%d", &v);
            Node* node = new_Node(v);
            root = merge(root, node);
        }
    
        for (int i = 0; i < M; ++i) {
            int type, l, r;
            scanf("%d %d %d", &type, &l, &r);
    
            Node* lt = split(&root, l-1);
            Node* mt = split(&root, r-l+1);
            Node* rt = root;
    
            if (type == 1) {
                root = merge(merge(mt, lt), rt);
            }
            else{
                root = merge(lt, merge(rt, mt));
            }
        }
    
        Node* lt = split(&root, 1);
        Node* mt = split(&root, Node_get_size(root)-1);
        Node* rt = root;
    
        #define ABS(x) ((x) > 0 ? (x) : -(x))
        printf("%d\n", ABS(lt->data - rt->data));
        #undef ABS
    
        root = merge(merge(lt, mt), rt);
    
        print_inorder(root);
    }
    
    return 0;
    

    }