Answers for "minimum number of flips to make k substring at least one 1 in binary string"

1

count number of zeros in array in O(logN)

int firstZero(int arr[], int low, int high) 
{
	if (high >= low) 
    { 
        // Check if mid element is first 0 
        int mid = low + (high - low)/2; 
        if (( mid == 0 || arr[mid-1] == 1) && arr[mid] == 0) 
            return mid; 
        if (arr[mid] == 1)  // If mid element is not 0 
            return firstZero(arr, (mid + 1), high); 
        else  // If mid element is 0, but not first 0 
            return firstZero(arr, low, (mid -1)); 
    } 
    return -1; 
}   
// A wrapper over recursive function firstZero() 
int countZeroes(int arr[], int n) 
{ 
    // Find index of first zero in given array 
    int first = firstZero(arr, 0, n-1); 
    // If 0 is not present at all, return 0 
    if (first == -1) 
        return 0; 
    return (n - first); 
} 

//Credits : GeeksForGeeks
Posted by: Guest on May-02-2020
2

generate all prime number less than n java

/**
Author: Jeffrey Huang
As far as I know this is almost the fastest method in java
for generating prime numbers less than n.
A way to make it faster would be to implement Math.sqrt(i)
instead of i/2.

I don't know if you could implement sieve of eratosthenes in 
this, but if you could, then it would be even faster.

If you have any improvements please email me at
[email protected].
 */


import java.util.*;
 
public class Primecounter {
    
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
      //int i =0;
      int num =0;
      //Empty String
      String  primeNumbers = "";
      boolean isPrime = true;
      System.out.print("Enter the value of n: ");
      System.out.println();
      int n = scanner.nextInt();

      for (int i = 2; i < n; i++) {
         isPrime = true;
         for (int j = 2; j <= i/2; j++) {
            if (i%j == 0) {
               isPrime = false; 
            }
         }
         if (isPrime)
         System.out.print(" " + i);
      }	

    }
}
Posted by: Guest on February-20-2020

Code answers related to "minimum number of flips to make k substring at least one 1 in binary string"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language