Update appNew update is available. Click here to update.
About
I am a Btech. Compter Science Engineering Student. I am actively learning the new concepts and new upcoming technologies.
Narula Institute of Technology 2024
My Stats
EXP gained
yellow-spark
12839
Level
7 (Expert)
Community stats
Discussions
0
Upvotes
1
Know more
Weekend contest rating
Contest attended
Problems solved
2021 2023
Better than %
Weekend contest rating
Contest attended
Problems solved
2021 2023
Better than %
213
Total problems solved
124
Easy
84
Moderate
5
Hard
0
Ninja
Jan Jan Feb Feb Mar Mar Apr Apr May May Jun Jun Jul Jul Aug Aug Sep Sep Oct Oct Nov Nov Dec Dec

Current streak:

0 days

Longest streak:

9 days

Less

More

Achievements
8
Ronin
Topics
Stacks & Queues
Sorting
+ 6 more
2
Samurai
Topics
Strings
Arrays
Discussions
Striver Soloution in JAVA
Interview problems

import java.util.*;

 

public class Solution {

    public static Boolean isLeaf(TreeNode root) {

        return (root.left == null) && (root.right == null);

    }

 

    public static void addLeftBoundary(TreeNode root, ArrayList<Integer>res)

    {

        TreeNode cur = root.left;

        while (cur != null) {

            if (isLeaf(cur) == false) res.add(cur.data);

            if (cur.left != null) cur = cur.left;

            else cur = cur.right;

        }

    }

    public static void addRightBoundary(TreeNode root, ArrayList<Integer>res)

    {

        TreeNode cur = root.right;

        ArrayList < Integer > tmp = new ArrayList < Integer > ();

        while (cur != null) {

            if (isLeaf(cur) == false) tmp.add(cur.data);

            if (cur.right != null) cur = cur.right;

            else cur = cur.left;

        }

        int i;

        for (i = tmp.size() - 1; i >= 0; --i) {

            res.add(tmp.get(i));

        }

    }

 

    public static void addLeaves(TreeNode root, ArrayList < Integer > res) {

        if (isLeaf(root)) {

            res.add(root.data);

            return;

        }

        if (root.left != null) addLeaves(root.left, res);

        if (root.right != null) addLeaves(root.right, res);

    }

    public static List<Integer> traverseBoundary(TreeNode root){

        // Write your code here.

        ArrayList<Integer>ans=new ArrayList<>();

        if(isLeaf(root)==false) ans.add(root.data);

        addLeftBoundary(root, ans);

        addLeaves(root, ans);

        addRightBoundary(root, ans);

        return ans;

    }

}

profile
Rajarshi_94ad
Published On 16-Oct-2023
44 views
2 replies
0 upvotes
Simple Recursion
Interview problems

public class Solution

{

    public static int minValue(Node root) {

        // Write your code here.

        if(root==null)

        {

            return -1;

        }

        if(root.left!=null)

        {

            return minValue(root.left);

        }

        else return root.data;

    }

}

profile
Rajarshi_94ad
Published On 01-Oct-2023
134 views
0 replies
1 upvotes
java code using backtracking
Interview problems

import java.util.* ;

import java.io.*; 

public class Solution {

    public static  void rec(int indx,ArrayList<ArrayList<Integer>> ans,ArrayList<Integer> arr)

    {

        if(indx==arr.size())

        {

            ArrayList<Integer> temp=new ArrayList<>();

            for(int i:arr)

            {

                temp.add(i);

            }

            ans.add(new ArrayList<>(temp));

        }

        else

        {

            for(int i=indx;i<arr.size();i++)

            {

                swap(i,indx,arr);

                rec(indx+1, ans, arr);

                swap(i,indx,arr);//backtracking

            }

        }

    }

    public static void swap(int a,int b,ArrayList<Integer> arr)

    {

        Collections.swap(arr, a, b);

    }

    static ArrayList<ArrayList<Integer>> permutations(ArrayList<Integer> arr, int size) {

        // Write your code here.

        ArrayList<ArrayList<Integer>> ans=new ArrayList<>();

        rec(0,ans,arr);

        return ans;

    }

}

profile
Rajarshi_94ad
Published On 09-Sep-2023
36 views
0 replies
0 upvotes
Java Solution
Interview problems

class Pair

{

    int hd;

    BinaryTreeNode node;

    public Pair(int hd,BinaryTreeNode node)

    {

        this.hd=hd;

        this.node=node;

    }

}public class Solution {

    public static ArrayList<Integer> getTopView(BinaryTreeNode root) {

        // Write your code here.

         ArrayList<Integer> list=new ArrayList<>();

        if(root==null) return list;

        Map<Integer,Integer> map=new TreeMap<>();

        Queue<Pair> q=new LinkedList<>();

        q.add(new Pair(0,root));

        while(!q.isEmpty())

        {

            Pair curr=q.poll();

            if(!map.containsKey(curr.hd))

            {

                map.put(curr.hd,curr.node.val);

            }

            if(curr.node.left!=null)

            {

                q.add(new Pair(curr.hd-1,curr.node.left));

            }

            if(curr.node.right!=null)

            {

                q.add(new Pair(curr.hd+1,curr.node.right));

            }

        }

        for(Map.Entry<Integer,Integer> entry:map.entrySet())

        {

            list.add(entry.getValue());

        }

        return list;

    }

}

profile
Rajarshi_94ad
Published On 04-Aug-2023
66 views
0 replies
0 upvotes
using java
Interview problems

import java.util.*;

import java.io.*;

 

public class Solution{

    static ArrayList<Integer> nextSmallerElement(ArrayList<Integer> arr, int n){

        // Write your code here.

        Stack<Integer> q=new Stack<>();

        q.push(-1);

        int curr=0;

        ArrayList<Integer> al=new ArrayList<>();

        for(int i=n-1;i>=0;i--)

        {

            curr=arr.get(i);

            while(curr<=q.peek())

            {

                q.pop();

            }

            al.add(q.peek());

            q.push(curr);

        }

        Collections.reverse(al);

        return al;

    }

}

profile
Rajarshi_94ad
Published On 29-Jul-2023
86 views
0 replies
0 upvotes