Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7030333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:39:24+00:00 2026-05-28T00:39:24+00:00

So this problem is a little bit complicated to understand what I’m trying to

  • 0

So this problem is a little bit complicated to understand what I’m trying to do.

Basically, I am trying to randomly generate 3 vectors, all of size 11.

The first vector must have a 1 at position 0 with the next 5 positions being 0 (e.g. 100000) while the next five digits can be either 0 1 or 2, however there can only be one zero used in the last 5 digits, hence 10000012101 would be valid but 10000012001 wouldn’t.

The same is applicable to the second and third vector however the first 1 will move a place for the second and the third (010000xxxxx for the second, and 001000xxxxx for the third).

There are more conditions that have to be satisfied. Each vector must differ from each other in at least 5 positions (10000011210 would differ from 01000022100 in 5 positions which would work).

However, there is also a final constraint which states that if you add the vectors modulo 3, then the result of adding these two must have at least 5 NON zero values in the vector.

I have went about this by using arraylists. As I know the first 6 elements of each arraylist for each vector I manually put these in, and for the next 5 elements I randomly assign these, if there is more than one 0 in the last five digits, i call the method again recursively.

The problem I have with this program is that when I try to run my code it comes up with a
Exception in thread “main” java.lang.StackOverflowError
at java.util.ArrayList.get(Unknown Source)

I think it’s because it’s continuously trying to loop and therefore crashing but I’m not sure. See below for the code.

import java.util.ArrayList;

/**
 * The purpose of this class is to be able to capture different ways 
 * of generating six vectors that will produce a collection of 729 
 * vectors that guarantee 9 out of 11 correct. 
 */

public class GenerateVectors {

    static ArrayList<Integer> firstVector = new ArrayList<Integer>();
    static ArrayList<Integer> secondVector = new ArrayList<Integer>();
    static ArrayList<Integer> thirdVector = new ArrayList<Integer>();
    static ArrayList<Integer> sumOfXandY = new ArrayList<Integer>();

    //Creates the first vectors to ensure it starts with "1,0,0,0,0,0" 
    //and has at most one more zero in the last 5 digits
    public void createFirstVector(){

        int[] fir stVector1 = {1,0,0,0,0,0};
        for (int i=0; i<firstVector1.length; i++) {
            firstVector.add(firstVector1[i]);
        }
        for(int i = 0; i < 5; i++){
            int x = (int) (Math.random()*3);
            firstVector.add(x);
    }
    int j = 0;
    for(int i = 6; i<firstVector.size(); i++){
        if(firstVector.get(i).equals(0)){
            j++;
        }
    }
    if(j>1){
        OneZeroInLastFive(firstVector);
    }


    int[] sum = {0,0,0,0,0,0,0,0,0,0,0};
        for (int i=0; i<sum.length; i++) {
            sumOfXandY.add(sum[i]);
        }
    }

    //Edits the vector if there is more than 0 in the last five digits
    public void OneZeroInLastFive(ArrayList<Integer> x){
        int j = 0;
        for(int i = 6; i<x.size(); i++){
            if(x.get(i).equals(0)){
                j++;
            }
        }
        if(j>1){
            x.set(6, (int) (Math.random()*3));
            x.set(7, (int) (Math.random()*3));
            x.set(8, (int) (Math.random()*3));
            x.set(9, (int) (Math.random()*3));
            x.set(10, (int) (Math.random()*3));
            j = 0;
            OneZeroInLastFive(x);
        }
    }


    //Creates the second vector with the last 5 digits random
    public void createSecondVector(){
        int[] secondVector1 = {0,1,0,0,0,0};
        for (int i=0; i<secondVector1.length; i++) {
            secondVector.add(secondVector1[i]);
        }
        for(int i = 0; i < 5; i++){
            int x = (int) (Math.random()*3);
            secondVector.add(x);
        }
    }

    //Creates the third vector with the last 5 digits random
    public void createThirdVector(){
        int[] thirdVector1 = {0,0,1,0,0,0};
        for (int i=0; i<thirdVector1.length; i++) {
            thirdVector.add(thirdVector1[i]);
        }
        for(int i = 0; i < 5; i++){
            int x = (int) (Math.random()*3);
            thirdVector.add(x);
        }
    }

