Tuesday, April 6, 2021

Published April 06, 2021 by Anonymous with 0 comment

Check if every node can me made accessible from a node of a Tree by at most N/2 given operations

import java.io.*;

import java.util.HashMap;

  

class GFG {

  

    

    

    

    public static void

    findNode(HashMap<Integer, Integer> map,

             int n)

    {

  

        

        

        int[] a = new int[n];

  

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

            a[i] = map.getOrDefault(i + 1, 0);

        }

  

        

        

        int count0 = 0;

  

        

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

  

            

            

            if (a[i] == 0) {

  

                

                count0++;

            }

        }

  

        count0 -= 1;

  

        

        

        if (count0

            <= Math.floor(((double)n)

                          / ((double)2))) {

            System.out.println("Yes");

        }

  

        

        else

            System.out.println("No ");

    }

  

    

    public static void main(String[] args)

    {

        

        int N = 3;

  

        

        HashMap<Integer, Integer> map

            = new HashMap<>();

  

        map.put(1, 0);

        map.put(2, 2);

        map.put(3, 0);

  

        findNode(map, N);

    }

}

Let's block ads! (Why?)


Original page link

Best Cool Tech Gadgets

Top favorite technology gadgets
      edit

0 comments:

Post a Comment