Permutations
ashok mandala
Greenhorn
Posts: 9
posted 4 years ago
 1
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.
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.
posted 4 years ago
why not {2,3,1} is good permutation?
 2
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?
~Suresh
ashok mandala
Greenhorn
Posts: 9
Campbell Ritchie
Marshal
Posts: 53182
124
posted 4 years ago
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.
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
posted 4 years ago
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 8year old child could follow to determine this?
The most important part of writing code does not involve your computer, but your brain + pencil + paper.
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 8year old child could follow to determine this?
The most important part of writing code does not involve your computer, but your brain + pencil + paper.
There are only two hard things in computer science: cache invalidation, naming things, and offbyone errors
mohammed irfan
Greenhorn
Posts: 2
Campbell Ritchie
Marshal
Posts: 53182
124
my overalls have superpowers  they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/ThreadBoostfeature
