Saturday, 26 August 2023

PostOrder Traversal in BinaryTree using 2 Stacks(Code in JAVA)


import java.util.*;


public class test {

    static class Node {

        int data;

        Node left;

        Node right;


        public Node(int data) {

            this.data = data;

            this.left = null;

            this.right = null;

        }

    }


    static class BinaryTree {

        static Node buildTree(Scanner scanner, String position, int parentValue) {

            System.out.print("Add " + position + " node for " + parentValue + " (y/n): ");

            String userChoice = scanner.next();


            if (userChoice.equalsIgnoreCase("y")) {

                System.out.print("Enter " + position + " node value: ");

                int nodeValue = scanner.nextInt();

                Node newNode = new Node(nodeValue);


                newNode.left = buildTree(scanner, "left", nodeValue);

                newNode.right = buildTree(scanner, "right", nodeValue);


                return newNode;

            } else {

                return null;

            }

        }

    }

    //levelorder traversal

    public static void levelorder(Node root)

    {

        if(root==null) return;

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

        q.add(root);

        q.add(null);

        while(!q.isEmpty())

        {

            Node currnode = q.remove();

            if(currnode==null) //NULL DENOTES NEW LINE

            {

                System.out.println();

                if(q.isEmpty())

                {

                    break;

                }

                else

                {

                    q.add(null);

                }

            }

            else

            {

                System.out.print(currnode.data+" ");

                if(currnode.left!=null)

                {

                    q.add(currnode.left);

                }

                if(currnode.right!=null)

                {

                    q.add(currnode.right);

                }

            }

        }

    }


    public static ArrayList<Integer> preorderUsingStack(Node root)

    {

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

        if (root==null) return preorderArrayList;

        Stack<Node> st = new Stack<Node>();

        st.push(root);

        while(!st.isEmpty())

        {

            root = st.pop();

            preorderArrayList.add(root.data);

            if(root.right != null)

            {

                st.push(root.right);

            }

            if(root.left != null)

            {

                st.push(root.left);

            }


        }

        return preorderArrayList;

    }


    public static ArrayList<Integer> inorderUsingStack(Node root)

    {

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

        if (root == null) return null;

        Stack<Node> st = new Stack<Node>();

        Node node = root;

        while(true)

        {

            if(node != null)

            {

                st.push(node);

                node = node.left;

            }

            else

            {

                if(st.isEmpty())

                {

                    break;

                }

                node = st.pop();

                inorderArrayList.add(node.data);

                node = node.right;

            }

        }

        return inorderArrayList;

    }


    public static ArrayList<Integer> postorderUsing2Stack(Node root)

    {

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

        if(root == null){return postorderArrayList;}

        Stack<Node> st1 = new Stack<Node>();

        Stack<Node> st2 = new Stack<Node>();

        st1.push(root);

        while(!st1.isEmpty())

        {

            root = st1.pop();

            st2.push(root);

            if(root.left!=null) st1.push(root.left);

            if(root.right!=null) st1.push(root.right);

        }

        while(!st2.isEmpty())

        {

            postorderArrayList.add(st2.pop().data);

        }

        return postorderArrayList;

    }


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        BinaryTree bt = new BinaryTree();

        

        System.out.print("Enter root node value: ");

        int rootValue = scanner.nextInt();

        Node root = new Node(rootValue);


        root.left = bt.buildTree(scanner, "left", rootValue);

        root.right = bt.buildTree(scanner, "right", rootValue);


        System.out.println("Tree construction complete.");

        levelorder(root);

        System.out.println(preorderUsingStack(root));

        System.out.println(inorderUsingStack(root));

        System.out.println(postorderUsing2Stack(root));

        scanner.close();

    }

}



  

InOrder Traversal in BinaryTree using iterative method and Stack(Code in Java)


import java.util.*;


public class test {

    static class Node {

        int data;

        Node left;

        Node right;


        public Node(int data) {

            this.data = data;

            this.left = null;

            this.right = null;

        }

    }


    static class BinaryTree {

        static Node buildTree(Scanner scanner, String position, int parentValue) {

            System.out.print("Add " + position + " node for " + parentValue + " (y/n): ");

            String userChoice = scanner.next();


            if (userChoice.equalsIgnoreCase("y")) {

                System.out.print("Enter " + position + " node value: ");

                int nodeValue = scanner.nextInt();

                Node newNode = new Node(nodeValue);


                newNode.left = buildTree(scanner, "left", nodeValue);

                newNode.right = buildTree(scanner, "right", nodeValue);


                return newNode;

            } else {

                return null;

            }

        }

    }

