diff --git a/LinearSearch/linear.java b/LinearSearch/linear.java new file mode 100644 index 0000000..3fa764f --- /dev/null +++ b/LinearSearch/linear.java @@ -0,0 +1,22 @@ +package com.company.LinearSearch; +import java.util.Scanner; +public class linear{ + + public static void main(String[] args){ + Scanner sc=new Scanner(System.in); + System.out.print("Enter the size of array: "); + int n=sc.nextInt(); + int[] arr=new int[n]; + for(int i=0;i nests,int sep) { - int birds = 1; - int location = nests.get(0); - - for(int i=1; i<=N-1; i++){ - int current_location = nests.get(i); - if(current_location - location >= sep){ - birds++; - location = current_location; - - if(birds==B){ - return true; - } - } - } - return false; - - } - public static void main(String[] args) { - int b = 3; - ArrayList nests =new ArrayList<>(); - nests.add(1); - nests.add(2); - nests.add(4); - nests.add(8); - nests.add(9); - - Collections.sort(nests); - int N = nests.size(); - //Binary Search - int s = 0; - int e = nests.get(N-1) - nests.get(0); - int ans = -1; - - while(s<=e){ - int mid = (s+e)/2; - - boolean canPlace = canPlaceBirds(b,N,nests,mid); - if(canPlace){ - ans = mid; - s = mid + 1; - } - else{ - e = mid - 1; - } - } - System.out.println(ans); - } -} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy