Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

two pointer in c++

  int i = 0;
 
    // represents second pointer
    int j = N - 1;
 
    while (i < j) {
 
        // If we find a pair
        if (A[i] + A[j] == X)
            return 1;
 
        // If sum of elements at current
        // pointers is less, we move towards
        // higher values by doing i++
        else if (A[i] + A[j] < X)
            i++;
 
        // If sum of elements at current
        // pointers is more, we move towards
        // lower values by doing j--
        else
            j--;
    }
 
PREVIOUS NEXT
Tagged: #pointer
ADD COMMENT
Topic
Name
6+7 =