    /**
     * Will edit the second vector to ensure the following conditions are satisfied 
     * - The sum of x and y modulo 3 has at least 5 NON zeros
     * - x and y must DIFFER in at least 5 places 
     * - There is only one zero within the last 5 digits 
     *
     */
    public void checkVectors(ArrayList<Integer> x, ArrayList<Integer> y){
        int k = 0;
        int m = 0;
        for(int j = 0; j < x.size(); j++){
            if(x.get(j).equals(y.get(j))){
                ;
            }
            else{
                k++;
            }   
        }
        for(int i = 6; i<y.size(); i++){
            if(y.get(i).equals(0)){
                m++;
            }
        }
        if((k>4 && m<1)&& checkNonZeros(x,y)){

            System.out.println("Conditions met");
        }
        else{
            y.set(6, (int) (Math.random()*3));
            y.set(7, (int) (Math.random()*3));
            y.set(8, (int) (Math.random()*3));
            y.set(9, (int) (Math.random()*3));
            y.set(10, (int) (Math.random()*3));
            k = 0;
            m = 0;
            checkVectors(x,y);
        }
    }


    public ArrayList<Integer> addTwoVectors(ArrayList<Integer> x, ArrayList<Integer> y, ArrayList<Integer> z){
        for(int i = 0; i<x.size(); i++){
            int j = x.get(i);
            int k = y.get(i);
            z.set(i, ((j+k)%3));
        }
        return z;
    }


    public boolean checkNonZeros(ArrayList<Integer> x, ArrayList<Integer> y){
        addTwoVectors(x,y, sumOfXandY);
        int j = 0;
        for(int i = 0; i<firstVector.size(); i++){
            if(sumOfXandY.get(i).equals(0)){
                ;
            }
            else{
                j++;
            }
        }
        if(j<5){
            return false;
        }
        else {
            return true;
        }
    }

    public static void main(String[] args){
        GenerateVectors g = new GenerateVectors();
        g.createFirstVector();
        g.createSecondVector();
        g.createThirdVector();

        g.checkVectors(firstVector,secondVector);
        g.checkVectors(secondVector,thirdVector);

        System.out.println(firstVector);
        System.out.println(secondVector);
        System.out.println(thirdVector + "\n");

        System.out.println(g.checkNonZeros(firstVector, secondVector));
        System.out.println(g.checkNonZeros(secondVector,thirdVector));
        System.out.println(sumOfXandY);
    }

}

Any help would be much appreciated!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:39:25+00:00Added an answer on May 28, 2026 at 12:39 am

    The problem is that you have methods that recursively call themselves in order to ‘redo’, which may happen many times before you get a success. This is fine in languages like scheme or ml which do proper tail recursion, but java does not, so you get stack overflows.

    In order to fix this you need to manually convert the recursive code into a loop. Code that looks like:

    method(arg1, arg2) {
        Code_block_1;
        if (test) {
            Code_block_2;
        } else {
            Code_block_3;
            method(newarg1, newarg2);
        }
    }
    

    needs to become something like:

    method(arg1, arg2) {
        Code_block_1;
        while(!test) {
            Code_block_3;
            arg1 = newarg1;
            arg2 = newarg2;
            Code_block_1;
        }
        Code_block_2;
    }
    

    You can then refactor stuff to get rid of/merge the duplicated code if you wish.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, this problem is a little complicated, so bear with me. I have a
Okay i'm a little bit stuck on how to solve this problem. When a
I want to do THIS , just a little bit more complicated: Lets say,
Okay, here's my problem. Please forgive me as it's a little bit complicated. I'm
I have this little problem, that I cannot figure out which arguments to pass
I have this little problem. My client wanted two distinct swf on a web
Okay, it took me a little while to narrow down this problem, but it
I'm having a little problem with the following: When I execute this line: echo
This problem involved me not knowing enough of C++. I am trying to access
For the first time, I am asking a little bit of help over here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.