This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Permutations

 
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi frnds i need help for the below problem

consider a permutation of first 'N' natural numbers 'good' if it doesn't have 'x' and 'x+1' appearing consecutively,
where (1 <= x <= N).
for example, for N=3, all 'good' permutations are:
1. {1,3,2}
2. {2,1,3}
3. {3,2,1}
Write a java program that takes an input 'N' and displays the number of possible 'good' permutations. So, for input
of 3 for 'n' above, we would print '3' as the output.
 
Ranch Hand
Posts: 34
Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ashok mandala wrote:hi frnds i need help for the below problem

consider a permutation of first 'N' natural numbers 'good' if it doesn't have 'x' and 'x+1' appearing consecutively,
where (1 <= x <= N).
for example, for N=3, all 'good' permutations are:
1. {1,3,2}
2. {2,1,3}
3. {3,2,1}
Write a java program that takes an input 'N' and displays the number of possible 'good' permutations. So, for input
of 3 for 'n' above, we would print '3' as the output.



why not {2,3,1} is good permutation?
 
ashok mandala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we are considering that the permutation will be good if it doesn't have consecutive numbers........
 
Suresh Sajja
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh! i misread you original post.

I would approach this problem in two steps,

1. To come up with a algorithm that generate all the permutations of N numbers
2. filter the list to hold only good permutations



 
Marshal
Posts: 75631
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Read Suresh Sajja’s post carefully; it contains good advice. I would suggest you filter each permutation before adding it to your list, however.
Please don’t use {} around your numbers, because that denotes a set. Use [] for a sequence. So [1, 2, 3] is a “bad” sequence and [3, 2, 1] is a “good” sequence.
 
ashok mandala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got the logic for first step...
but dont know how to filter.....
help me out..........
 
lowercase baba
Posts: 13048
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ashok mandala wrote:i got the logic for first step...
but dont know how to filter.....
help me out..........


How do YOU, personally, decide if a sequence is valid or not? Let's say I gave you this:

[2,5,3,4,1]

Is that 'good', or 'bad'? How do you know? can you write down the steps that an 8-year old child could follow to determine this?

The most important part of writing code does not involve your computer, but your brain + pencil + paper.
 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
according to this question a good permutation is,that doesn't have consecutive increasing numbers side by side...
for input 3 we can only have {[1,3,2],[3,1,2],[2,1,3],[3,2,1]} but we cant have {[1,2,3],[2,3,1],[]} i.e we can't have x,x+1 type arrangements....
 
Campbell Ritchie
Marshal
Posts: 75631
354
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch , Mohammed Irfan.
 
mohammed irfan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnq Campbell Ritchie...!!!
 
I am going to test your electrical conductivity with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic