1971. Find if Path Exists in Graph

1971. Find if Path Exists in Graph

class Solution {
    public boolean validPath(int n, int[][] edges, int source, int destination) {
        if(source == destination) return true;
        Graph g = new Graph(n);
        
        for(int i = 0 ; i < edges.length; i++){
            g.addEdge(edges[i][0],edges[i][1]);
            g.addEdge(edges[i][1],edges[i][0]);
        }
        boolean[] visited = new boolean[n];
        
        Queue<Integer> q = new LinkedList<Integer>();
        q.add(source);
        visited[source] =true;
        while(!q.isEmpty()){
            int current = q.poll();
             Iterator<Integer> i = g.adj[current].listIterator();
            while (i.hasNext())
            {
                int next = i.next();
                if(next == destination) return true;
                if (!visited[next])
                {
                    visited[next] = true;
                    q.add(next);
                   
                }
            }
            
            
        }
        
        return false;
    }
    class Graph
{
    private int V;   // No. of vertices
    private LinkedList<Integer> adj[]; //Adjacency Lists
 
    // Constructor
    Graph(int v)
    {
        V = v;
        adj = new LinkedList[v];
        for (int i=0; i<v; ++i)
            adj[i] = new LinkedList();
    }
 
    // Function to add an edge into the graph
    void addEdge(int v,int w)
    {
        adj[v].add(w);
    }
}
}

605. Can Place Flowers

605. Can Place Flowers

class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        if(flowerbed.length == 1 && flowerbed[0] == 0) n--;
        for(int i = 0; i < flowerbed.length; i++){
            if(i ==0 && flowerbed[i] == 0 && i+1 <  flowerbed.length && flowerbed[i+1] == 0){
                flowerbed[i] = 1;
                n--;
            }else if( i == flowerbed.length -1 && 
                     flowerbed[i] == 0 && i-1 >= 0 &&
                     flowerbed[i-1] == 0){
            flowerbed[i] = 1;
                n--;
            }
            else if(flowerbed[i] == 0  &&
                    i-1 >= 0 &&
                    flowerbed[i-1] == 0  &&
                    i+1 <  flowerbed.length &&
                    flowerbed[i+1] == 0){
                flowerbed[i] = 1;
                n--;
            } 
        }
        if(n <= 0) return true;
        else return false;
    }
}

Mini-Max Sum

https://www.hackerrank.com/challenges/mini-max-sum/problem

   public static void miniMaxSum(List<Integer> arr) {
    // Write your code here
         Collections.sort(arr);
         BigInteger sum = BigInteger.ZERO;
         for (int i = 0; i < arr.size(); i++){
              sum =sum.add(BigInteger.valueOf(arr.get(i)));
         }
         System.out.print(sum.subtract(BigInteger.valueOf(arr.get(arr.size()-1).intValue()))+" ");
         System.out.print(sum.subtract(BigInteger.valueOf(arr.get(0).intValue())));
      
    }

Compare the Triplets

https://www.hackerrank.com/challenges/compare-the-triplets/problem

    public static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
    // Write your code here
        List<Integer> out = new ArrayList<Integer>(2);
        out.add(0);
        out.add(0);
        for (int i = 0; i < a.size(); i++) {
            if (a.get(i) > b.get(i))
                out.set(0, out.get(0) + 1);
            else if (a.get(i) < b.get(i))
                out.set(1, out.get(1) + 1);
        }

        return out;
    }