Find the unvisited positions in Array traversal


import java.util.*;

  

class GFG {

    public static int unvisitedpositions(int N, int d,

                                         int A[])

    {

  

        

        

        int positionStatus[] = new int[d + 1];

  

        

        

        

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

            if (A[i] <= d && positionStatus[A[i]] == 0) {

  

                

                

                

                for (int j = A[i]; j <= d; j += A[i]) {

                    positionStatus[j] = 1;

                }

            }

        }

  

        

        

        int positionCount = d;

        for (int i : positionStatus) {

            if (i == 1) {

                positionCount--;

            }

        }

  

        

        

        return positionCount;

    }

  

    

    public static void main(String[] args)

    {

        int N = 3;

        int d = 4;

        int[] A = { 3, 2, 4 };

  

        

        int unvisited = unvisitedpositions(N, d, A);

        System.out.println(unvisited);

    }

}


import java.util.*;

  

class GFG {

    public static int unvisitedpositions(int N, int d,

                                         int A[])

    {

  

        

        

        int positionStatus[] = new int[d + 1];

  

        

        

        

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

            if (A[i] <= d && positionStatus[A[i]] == 0) {

  

                

                

                

                for (int j = A[i]; j <= d; j += A[i]) {

                    positionStatus[j] = 1;

                }

            }

        }

  

        

        

        int positionCount = d;

        for (int i : positionStatus) {

            if (i == 1) {

                positionCount--;

            }

        }

  

        

        

        return positionCount;

    }

  

    

    public static void main(String[] args)

    {

        int N = 3;

        int d = 4;

        int[] A = { 3, 2, 4 };

  

        

        int unvisited = unvisitedpositions(N, d, A);

        System.out.println(unvisited);

    }

}

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – admin@technoblender.com. The content will be deleted within 24 hours.
arrayFindLatestpositionsTechTraversalunvisitedUpdates
Comments (0)
Add Comment