Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name Added and Code Added #2684

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"java.project.sourcePaths": [
"Program's_Contributed_By_Contributors/Java_Programs/Sorting/QuickSort.java",
"Program's_Contributed_By_Contributors/Java_Programs/Data_structure"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.*;

public class InfiniteSortedArray {
public static void main(String[] args) {
int[] arr = { 3, 5, 7, 9, 10, 13, 27, 90, 200, 300 };
int target = 10;

System.out.println(ans(arr, target));
}

static int ans(int[] arr, int target) {
// first find the box
// first start with a box of size 2
int start = 0;
int end = 1;

// condition for the target to lie in the range
while (target > arr[end]) {
int newstart = end + 1;
end = end + (end - start + 1) * 2;
start = newstart;

}
return binarysearch(arr, target, start, end);
}

static int binarysearch(int[] arr, int target, int start, int end) {
while (start <= end) {
// finding the middle element
// int mid = (start + end) / 2 not using this because mmight be the integer
// exceeds the java capacity
int mid = start + (end - start) / 2;
if (target < arr[mid]) {
end = mid - 1;
} else if (target > arr[mid]) {
start = mid + 1;
} else {
// answer found
return mid;
}
}
return -1;
}
}
5 changes: 5 additions & 0 deletions contributors/contributorsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,11 @@ contributors = [
id:387,
fullname: "Navneet Raj",
username: "https://github.com/2nav"
},
{
id: 388,
fullname: "Abhishek Anand",
username: "https://github.com/picturepatch07"
}

];