nHimanshu's blog

By nHimanshu, history, 13 months ago, In English

The following tasks can be performed on an array of integers: 1.Choose a subarray of arr of size 1 at most x times. 2.Choose a subarray of arr of size 2 at most y times. 3.Choose a subarray of arr of size 3 at most z times.

The chosen subarrays should be non-overlapping. The profit is calculated as the sum of the elements maximum profit that can be obtained?

For example,consider the array [1,2,3,4,5] for x,y,and z exqual 1. for x=1 ,choose any one element from the array. The maximum element is 5,so chose that one.it is the maximum profit under this scenario. For y=1,choose andy subarray of two elements:[1,2],[2,3],3,4] or [4,5].The last subarray has the highest sum (profit)of 9. for z=1,the maximum profit is obatained with the subarray [3,4,5] with a sum of 12.

if you can choose one of each, maximum profit would be obtained by ignoring x then using y and z to capture [1,2] and [3,4,5] or [1,2,3] and [4,5] for a total profit of 15.

Constraints: 1<=n<=200 -10^5<=arr[i]<=10^5 0<=x,y,z<=20

Sample Case 0 Sample input 0 n=2 arr[5,3] x=1 y=0 z=0 output 5

Sampe Case 1: n=4 arr[-7,4,0,-7] x=0 y=1 z=0 output 4

What I have tried:

I am not able to find who to solve this question

Full text and comments »

  • Vote: I like it
  • -14
  • Vote: I do not like it

By nHimanshu, history, 14 months ago, In English

Here is my code in java but these is issue in my code can anybody correct it without change my logic, hey coders!!! please show your debugging skill and solve it:::

import org.w3c.dom.Node;

import  java.util.*;
// BST TREE CLASS
class  TreeNode{
    int data;
    TreeNode left,right;
    TreeNode(int data){
        this.data=data;
        this.left=null;
        this.right=null;
    }
}

//Merge 2 BST
public class Main {
   //insert AT BST method
    private static TreeNode insertATBST(TreeNode root,int x){
        if(root==null){
            root=new TreeNode(x);
            return root;
        }
        if(x>root.data){
            root.right=insertATBST(root.right,x);
        }
        else root.left=insertATBST(root.left,x);
        return root;
    }
   private static TreeNode head=null;
    private  static TreeNode pre=null;
    private static TreeNode bstToDll(TreeNode root){
if(root==null) return null;
//inorder form to use form conversion
bstToDll(root.left);
if(pre==null) head=root;
else{
    root.left=pre;
    pre.right=root;
}
pre=root;
bstToDll(root.right);
return head;
    }
    private static TreeNode mergeBST(TreeNode root1,TreeNode root2){
        TreeNode head=null;
        TreeNode tail=null;
        while(root1!=null && root2!=null){
           if(root1.data<root2.data){
               if(head==null){
                   head=root1;
                   tail=root1;
                   root1=root1.right;
               }
               else{
                   tail.right=root1;
                   root1.left=tail;
                   tail=root1;
                   root1=root1.right;
               }
           }
           else{
               if(head==null){
                   head=root2;
                   tail=root2;
                   root2=root2.right;
               }
               else{
                   tail.right=root2;
                   root2.left=tail;
                   tail=root2;
                   root1=root1.right;
               }
           }
        }
        if(root1!=null){
            tail.right=root1;
            root1.left=tail;
            root1=root1.right;
        }
        if(root2!=null){
            tail.right=root2;
            root2.left=tail;
            root2=root2.right;
        }
        return head;
    }
    private static int countTreeNodes(TreeNode root){
        TreeNode temp=root;
        int cnt=0;
        while(temp!=null){
           cnt++;
           temp=temp.right;
        }
        return  cnt;
    }

     private static TreeNode DllToBst(TreeNode root,int n){
        if(n<=0 || root==null) return null;
        TreeNode left=DllToBst(root,n/2);
        TreeNode newHead=root;
        newHead.left=left;
        root= root.right;
        newHead.right=DllToBst(root,n-n/2-1);
        return  newHead;
    }
    private static void inOrderTraversal(TreeNode root) {
        if (root == null)
            return;
        inOrderTraversal(root.left);
        System.out.println(root.data);
        inOrderTraversal(root.right);
    }
    public static void main(String[] args) {
        int[] arr1={1,2,3};
        int[] arr2={4,5,6};
        TreeNode root1=null; // nullify root object
        TreeNode root2=null;
        for (int j : arr1) {
            root1 = insertATBST(root1, j);
        }
        for (int j : arr2) {
            root2 = insertATBST(root2, j);
        }

        root1=bstToDll(root1);
        head=null; pre=null;
        root2=bstToDll(root2);
        TreeNode root=null;
      root=  mergeBST(root1,root2);
        int cnt=countTreeNodes(root);
//        while(root!=null) {
//            System.out.println(root.data);
//            root=root.right;
//        }
        root=DllToBst(root,cnt);



    }
}

Full text and comments »

  • Vote: I like it
  • -4
  • Vote: I do not like it

By nHimanshu, history, 18 months ago, In English

Problem Description

Given an array A of N length and you are also given number B.

You need to find the number of subarrays in A having sum less than B. We may assume that there is no overflow.

Problem Constraints 1 <= N <= 10000, -100<=A[i]<=100, 0<=B<=10000

Example: INPUT:: A----> [1,−2,4,8], B=0, OUTPUT: 2

Full text and comments »

  • Vote: I like it
  • -4
  • Vote: I do not like it

By nHimanshu, history, 20 months ago, In English

Nobody believes in you, you lost again and again.the lights are cut off but you are still looking at your dream, reviewing it every day and say to yourself: it is not over until I win...... I never leave cp until I can't reach in specialist

Full text and comments »

  • Vote: I like it
  • +48
  • Vote: I do not like it