What's wrong in the following

import java.util.*;
public class Main {
public static void main (String args[]) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
String strings[] = new String[num];
for(int i=0; i < num; i++){
strings[i] = sc.next();
}

	String word1 = sc.next();
	String word2 = sc.next();

	System.out.print(maxDist(strings, word1, word2));
}


public static int maxDist(String[] words, String word1, String word2){
	int min = words.length;

	if(word1.equals(word2)){
		int p = -1;
		for(int i=0; i<words.length; i++) {
			if(words[i].equals(word1)){
				if(p == -1){
					p = i;
				}else{
					min = Math.min(min, Math.abs(p-i));
					p =i;
				}
			}
		}
	} else {
		int a1 = -1;
		int a2 = -1;
		for(int i = 0; i < words.length; i++){
			if(words[i].equals(word1)){
				a1 = i;
			}else if(words[i].equals(word2)){
				a2 = 1;
			}
			if(a1 != -1 && a2 != -1){
				min = Math.min(min, Math.abs(a1-a2));
			}
		}
	}
	return min;
 }

}

Hi @subodh_dreaming_faang

Can you please share the Question link with me. It would be easier for me to help you further.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.