Skip to content

Commit

Permalink
[Java] : Binary Search Iterative/Recursive approach and some coding s…
Browse files Browse the repository at this point in the history
…tyle changes (#13)

Co-authored-by: Ritabrata Nag <[email protected]>
  • Loading branch information
Ritabrata1080 and Ritabrata Nag authored Aug 31, 2024
1 parent 4552451 commit effa73e
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
40 changes: 40 additions & 0 deletions binSearch/iterativeBinSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Created by Ritabrata, 31/08/2024
*/

package Codes_contest.binSearch;

import java.util.Scanner;

public class iterativeBinSearch {
public static int binSearchIteration(int[] a, int n, int t) {
int low = 0;
int high = n - 1;
int mid = -1;
while (low <= high) {
mid = (low + high) / 2;
if (a[mid] == t) {
return mid;
} else if (t > a[mid]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return mid;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int target = sc.nextInt();
int index = binSearchIteration(a, n, target);
System.out.println((index != -1) ? "Target is found" : "Target is not found");
System.out.println("Target element is found at index : " + index);
sc.close();
}
}
37 changes: 37 additions & 0 deletions binSearch/recursiveBinSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Created by Ritabrata, 31/08/2024
*/


package Codes_contest.binSearch;

import java.util.Scanner;

public class recursiveBinSearch {
public static int recursionOnArray(int[] a, int n, int target, int low, int high) {
if (low > high) {
return -1;
}
int mid = (low + high) / 2;
if (a[mid] == target) {
return mid;
} else if (a[mid] > target) {
return recursionOnArray(a, n, target, low, mid - 1);
} else {
return recursionOnArray(a, n, target, mid + 1, high);
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int target = sc.nextInt();
int targetIndex = recursionOnArray(a, n, target, 0, n - 1);
System.out.println("Target element found at index : " + targetIndex);
sc.close();
}
}
5 changes: 4 additions & 1 deletion goodbye_2023_C.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package Codes_contest;

import java.util.*;

public class goodbye_2023_C {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int n = sc.nextInt();
// Input handling
while(t --> 0){
generateSquareNumbers(n);
}

scanner.close();
sc.close();
}
static void generateSquareNumbers(int n) {
int[] numbers = new int[n];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package leetcode;
package Codes_contest.leetcode;

import java.util.*;
class Solution {
class RobotCollisions {
class Robot{
int position;
int health;
Expand Down
2 changes: 1 addition & 1 deletion leetcode/01-two-sum.java → leetcode/TwoSum.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package leetcode;
import java.util.*;
class Solution {
class TwoSum {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
HashMap<Integer,Integer> map = new HashMap<>();
Expand Down
1 change: 0 additions & 1 deletion leetcode_biweekly_121_C.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ static int helper(int x, int y, int minOperations, Queue<Integer> q){
}
return minOperations;
}
public static void main(Str)
}

0 comments on commit effa73e

Please sign in to comment.