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

BST CODE #7927

Open
wants to merge 5 commits into
base: master
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
47 changes: 47 additions & 0 deletions binary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Java implementation of iterative Binary Search

import java.io.*;

class BinarySearch {

// Returns index of x if it is present in arr[].
int binarySearch(int arr[], int x)
{
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;

// Check if x is present at mid
if (arr[mid] == x)
return mid;

// If x greater, ignore left half
if (arr[mid] < x)
low = mid + 1;

// If x is smaller, ignore right half
else
high = mid - 1;
}

// If we reach here, then element was
// not present
return -1;
}

// Driver code
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, x);
if (result == -1)
System.out.println(
"Element is not present in array");
else
System.out.println("Element is present at "
+ "index " + result);
}
}
21 changes: 21 additions & 0 deletions maxKelements.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public long maxKelements(int[] nums, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);

for(int i = 0; i < nums.length; i++){
pq.offer(nums[i]);
}

long max = 0;

while(k-- > 0){
int val = pq.poll();
max += val;
int ceil = (int) Math.ceil(val / 3.0);
pq.offer(ceil);
}

return max;

}
}
15 changes: 15 additions & 0 deletions minimumSteps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public long minimumSteps(String s) {
long swaps = 0;
int whiteCount = 0;

for(int i = s.length() - 1 ; i >= 0; i--){
if(s.charAt(i) == '1'){
swaps += (long) whiteCount;
}else{
whiteCount++;
}
}
return swaps;
}
}