Skip to content
Open
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.debug.settings.onBuildFailureProceed": true
}
37 changes: 37 additions & 0 deletions LongestSubarraySumK.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.*;
public class LongestSubarraySumK
{
public static int longestSubarrayWithSumK(int[] arr, int k) {
Map<Integer, Integer> prefixSumMap = new HashMap<>();
int sum = 0, maxLen = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
// If sum equals K, whole array till i has sum K
if (sum == k) {
maxLen = i + 1;
}
if (prefixSumMap.containsKey(sum - k)) {
maxLen = Math.max(maxLen, i - prefixSumMap.get(sum - k));
}
// Store sum if it's not already present
prefixSumMap.putIfAbsent(sum, i);
}
return maxLen;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size:");
int n = sc.nextInt();
int[] a = new int[n];
System.out.println("Enter array elements:");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.println("Enter K:");
int k = sc.nextInt();
int result = longestSubarrayWithSumK(a, k);
System.out.println("Length of longest subarray with sum " + k + " is: " + result);
}
}

30 changes: 30 additions & 0 deletions SearchingAlgorithms/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package SearchingAlgorithms;

public class BinarySearch {
public static int binarySearch(int[] arr, int target) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;

if (arr[mid] == target)
return mid;
if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}

public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int target = 40;

int result = binarySearch(arr, target);
if (result != -1)
System.out.println("Element found at index: " + result);
else
System.out.println("Element not found");
}
}

43 changes: 43 additions & 0 deletions SearchingAlgorithms/ExponentialSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package SearchingAlgorithms;

public class ExponentialSearch {
public static void main(String[] args) {
int[] arr = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int target = 14;
int result = exponentialSearch(arr, target);

if (result == -1)
System.out.println("Element not found");
else
System.out.println("Element found at index: " + result);
}

static int exponentialSearch(int[] arr, int target) {
int n = arr.length;

// If the target is at first index
if (arr[0] == target)
return 0;

// Find range for binary search by repeated doubling
int i = 1;
while (i < n && arr[i] <= target)
i = i * 2;

// Call binary search for the found range
return binarySearch(arr, i / 2, Math.min(i, n - 1), target);
}

static int binarySearch(int[] arr, int left, int right, int target) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
}
38 changes: 38 additions & 0 deletions SearchingAlgorithms/InterpolationSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package SearchingAlgorithms;
public class InterpolationSearch {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int target = 70;
int result = interpolationSearch(arr, target);

if (result == -1)
System.out.println("Element not found");
else
System.out.println("Element found at index: " + result);
}

static int interpolationSearch(int[] arr, int target) {
int low = 0, high = arr.length - 1;

while (low <= high && target >= arr[low] && target <= arr[high]) {
// Avoid division by zero
if (arr[low] == arr[high]) {
if (arr[low] == target)
return low;
else
return -1;
}

// Estimate position using interpolation formula
int pos = low + ((target - arr[low]) * (high - low)) / (arr[high] - arr[low]);

if (arr[pos] == target)
return pos;
if (arr[pos] < target)
low = pos + 1;
else
high = pos - 1;
}
return -1;
}
}
44 changes: 44 additions & 0 deletions SearchingAlgorithms/JumpSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package SearchingAlgorithms;

public class JumpSearch {
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
int target = 13;
int result = jumpSearch(arr, target);

if (result == -1)
System.out.println("Element not found");
else
System.out.println("Element found at index: " + result);
}

static int jumpSearch(int[] arr, int target) {
int n = arr.length;

// Finding block size to jump
int step = (int)Math.floor(Math.sqrt(n));
int prev = 0;

// Jump ahead while the last element of block < target
while (arr[Math.min(step, n) - 1] < target) {
prev = step;
step += (int)Math.floor(Math.sqrt(n));
if (prev >= n)
return -1;
}

// Linear search in the found block
while (arr[prev] < target) {
prev++;
if (prev == Math.min(step, n))
return -1;
}

// If element is found
if (arr[prev] == target)
return prev;

return -1;
}
}

Binary file added SearchingAlgorithms/LinearSearch.class
Binary file not shown.
22 changes: 22 additions & 0 deletions SearchingAlgorithms/LinearSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package SearchingAlgorithms;

public class LinearSearch {
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target)
return i;
}
return -1;
}

public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int target = 30;

int result = linearSearch(arr, target);
if (result != -1)
System.out.println("Element found at index: " + result);
else
System.out.println("Element not found");
}
}
37 changes: 37 additions & 0 deletions SearchingAlgorithms/TernarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package SearchingAlgorithms;

public class TernarySearch {
public static void main(String[] args) {
int[] arr = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int target = 16;
int result = ternarySearch(arr, 0, arr.length - 1, target);

if (result == -1)
System.out.println("Element not found");
else
System.out.println("Element found at index: " + result);
}

static int ternarySearch(int[] arr, int left, int right, int target) {
if (right >= left) {
// Split the range into 3 parts
int mid1 = left + (right - left) / 3;
int mid2 = right - (right - left) / 3;

// Check if target is at any mid
if (arr[mid1] == target)
return mid1;
if (arr[mid2] == target)
return mid2;

// Recur in left or right part
if (target < arr[mid1])
return ternarySearch(arr, left, mid1 - 1, target);
else if (target > arr[mid2])
return ternarySearch(arr, mid2 + 1, right, target);
else
return ternarySearch(arr, mid1 + 1, mid2 - 1, target);
}
return -1;
}
}