Skip to main content
deleted 4 characters in body
Source Link

Is it possible to get the sum of an array using divide and conquer? I've tried it, but I always miss some numbers, or I calculate a number twice.

int[] arr = new int[]{1,2,3,4,5};

    public int sum(int[] arr) {
        int begin = 0;
        int end = array.length - 1;
        int counter = 0;

        while (begin <= end) {
            int mid = (begin + end) / 2;
            counter += array[end]arr[end] + array[mid];arr[mid];
            end = mid - 1;
        }
        return counter;
    }

Is it possible to get the sum of an array using divide and conquer? I've tried it, but I always miss some numbers, or I calculate a number twice.

int[] arr = new int[]{1,2,3,4,5};

    public int sum(int[] arr) {
        int begin = 0;
        int end = array.length - 1;
        int counter = 0;

        while (begin <= end) {
            int mid = (begin + end) / 2;
            counter += array[end] + array[mid];
            end = mid - 1;
        }
        return counter;
    }

Is it possible to get the sum of an array using divide and conquer? I've tried it, but I always miss some numbers, or I calculate a number twice.

int[] arr = new int[]{1,2,3,4,5};

    public int sum(int[] arr) {
        int begin = 0;
        int end = array.length - 1;
        int counter = 0;

        while (begin <= end) {
            int mid = (begin + end) / 2;
            counter += arr[end] + arr[mid];
            end = mid - 1;
        }
        return counter;
    }
Source Link

Divide and conquer sum of array iterative

Is it possible to get the sum of an array using divide and conquer? I've tried it, but I always miss some numbers, or I calculate a number twice.

int[] arr = new int[]{1,2,3,4,5};

    public int sum(int[] arr) {
        int begin = 0;
        int end = array.length - 1;
        int counter = 0;

        while (begin <= end) {
            int mid = (begin + end) / 2;
            counter += array[end] + array[mid];
            end = mid - 1;
        }
        return counter;
    }