    //levelorder traversal

    public static void levelorder(Node root)

    {

        if(root==null) return;

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

        q.add(root);

        q.add(null);

        while(!q.isEmpty())

        {

            Node currnode = q.remove();

            if(currnode==null) //NULL DENOTES NEW LINE

            {

                System.out.println();

                if(q.isEmpty())

                {

                    break;

                }

                else

                {

                    q.add(null);

                }

            }

            else

            {

                System.out.print(currnode.data+" ");

                if(currnode.left!=null)

                {

                    q.add(currnode.left);

                }

                if(currnode.right!=null)

                {

                    q.add(currnode.right);

                }

            }

        }

    }


    public static ArrayList<Integer> preorderUsingStack(Node root)

    {

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

        if (root==null) return preorderArrayList;

        Stack<Node> st = new Stack<Node>();

        st.push(root);

        while(!st.isEmpty())

        {

            root = st.pop();

            preorderArrayList.add(root.data);

            if(root.right != null)

            {

                st.push(root.right);

            }

            if(root.left != null)

            {

                st.push(root.left);

            }


        }

        return preorderArrayList;

    }


    public static ArrayList<Integer> inorderUsingStack(Node root)

    {

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

        if (root == null) return null;

        Stack<Node> st = new Stack<Node>();

        Node node = root;

        while(true)

        {

            if(node != null)

            {

                st.push(node);

                node = node.left;

            }

            else

            {

                if(st.isEmpty())

                {

                    break;

                }

                node = st.pop();

                inorderArrayList.add(node.data);

                node = node.right;

            }

        }

        return inorderArrayList;

    }


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        BinaryTree bt = new BinaryTree();

        

        System.out.print("Enter root node value: ");

        int rootValue = scanner.nextInt();

        Node root = new Node(rootValue);


        root.left = bt.buildTree(scanner, "left", rootValue);

        root.right = bt.buildTree(scanner, "right", rootValue);


        System.out.println("Tree construction complete.");

        levelorder(root);

        System.out.println(preorderUsingStack(root));

        System.out.println(inorderUsingStack(root));

        scanner.close();

    }

}



  



PreOrder Traversal using iterative approarch(Code in JAVA)


import java.util.*;


public class test {

    static class Node {

        int data;

        Node left;

        Node right;


        public Node(int data) {

            this.data = data;

            this.left = null;

            this.right = null;

        }

    }


    static class BinaryTree {

        static Node buildTree(Scanner scanner, String position, int parentValue) {

            System.out.print("Add " + position + " node for " + parentValue + " (y/n): ");

            String userChoice = scanner.next();


            if (userChoice.equalsIgnoreCase("y")) {

                System.out.print("Enter " + position + " node value: ");

                int nodeValue = scanner.nextInt();

                Node newNode = new Node(nodeValue);


                newNode.left = buildTree(scanner, "left", nodeValue);

                newNode.right = buildTree(scanner, "right", nodeValue);


                return newNode;

            } else {

                return null;

            }

        }

    }

    //levelorder traversal

    public static void levelorder(Node root)

    {

        if(root==null) return;

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

        q.add(root);

        q.add(null);

        while(!q.isEmpty())

        {

            Node currnode = q.remove();

            if(currnode==null) //NULL DENOTES NEW LINE

            {

                System.out.println();

                if(q.isEmpty())

                {

                    break;

                }

                else

                {

                    q.add(null);

                }

            }

            else

            {

                System.out.print(currnode.data+" ");

                if(currnode.left!=null)

                {

                    q.add(currnode.left);

                }

                if(currnode.right!=null)

                {

                    q.add(currnode.right);

                }

            }

        }

    }


    public static ArrayList<Integer> preorderUsingStack(Node root)

    {

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

        if (root==null) return preorderArrayList;

        Stack<Node> st = new Stack<Node>();

        st.push(root);

        while(!st.isEmpty())

        {

            root = st.pop();

            preorderArrayList.add(root.data);

            if(root.right != null)

            {

                st.push(root.right);

            }

            if(root.left != null)

            {

                st.push(root.left);

            }


        }

        return preorderArrayList;

    }


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        BinaryTree bt = new BinaryTree();

        

