Skip to content
This repository has been archived by the owner on Oct 19, 2020. It is now read-only.

algo. in java to find the index of lowest and highest number #14

Open
wants to merge 3 commits into
base: competitive
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions Algorithms to find index in java/lowerBound.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public int lowerBound(ArrayList<Integer> ar,int k)
{
int s=0;
int e=ar.size();
while (s !=e)
{
int mid = s+e>>1;
if (ar.get(mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
15 changes: 15 additions & 0 deletions Algorithms to find index in java/upperBound.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public int upperBound(int[] arr, int key){
int s = 0;
int e = arr.length;
while(s != e){
int mid = (s+e)>>1;
if(arr[mid] >= k) // same as lower bound only diff. is in this line
s = mid+1;
else
e = mid;
}
if(s == arr.length)
return -1;

return s;
}
50 changes: 50 additions & 0 deletions ZAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.io.*;
import java.util.*;

class ZAlgorithm {
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String c = br.readLine();
String s = br.readLine();
int m = c.length();
int n = s.length();
c += "#" + s;
int count = 0;
int[] z = new int[n+m+1];

//System.out.println(c);
int L = 0, R = 0;
for (int i = 1; i < m+n+1; i++){
if (i > R){
L = R = i;
while (R < n && c.charAt(R-L) == c.charAt(R)){
R++;
}
z[i] = R-L;
R--;
}
else{
int k = i-L;
if (z[k] < R-i+1){
z[i] = z[k];
}
else{
L = i;
while (R < n && c.charAt(R-L) == c.charAt(R)){
R++;
}
z[i] = R-L;
R--;
}
}

if(z[i] == m)
count++;
}

// for(int x: z)
// System.out.print(x+"");

System.out.println(count);
}
}
31 changes: 31 additions & 0 deletions modifiedBinary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.io.*;
import java.util.*;

class TestClass {
public static void main(String args[] ) throws Exception {
// Write your code here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int m = Integer.parseInt(str[1]);
String[] an = br.readLine().split(" ");
PriorityQueue<Integer> pq = new PriorityQueue<>();
int[] a = new int[n];
for(int i=0;i<n;i++)
a[i] = Integer.parseInt(an[i]);
long l = 1, r = 10000000, ans = 10000000;
while(l <= r){
long mid = (l+r)/2;
long s = 0;
for(int i = 0; i < n; i++)
s += mid/a[i];
if(s >= m){
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
System.out.println(ans);
}
}