Given an integer k and a list of integers, count the number of distinct valid pairs of integers (a, b) in the list for which a + k = b. Two pairs of integers (a, b) and (c, d) are considered distinct if at least one element of (a, b) does not also belong to (c, d). Note that the elements in a pair might be the same element in the array. An instance of this is below where k = 0. Example n = 4 numbers = [1, 1, 1, 2] k = 1
תשובות
הוסף תשובה
|
לצפיה בתשובות
נובמבר 2024
import java.util.HashSet;
import java.util.Set;
public class DistinctPairs {
public static void main(String[] args) {
int[] numbers = {1, 1, 1, 2};
int k = 1;
int result = countDistinctPairs(numbers, k);
System.out.println("Number of distinct valid pairs: " + result);
}
public static int countDistinctPairs(int[] numbers, int k) {
Set seen = new HashSet<>();
Set pairs = new HashSet<>();
for (int number : numbers) {
if (seen.contains(number + k)) {
pairs.add(Math.min(number, number + k) + "," + Math.max(number, number + k));
}
if (seen.contains(number - k)) {
pairs.add(Math.min(number, number - k) + "," + Math.max(number, number - k));
}
seen.add(number);
}
ריאיון עם מנהל צוות ומנהל בדיקות, ריאיון משאבי אנוש וריאיון עם מנהל הפיתוח.
שאלות מתוך הראיון
כיצד יש להתמודד עם דף HTML שמתעדכן במהלך בדיקתו על ידי הוספת מקטעים נוספים בעלי מבנה זהה?
תשובות
הוסף תשובה
על מנת לצפות במידע ששותף על ידי חברי TheWorker
אנא שתפו גם אתם את הקהילה!
דוקיוסיין
1000 עובדים
משרדים ראשיים: פתח תקווה
empowering more than 225,000 companies and 85 million users in 188 countries to sign, send and manage documents anytime, anywhere, on any device, with confidence.