Mai intai trebuie sa te autentifici.

Cod sursa(job #2683345)

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

public class Main {

    private static int n;
    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();
            }
            n = scanner.nextInt(); // the number of commands

            int command;
            int number;
            for (int i = 0; i < n; i++) {
                command = scanner.nextInt();
                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 = -2;
        int pivot;
        while (left <= right) {
            pivot = (left + right) / 2;
            if (array[pivot] < number) {

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

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

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