Search code examples
arraysalgorithmpseudocode

Pseudocode for an array search


The question:

"Write an algorithm that given an array A and an integer value k it returns the value true if there are two different integers in A that sum to k, and it returns false otherwise."

My pseudocode:

Input: array A of size n with value k

Output: true if two different integers in A sum to k, false otherwise

Algorithm ArraySum(A, n, k)
for (i=0, i<n, i++)
    for (j=i+1, j<n, j++)
        if (A[i]+A[j]=k)
            return true
return false

Have I written this algorithm correctly? Are there any mistakes I'm just not seeing?


Solution

  • If two different integers means A[i], A[j] where i != j rather than A[i] != A[j], your pseudocode is correct.