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 3442274
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:38:55+00:00 2026-05-18T08:38:55+00:00

I am trying to run my code so it prints cyclic permutations, though I

  • 0

I am trying to run my code so it prints cyclic permutations, though I can only get it to do the first one at the moment. It runs correctly up to the point which I have marked but I can’t see what is going wrong. I think it has no break in the while loop, but I’m not sure. Really could do with some help here.

package permutation;

public class Permutation {
static int DEFAULT = 100;

public static void main(String[] args) {
    int n = DEFAULT;
    if (args.length > 0)
        n = Integer.parseInt(args[0]);

    int[] OA = new int[n];
    for (int i = 0; i < n; i++)
        OA[i] = i + 1;

    System.out.println("The original array is:");
    for (int i = 0; i < OA.length; i++)
        System.out.print(OA[i] + " ");
    System.out.println();

    System.out.println("A permutation of the original array is:");
    OA = generateRandomPermutation(n);
    printArray(OA);
    printPemutation(OA);
}

static int[] generateRandomPermutation(int n)// (a)
{
    int[] A = new int[n];
    for (int i = 0; i < n; i++)
        A[i] = i + 1;

    for (int i = 0; i < n; i++) {
        int r = (int) (Math.random() * (n));
        int swap = A[r];
        A[r] = A[i];
        A[i] = swap;
    }
    return A;
}

static void printArray(int A[]) {
    for (int i = 0; i < A.length; i++)
        System.out.print(A[i] + " ");
    System.out.println();
}

static void printPemutation(int p[])// (b)
{
    System.out
            .println("The permutation is represented by the cyclic notation:");
    int[] B = new int[p.length];
    int m = 0;
    while (m < p.length)// this is the point at which my code screws up
    {
        if (!check(B, m)) {
            B = parenthesis(p, m);
            printParenthesis(B);
            m++;
        } else
            m++;
    }// if not there are then repeat
}

static int[] parenthesis(int p[], int i) {
    int[] B = new int[p.length];
    for (int a = p[i], j = 0; a != B[0]; a = p[a - 1], j++) {
        B[j] = a;
    }
    return B;
}

static void printParenthesis(int B[]) {
    System.out.print("( ");
    for (int i = 0; i < B.length && B[i] != 0; i++)
        System.out.print(B[i] + " ");
    System.out.print(")");
}

static boolean check(int B[], int m) {
    int i = 0;
    boolean a = false;
    while (i < B.length || !a) {
        if ((ispresent(m, B, i))){
            a = true;
            break;
        }
        else
            i++;
    }
    return a;
}

static boolean ispresent(int m, int B[], int i) {
    return m == B[i] && m < B.length;
}
}
  • 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-18T08:38:55+00:00Added an answer on May 18, 2026 at 8:38 am

    Among others you should check p[m] in check(B, p[m]) instead of m:

    in static void printPemutation(int p[]):

    while (m < p.length){
        if (!check(B, p[m])) {
             B = parenthesis(p, m);
             printParenthesis(B);
        }
        m++;
    }
    

    then

    static boolean check(int B[], int m) {
        int i = 0;
        while (i < B.length) {
            if (m == B[i]) {
                return true;
            }
            i++;
        }
        return false;
    }
    

    this does somehow more what you want, but not always i fear…

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

Sidebar

Related Questions

When trying run this code: At first it prints Process some_id BEFORE enter for
I have the following code. I am trying to get reactor to run on
I am trying to run code on several cores (I tried both the snow
I'm trying to run this code but this error appear: Uncaught TypeError: string is
I am trying to run the code below but it keeps locking up my
I'm trying to run this code: let coins = [50, 25, 10, 5, 2,1]
I am trying to run this code: ItemTaxonomy iTaxonomy = from itemTaxonomy in connection.ItemTaxonomy
I am trying to run this code : exec myStoredProcedure Cast('c5b48202-36af-4597-9780-5366d4188f55' AS uniqueidentifier), 744,
I'm trying to run the code from here MDN: Generators and Iterators function fib()
when I trying to run following code. var result = from c in db.brand

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.