2

I'm Developing a Code for java to Fold the element inside an array with example

A[0] = 2, A[1] = 7, A[2] = 9, A[3] = 7

Then Fold it with this format

A[0] = (A[0] + A[3]) mod 10 = 9

A[1] = (A[1] + A[2]) mod 10 = 6

And Fold it again until single

A[0] = (A[0] + A[1]) mod 10 = 5

Below are incomplete code:

import java.util.ArrayList;
import java.util.List;
import java.lang.Math;

public class ArrayFolder {

    public static void main(String[] args) {

        //ArrayList
        int[] A = {
            2,
            7,
            9,
            7
        };

        //To define a new Array later
        List<Integer> intList = new ArrayList<Integer>();

        //To print the ArrayList A
        for (int x = 0; x < A.length; x++) {
            System.out.println("A[" + x + "]= " + A[x]);
        }

        //Function to fold the ArrayList into half
        int res = 0;
        int result = 0;

        //if A.length is Even
        if (A.length % 2 == 0) {
            for (int i = 0; i < A.length / 2; i++) {
                res = A[i] + A[A.length - 1 - i];
                result = res % 10;
                intList.add(result);
            }
        }

        //if A.length is Odd
        else {
            for (int i = 0; i < A.length / 2; i++) {
                res = A[i] + A[A.length - 1 - i];
                result = res % 10;
                intList.add(result);
            }
            result = A[A.length / 2];
            intList.add(result);
        }

        //add the int to New ArrayList
        Integer[] intArray = new Integer[intList.size()];
        intArray = intList.toArray(intArray);
        System.out.println("\nNew Array ");

        for (Integer s: intArray) {
            System.out.println(s);
        }

    }
}

Compiled result

A[0]= 2
A[1]= 7
A[2]= 9
A[3]= 7

New Array
9
6

I couldn't find an efficient way to keep looping the function, so the code will stop folding when a single Integer achieved.

My question is, is there any efficient way to loop the process so later It can work with a larger element of Array?.

Kindly provide the logic or the code so I can continue with my code.

Many Thanks

akuzminykh
  • 4,522
  • 4
  • 15
  • 36
Al_Muzakier
  • 49
  • 1
  • 9

2 Answers2

2

You could put your folding algorithm into a separate method that returns you the folded array and call this method until you achieve a single Integer, for example like this:

import java.util.ArrayList;
import java.util.List;

public class ArrayFolder {

    public static void main(String[] args) {

        //ArrayList
        Integer[] A = {
            2,
            7,
            9,
            7
        };

        while (A.length > 1) {
            A = fold(A);
        }

        System.out.println("A[0]= " + A[0]);

    }

    private static Integer[] fold(Integer[] A) {
        List<Integer> intList = new ArrayList<>();
        //To print the ArrayList A
        for (int x = 0; x < A.length; x++) {
            System.out.println("A[" + x + "]= " + A[x]);
        }

        //Loop to fold the ArrayList into half
        for (int i = 0; i < A.length / 2; i++) {
            int res = A[i] + A[A.length - 1 - i];
            int result = res % 10;
            intList.add(result);
        }
        
        //if A.length is odd
        if (A.length % 2 != 0) {
            intList.add(A[A.length / 2]);
        }
        System.out.println("\n");
        
        return intList.toArray(new Integer[intList.size()]);
    }
}

You could also use a recursive method, i.e. the folding method will call itself for further folding until it reaches one Integer.

import java.util.ArrayList;
import java.util.List;

public class ArrayFolder {

    public static void main(String[] args) {

        //ArrayList
        final Integer[] A = {
            2,
            7,
            9,
            7
        };

        fold(A);
    }

    private static void fold(Integer[] A) {
        if (A.length > 0) {
            List<Integer> intList = new ArrayList<>();
            //To print the ArrayList A
            for (int x = 0; x < A.length; x++) {
                System.out.println("A[" + x + "]= " + A[x]);
            }

            //Loop to fold the ArrayList into half
            for (int i = 0; i < A.length / 2; i++) {
                int res = A[i] + A[A.length - 1 - i];
                int result = res % 10;
                intList.add(result);
            }

            //if A.length is odd
            if (A.length > 1 && A.length % 2 != 0) {
                intList.add(A[A.length / 2]);
            }
            System.out.println("\n");
        
            fold(intList.toArray(new Integer[intList.size()]));
        }
    }
}
Mario Varchmin
  • 3,704
  • 4
  • 18
  • 33
1

Folding in same array:

public static void main(String[] args) throws Exception {
    int[] A = {2,7,9,7};
    System.out.println("Input: " + Arrays.toString(A));

    int length = A.length;
    int mid = mid(length);
    while(mid > 0) {  //fold till only one left to fold
        int i = 0;
        for (; i <mid ; i++) { //single fold
            int end = length-i-1;
            if(i!=end) {
                A[i] = (A[i] + A[end])%10;
            }
        }
        System.out.println(Arrays.toString(Arrays.copyOf(A, mid)));
        length = mid;
        mid = mid(i);
    }
    System.out.println("Output: " + A[0]);
}
static int mid(int length) {
    if(length == 1) return 0;
    return (int)Math.ceil(length/2.0);
}

Output:

Input: [2, 7, 9, 7]
[9, 6]
[5]
Output: 5

For odd numbers:

Input: [2, 7, 3, 9, 7]
[9, 6, 3]
[2, 6]
[8]
Output: 8
the Hutt
  • 16,980
  • 2
  • 14
  • 44