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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:15:01+00:00 2026-06-10T15:15:01+00:00

I’m a high school student of grade 10 trying to work through some problems

  • 0

I’m a high school student of grade 10 trying to work through some problems in a Data Structures and Algorithms book on Java.

One of the questions is to print all permutations of a string.

class C14
{
public static void main(char a[])
{
    // char[] a = {'c','a','r','b','o','n'};
    int c=0,w=0;
    for(int q = 0;q<a.length;q++) 
    {
        for(int i=0;i<a.length;i++)
        {
            for(int j=1;j<a.length;j++)
            {
                for(int k=1;k<a.length-1;k++) 
                {
                    for(int z=0;z<a.length;z++)
                    {
                        System.out.print(a[z]);
                        c++;
                    }
                    w++;
                    System.out.println();
                    char p=a[k+1];
                    a[k+1]=a[k];
                    a[k]=p;
                }
                System.out.println();
            }
            System.out.println();
            char x=a[0];
            a[0]=a[1];
            a[1]=x;
        }
      }
    System.out.println(" Character count = " + c);
    System.out.println(" Word count = " + w);
    }
}

This is my attempt. The book asks me to do it for the characters ‘c’,’a’,’r’,’b’,’o’,’n’.
My solution does just that, but when I try to use 3 or 4 letter words, it gives me repetitions. If I remove the outermost loop and try to print it, it works for 3 and 4 letter words but not for 5+ letter words.

I’ll be glad to clarify my reasoning for it, I know it’s not the most efficient, but do bear in mind the fact that I’m only in grade 10 and this is what came to my mind first.

Can someone please help me out, or at least hint at what’s wrong?
Please don’t advise a recursive solution, because I want to work through it iteratively first.
Thanks,
Sumit.

  • 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-06-10T15:15:03+00:00Added an answer on June 10, 2026 at 3:15 pm

    Permutation with repetitions

    When you have n things to choose from … you have n choices each time!

    When choosing r of them, the permutations are:

    n × n × … (r times) = n^r

    I’m presenting 2 cases. First case is when the we know the size of n and r already, and its the easy one. The second when n and r are dynamic.

    //when n and r are known statically
    
    class Permutation
    {
        public static void main(String[] args)
        {
            char[] values = {'a', 'b', 'c', 'd'};
            int n = values.length;
            int r = 2; 
    
            int i = 0, j = 0;
            for(i=0; i<n; i++)
            {
                for(j=0; j<n; j++)
                {
                    System.out.println(values[j] + " " + values[i]);
                }
            }
        }
    }
    
    
    //when n and r are known only dynamically
    
    class Permutation
    {
        public static void main(String[] args)
        {
            char[] values = {'a', 'b', 'c', 'd'};
            int n = values.length;
            int r = 2; 
            int i[] = new int[r];
            int rc = 0;
            for(int j=0; j<Math.pow(n,r); j++)
            {
    
                rc=0;
                while(rc<r)
                {
                    System.out.print(values[i[rc]] + " ");
                    rc++;
                }
                System.out.println();
                rc = 0;
                while(rc<r)
                {
                    if(i[rc]<n-1)
                    {
                        i[rc]++;
                        break;
                    }
                    else
                    {
                        i[rc]=0;
                    }
                    rc++;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.