        System.out.print("Enter root node value: ");

        int rootValue = scanner.nextInt();

        Node root = new Node(rootValue);


        root.left = bt.buildTree(scanner, "left", rootValue);

        root.right = bt.buildTree(scanner, "right", rootValue);


        System.out.println("Tree construction complete.");

        levelorder(root);

        System.out.println(preorderUsingStack(root));

        scanner.close();

    }

}



  

Friday, 25 August 2023

LevelOrder Traversal in Binary Tree (code in JAVA)



import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;


public class test {

    static class Node {

        int data;

        Node left;

        Node right;


        public Node(int data) {

            this.data = data;

            this.left = null;

            this.right = null;

        }

    }


    static class BinaryTree {

        static Node buildTree(Scanner scanner, String position, int parentValue) {

            System.out.print("Add " + position + " node for " + parentValue + " (y/n): ");

            String userChoice = scanner.next();


            if (userChoice.equalsIgnoreCase("y")) {

                System.out.print("Enter " + position + " node value: ");

                int nodeValue = scanner.nextInt();

                Node newNode = new Node(nodeValue);


                newNode.left = buildTree(scanner, "left", nodeValue);

                newNode.right = buildTree(scanner, "right", nodeValue);


                return newNode;

            } else {

                return null;

            }

        }

    }

    //levelorder traversal

    public static void levelorder(Node root)

    {

        if(root==null) return;

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

        q.add(root);

        q.add(null);

        while(!q.isEmpty())

        {

            Node currnode = q.remove();

            if(currnode==null)

            {

                System.out.println();

                if(q.isEmpty())

                {

                    break;

                }

                else

                {

                    q.add(null);

                }

            }

            else

            {

                System.out.print(currnode.data+" ");

                if(currnode.left!=null)

                {

                    q.add(currnode.left);

                }

                if(currnode.right!=null)

                {

                    q.add(currnode.right);

                }

            }

        }

    }


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        BinaryTree bt = new BinaryTree();

        

        System.out.print("Enter root node value: ");

        int rootValue = scanner.nextInt();

        Node root = new Node(rootValue);


        root.left = bt.buildTree(scanner, "left", rootValue);

        root.right = bt.buildTree(scanner, "right", rootValue);


        System.out.println("Tree construction complete.");

        levelorder(root);

        scanner.close();

    }

}



  

 BinaryTree Data Structure implementation in JAVA: ( User Input Method)


import java.util.Scanner;

public class test {
    static class Node {
        int data;
        Node left;
        Node right;

        public Node(int data) {
            this.data = data;
            this.left = null;
            this.right = null;
        }
    }

    static class BinaryTree {
        static Node buildTree(Scanner scanner, String position, int parentValue) {
            System.out.print("Add " + position + " node for " + parentValue + " (y/n): ");
            String userChoice = scanner.next();

            if (userChoice.equalsIgnoreCase("y")) {
                System.out.print("Enter " + position + " node value: ");
                int nodeValue = scanner.nextInt();
                Node newNode = new Node(nodeValue);

                newNode.left = buildTree(scanner, "left", nodeValue);
                newNode.right = buildTree(scanner, "right", nodeValue);

                return newNode;
            } else {
                return null;
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        BinaryTree bt = new BinaryTree();
        
        System.out.print("Enter root node value: ");
        int rootValue = scanner.nextInt();
        Node root = new Node(rootValue);

        root.left = bt.buildTree(scanner, "left", rootValue);
        root.right = bt.buildTree(scanner, "right", rootValue);

        System.out.println("Tree construction complete.");
        scanner.close();
    }
}


  

 BinaryTree Data Structure implementation in JAVA: (Hard Coding Method)


public class test {

    static class Node {   

        int data;

        Node left;

        Node right;


        public Node(int data) {

            this.data = data;

            this.left = null;  // even if we do not explicitly specify them as null  then also it is by default                                            //set up as null in JAVA but it is always a good practise to assign them as          this.right = null;                                    //null

         

        }

    }

 

    static class BinaryTree {

        static Node buildTree(int rootValue) {

            if (rootValue == -1) return null;

            Node newNode = new Node(rootValue); // Now you can instantiate Node directly

            //newNode.left = null;

            //newNode.right = null;

            return newNode;

        }

    }


