0

Apologies if this does not seem related to code I have written but would want to understand what is going on in these Java code snippets(I do not understand Java to the extend I could decode this). I would like to implement these snippets in C(which I know fair bit). I see in snippet one there is some Hash Table search going on like elements of one array used as keys to search other array, but could not get it correctly.

1] Snippet 1.

The problem it is trying to solve is: Find first covering prefix of a array

For example, the first covering prefix of the following 5�?�element array A:

A[0] = 2  A[1] = 2  A[2] = 1
A[3] = 0  A[4] = 1

is 3, because sequence [ A[0], A[1], A[2], A[3] ] equal to [2, 2, 1, 0], contains all values that occur in array A.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;


class FirstCovering {
    int ps ( int[] A ) {
        ArrayList<Integer> arrA = new ArrayList<Integer>(A.length);
        for (int i = 0; i < A.length; i++) {
                arrA.add(A[i]);
        }

        HashSet<Integer> hashSet = new HashSet<Integer>(arrA);
        Iterator<Integer> iter = hashSet.iterator();

        int index = 0, tempIndx=0;
        while (iter.hasNext()) {

                tempIndx = arrA.indexOf(iter.next());
                if (tempIndx > index ) index = tempIndx;
        }

        return index;
    }
}

2] Snippet 2

class ComplementaryPairs {

  private static String palindrome;
  public static void main(String[] args) {

    int array[] = {4,5};
    int a = complementary_pairs(6, array);
    System.out.println(a);

    int array2[] = {4,5};
    int b = complementary_pairs(4, array2);
    System.out.println("b = " + b);
   }

  static int complementary_pairs ( int k,int[] A ) {
    // find count of complementary pairs from array A.
    int count = 0;
    for (int i = 0; i < A.length; i++) {
      for (int j = 0; j < A.length; j++) {
        if (A[j] + A[i] == k) {
          count++;
        }
      }
    }
    return count;
  }
}
5
  • 3
    I suggest compiling and running this code through a debugger to understand what it does. It doesn't look malicious so you should be fine to do so and this really is the best way to understand it yourself.
    – Bernard
    Commented Jan 27, 2012 at 16:22
  • @goldenmean This is an interesting and well formed question, it is however off topic here (by virtue of being about a specific language specifically) but on topic on Stack-overflow and thus will be moved there.
    – user777082
    Commented Jan 27, 2012 at 18:47
  • 1
    @WorldEngineer, this is not a good question for either Stack Overflow or Programmers. Bernard's suggestion is correct: run the code. We're not here to help people understand code, we are here to help people get code to work (or answer why it can't). Commented Jan 27, 2012 at 18:54
  • @AnthonyPegram I stand corrected.
    – user777082
    Commented Jan 27, 2012 at 19:00
  • @AnthonyPegram Where in the Stack Overflow FAQ or About page does it say that we shouldn't help people understand code? Seems rather pompous. Commented Jan 27, 2012 at 19:20

2 Answers 2

1

You are correct about snippet 1, though you could do this in a single pass of the array...

public int lastNonRepeat( int[] a )
{
    HashMap map = new HashMap();
    int lastIndex = 0;
    for( int i = 0; i < a.length; i++ )
    {
        if( !map.containsKey(a[i]) )
        {
            map.put(a[i],true);
            lastIndex = i;
        }
    }
    return lastIndex;
}

For snippet 2, the complementary pairs section is just checking to see if the sum of two numbers in an array equals k. The time complexity of the method is O(n^2).

Of Note: a[0] + a[0] is valid in this implementation.

1

I upvoted @The Real Baumann's solution, but I'd suggest that there's an even simpler solution. The java.util.HashSet.add() function returns true only when the add modifies the set, thus you could do the following:

public int ps( int[] a ) {
    HashSet set = new HashSet();
    int lastIndex = 0;
    for( int i = 0; i < a.length; i++ ) {
        if( set.add(a[i]) ) {
            lastIndex = i;
        }
    }
    return lastIndex;
}
1
  • It's been a few years since I touched Java so I'm not that familiar with the API. Nice optimization though. Commented Jan 28, 2012 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.