Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/main/java/com/walking/intensive/chapter4/task18/Task18.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
*/
public class Task18 {
public static void main(String[] args) {
// Для собственных проверок можете делать любые изменения в этом методе
int[] girls = new int[] {13, 15, 18, 22, 29, 33, 40, 48, 51, 54, 66};
System.out.println(find(girls, 40));
}

/**
Expand Down Expand Up @@ -55,7 +56,33 @@ public static void main(String[] args) {
* </ul>
*/
static int find(int[] girlAges, int targetAge) {
// Ваш код
return 0;
if (!isValid(girlAges, targetAge)) {
return -1;
}

return find(girlAges, 0, girlAges.length, targetAge);
}

static int find(int[] girlAges, int left, int right, int targetAge) {
if (right - left > 1) {

int middle = left + (right - left) / 2;
if (girlAges[middle] == targetAge) {
return girlAges[middle];
}

if (girlAges[middle] > targetAge) {
return find(girlAges, left, middle, targetAge);
}
if (girlAges[middle] < targetAge) {
return find(girlAges, middle, right, targetAge);
}
}

return girlAges[left];
}

static boolean isValid(int[] girlAges, int targetAge) {
return girlAges != null && girlAges.length > 0 && girlAges[0] <= targetAge;
}
}