    public static void main(String[] args) {

        // You can create instances of Node and BinaryTree here as needed

        BinaryTree bt = new BinaryTree();

//For the sake of understanding we are doing hardcoding now. We'll see user input coding also in next article

        Node root = BinaryTree.buildTree(10); //Node root = bt.buildTree(10);

        root.left = new Node(6); 

        root.right= new Node(12);

        root.left.left = new Node(5);

        root.right.right = new Node(7);

        System.out.println(root.data);

        System.out.println(root.left.data+" "+root.right.data);

        System.out.println(root.left.left.data+" "+root.right.right.data);

         /* Our Tree is something like below:

                10
                /\

            6       12

            /            \

        5                    7

*/

    }

}

/*

Question: when we are removing both newnode.right and newnode.left = null and also this,left=null and this.right=null then also we are getting root.left value as null and no error is coming why?

The reason you're not seeing any errors or changes in the behavior of your program when you remove both newnode.left = null; and newnode.right = null; from the buildTree method and also remove this.left = null; and this.right = null; from the Node constructor is because Java automatically initializes object fields to default values when an object is created.

In Java, when you create a new object (instance of a class), all instance variables (fields) are automatically initialized to default values. For numeric types like int, the default value is 0, and for object types, including reference types like Node, the default value is null.

In your Node class constructor, you are manually setting left and right to null, but if you don't provide any initialization for these fields, Java will still initialize them to null by default when you create a new Node object.

Similarly, in the buildTree method, even if you remove the lines that explicitly set newnode.left and newnode.right to null, Java will automatically set them to null when the Node object is created.

That's why you're not observing any difference in behavior or errors when you remove these lines. Java's default behavior takes care of it for you.

 */

Thursday, 24 August 2023

 Codeforces Round 894 Div 3 Solution:

Problem Statement Link -->   Problem - A - Codeforces

Solution in JAVA:


import java.util.*;


public class test {


    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);


        int testCases = scanner.nextInt();


        for (int testCase = 0; testCase < testCases; testCase++) {


            int numRows = scanner.nextInt();

            int numCols = scanner.nextInt();


            List<String> carpetRows = new ArrayList<>();


            for (int i = 0; i < numRows; i++) {

                carpetRows.add(scanner.next());

            }


            int requiredColumns = 4;

            String target = "vika";

            int targetIndex = 0;

            int selectedColumns = 0;


            for (int col = 0; col < numCols; col++) {


                boolean foundColumn = false;


                for (int row = 0; row < numRows; row++) {


                    String rowContent = carpetRows.get(row);

                    char currentChar = rowContent.charAt(col);


                    if (currentChar == target.charAt(targetIndex)) {

                        targetIndex++;

                        foundColumn = true;

                        break;

                    }

                }


                if (foundColumn) {

                    selectedColumns++;

                    if (selectedColumns == requiredColumns) {

                        break;

                    }

                }

            }


            if (selectedColumns == requiredColumns) {

                System.out.println("YES");

            } else {

                System.out.println("NO");

            }

        }


        scanner.close();

    }

}


Wednesday, 23 August 2023

Codeforces Round 640 Div 4 Solution:

Problem Statement Link -->  Status - Codeforces

Solution in JAVA:


import java.util.*;


public class test {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();

        

        for (int i = 0; i < t; i++) {

            int num = sc.nextInt();

            List<Integer> terms = new ArrayList<>();

            

            int multiplier = 1;

            while (num > 0) {

                int digit = num % 10;

                if (digit > 0) {

                    terms.add(digit * multiplier);

                }

                num /= 10;

                multiplier *= 10;

            }

            

            System.out.println(terms.size());

            for (int term : terms) {

                System.out.print(term + " ");

            }

            System.out.println();

        }

        

        sc.close();

    }

}


Monday, 21 August 2023

Codeforces Round 107 Div 2 Solution:

Problem Statement Link --> Problem - 151A - Codeforces

Solution in JAVA:


import java.util.*;


public class test {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n, k, l, c, d, p, nl, np;

        n = sc.nextInt();

        k = sc.nextInt();

        l = sc.nextInt();

        c = sc.nextInt();

        d = sc.nextInt();

        p = sc.nextInt();

        nl = sc.nextInt();

