Cod sursa(job #2683337)

Utilizator mstevanStevan mstevan Data 10 decembrie 2020 22:14:21
Problema Cautare binara Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 2.96 kb
import java.io.*;
import java.util.Scanner;

public class Main {

    private static int n;
    private static int m;
    private static int [] array;

    public static void main(String []args) {
        File inputFile = new File("cautbin.in");
        File outputFile = new File("cautbin.out");

        try {
            FileInputStream inputStream = new FileInputStream(inputFile);
            Scanner scanner = new Scanner(inputStream);

            FileOutputStream outputStream = new FileOutputStream(outputFile);
            PrintWriter writer = new PrintWriter(outputStream);


            n = scanner.nextInt(); // the number of elements
            array = new int[n];
            for (int i = 0; i < n; i++){
                array[i] = scanner.nextInt();
            }
            m = scanner.nextInt(); // the number of commands

            for (int i = 0; i < m; i++) {
                int command = scanner.nextInt();
                int number = scanner.nextInt();
                if (command == 0)
                    writer.println(searchMaxIndex(number) + 1);
                if (command == 1)
                    writer.println(searchLowerOrEqual(number) + 1);
                if (command == 2)
                    writer.println(searchHigherOrEqual(number) + 1);
            }

            writer.close();
            outputStream.close();
        } catch(IOException e) {

        }

    }

    private static int searchHigherOrEqual(int number) {
        int left = 0;
        int right = array.length - 1;
        int potential = -1;
        boolean notFound = false;
        while (!notFound) {
            if (left > right) {
                return potential;
            }
            int pivot = (left + right) / 2;
            if (array[pivot] < number) {

                left = pivot + 1;
            }
            else {
                right = pivot - 1;
                potential = pivot;
            }
        }
        return -1;
    }

    private static int searchLowerOrEqual(int number) {
        int left = 0;
        int right = array.length - 1;
        int potential = -1;
        boolean notFound = false;
        while (!notFound) {
            if (left > right) {
                return potential;
            }
            int pivot = (left + right) / 2;
            if (array[pivot] <= number) {
                potential = pivot;
                left = pivot + 1;
            }
            else
                right = pivot - 1;
        }
        return -1;
    }

    private static int searchMaxIndex(int number) {
        int left = 0;
        int right = array.length - 1;
        boolean notFound = false;
        while (!notFound) {
            if (left > right)
                return -1;
            int pivot = (left + right) / 2;
            if (array[pivot] < number)
                left = pivot + 1;
            else if (array[pivot] == number)
                return pivot;
            else
                right = pivot - 1;
        }
        return -1;
    }
}