        np = sc.nextInt();

        int num_of_toasts = Math.min(Math.min((k * l) / (n * nl), (c * d) / n), p / (n * np));

        System.out.println(num_of_toasts);

        sc.close();

    }

}


Codeforces Round 161 Div 2 Solution in Java

Problem Statement Link --> Problem - 263A - Codeforces

Solution in JAVA:

import java.util.*;

public class test {

public static void main(String[] args) {

    int arr[][] = new int[6][6] ;

    int r=0;int c=0;

    Scanner sc = new Scanner(System.in);

    for (int i = 1; i < 6; i++) {

        for (int j = 1; j < 6; j++) {

           arr[i][j] = sc.nextInt();

           if(arr[i][j] == 1)

           {

            r = Math.abs(3-i);

            c = Math.abs(3-j);

           }

        }

    }

    System.out.println(r+c);

    sc.close();

  }   

}

Tuesday, 15 August 2023

Video Solution for Codeforces Round 893 DIV 2 - Problem A in #JAVA 



Hello fellow programmers,

For those of you who participated in Codeforces Round 893 DIV 2 and were working on Problem A, here is the solution in JAVA



Solution in JAVA 

import java.util.*;

public class test {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();   //for total number of test cases
        for (int i = 0; i < t; i++) {
            int a, b, c;   
            a = sc.nextInt();
            b = sc.nextInt();
            c = sc.nextInt();
            int qwerty = c/2;  
            a = a + c - qwerty;
            b = b + qwerty;
            if(a>b)
            {
                System.out.println("First"); //Anna will win
            }
            else
            {
                System.out.println("Second"); //else Katie
            }
             
    }
    sc.close();
  }
}

Saturday, 12 August 2023

Video Solution for Codeforces Round 892 DIV 2 - Problem A in #JAVA 


Hello fellow programmers,

For those of you who participated in Codeforces Round 892 DIV 2 and were working on Problem A, here is the solution in JAVA


import java.util.*;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt(); //FOR NUMBER OF TEST CASES

        for (int i = 0; i < t; i++) {
            int n = sc.nextInt(); // length of the array a
            int[] a = new int[n];
            
            // Input the array a
            for (int j = 0; j < n; j++) {
                a[j] = sc.nextInt();
            }
            
            Arrays.sort(a); //SORT THE ARRAY SO THAT IT CAN BE DIVIDED INTO b and c
            boolean flag=false; //TAKING variable flag for output purpose
            for(int k=0 ;  k<n-1 && !flag;k++) // and flag is important 
            {
                if(a[k] != a[k+1])
                {
                    flag=true;
                
             
            System.out.println(k+1 + " " + (n-k-1)); 
            for(int m=0;m<=k;m++)
            {
                System.out.print(a[m]+" ");
            }
            System.out.println();
            for(int p=k+1;p<n;p++)
            {
                System.out.print(a[p]+" ");
            }
                 }
                   
            } //loop is over here
            if(!flag) //otherwise -1 according to the question
                 {
                    System.out.println(-1);
                 }
        }
        sc.close();
    }
}

 
 
 

Friday, 14 July 2023

Hello World!! 

Data Visualization Project: Census of India 2011 - Population of Religious Groups in a State

Project's Link - https://amanraj1212.pythonanywhere.com

My Portfolio website's Link - https://amanraj-website.netlify.app

This is a full stack project from development to deployment.


I have created an intriguing data visualization project focused on the Census of India 2011. This project aims to provide insights into the population distribution of different religious groups across a particular state. By utilizing the data from the Census, I have implemented a bar graph visualization that effectively represents the population numbers for each religious group.


The visualization allows users to explore and compare the population of various religious groups within the selected state. By presenting the data in a bar graph format, it becomes easy to comprehend the relative sizes and proportions of each religious group. This visualization enables users to identify trends, patterns, and disparities in the population distribution, facilitating a better understanding of the demographics within the state.


Through this project, I have successfully combined data analysis, visualization techniques, and web development skills to create an informative and interactive tool. It provides a visual representation of the religious diversity within the chosen state based on the Census data from 2011.


Overall, my data visualization project offers a valuable resource for individuals interested in exploring the population distribution of religious groups in a specific state. It highlights the power of data visualization in conveying complex information effectively and engagingly.


Feel free to visit my website and explore my data visualization project further. It's a great opportunity to gain insights into the Census of India 2011 and appreciate the power of data visualization in understanding demographic trends.

Sunday, 11 June 2023

Here is a Java practice problem taken from HackerRank that tests your knowledge of strings. The problem involves arranging strings in lexicographical order and capitalizing the first letter of each word. It's a great exercise for revision and practice. Here's the problem description and a sample solution:

Problem statement:

Given two strings of lowercase English letters, A and B, perform the following operation:

  1. Sum of length of A and B.
  2. Determine if A is lexicographically larger than B(i.e.: B comes after A in dictionary?).
  3. Capitalize the first letter of A and B and print them on a single line, separated by a space.

Input Format

The first line contains a string A. The second line contains another string B. The strings are comprised of only lowercase English letters.

Output Format

There are three lines of output:
For the first line, sum the lengths of A and B.
For the second line, write Yes if is A lexicographically greater than B otherwise print No instead.
For the third line, capitalize the first letter in both A and B and print them on a single line, separated by a space.

Sample Input
 hello
 java
Sample Input
 9
 No
 Hello Java
Explanation

String A is “hello” and B is “java”. A has a length of 5, and B has a length of 4; the sum of their lengths is 9.
When sorted alphabetically/lexicographically, “hello” precedes “java”; therefore, A is not greater than B and the answer is No. When you capitalize the first letter of both A and B and then print them separated by a space, you get “Hello Java”.

// My solution:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
         
        Scanner scanner = new Scanner(System.in);
        String A = scanner.nextLine();
        String B = scanner.nextLine();
        scanner.close();
        String lCaseA= A.toLowerCase();
        String lCaseB=B.toLowerCase();
        System.out.println(lCaseA.length()+lCaseB.length());
        String flag="Yes";
        int min=lCaseB.length();
        if(lCaseA.length()<=lCaseB.length()) min=lCaseA.length();
        //System.out.println(max);
        int i=0;
        while(i<min)
        {
           if((int)lCaseA.charAt(i)<(int)lCaseB.charAt(i))
           {
              flag="No";
              break;
           }
           else if((int)lCaseA.charAt(i)==(int)lCaseB.charAt(i))
           {
            i++;   
            flag="No";
            continue;
           }
           else
           {
            break;
           }
         
        }
      //System.out.println(lCaseA);
      //System.out.println(lCaseB);
      System.out.println(flag);   
      
      String ucase=" ";
       Scanner scaner=new Scanner(lCaseA);
       while(scaner.hasNext())
       {
        String word=scaner.next();
        ucase+=Character.toUpperCase(word.charAt(0))+word.substring(1)+" ";

       }
       System.out.print(ucase.trim()+" ");
       scaner.close();
       
       String ucase1=" ";
       Scanner scaner1=new Scanner(lCaseB);
       while(scaner1.hasNext())
       {
        String word1=scaner1.next();
        ucase1+=Character.toUpperCase(word1.charAt(0))+word1.substring(1)+" ";

       }
       System.out.println(ucase1.trim());
       scaner1.close();
    }
}

// program ends

While the provided solution uses two separate while loops for simplicity,
it is worth mentioning that a dedicated function can be created to encapsulate
the logic of capitalizing the first letter of a word. This approach can make the
code more modular and readable. (For the sake of better understanding I showed
with two separate loops)
By practicing this problem, you will gain a deeper understanding of string
manipulation and improve your ability to solve similar challenges efficiently.
Mastery of these concepts will prove valuable in real-world scenarios where
string handling plays a crucial role. Remember to consider edge cases, such as handling empty strings or strings with
leading/trailing spaces(trim()). Understanding these nuances will enhance the robustness
of your solution and make it applicable in various scenarios. Conclusion: This Java practice problem focusing on arranging strings in lexicographical
order and capitalizing the first letter of each word provides an excellent
opportunity to revise and strengthen your skills in string manipulation.
By understanding and implementing the solution, you will enhance your ability to
work with strings effectively and tackle similar challenges with confidence.
Practice this problem and continue exploring more string-related problems to
expand your knowledge and become proficient in different programming logic.

Good thoughtful question on Binary search on answers

Problem link:  https://leetcode.com/problems/maximize-score-of-numbers-in-ranges/description/ Solution: //import java.util.Arrays; class So...