Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
Colombo
Red Hat Certified System Administrator (RHCSA) - RHEL 10
Sanjeewani95
Updated:
Friday at 7:43 PM
NURSING , CAREGIVER , HOTEL & BEAUTY COURSES
IVA Para Medical Campus
Updated:
Thursday at 9:24 AM
Handmade Character Soft Toys Peppa Pig Family
anil1961
Updated:
Wednesday at 9:58 PM
Ad icon
Video Content Creator
pramukag
Updated:
Jun 28, 2026
Ad icon
QA Engineer Intern
pramukag
Updated:
Jun 28, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
Education
Thread for Programming and Realeted Topics
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="rclakmal" data-source="post: 5976096" data-attributes="member: 98858"><p>[code]</p><p>/*</p><p> * To change this template, choose Tools | Templates</p><p> * and open the template in the editor.</p><p> */</p><p>package example1;</p><p></p><p>import java.util.ArrayList;</p><p>import java.util.HashMap;</p><p>import java.util.Random;</p><p>import java.util.Scanner;</p><p>import java.util.TreeSet;</p><p></p><p>/**</p><p> *</p><p> * @author Lakmal</p><p> */</p><p>class Permutations {</p><p></p><p> public static void main(String str[]) {</p><p> Scanner input = new Scanner(System.in);</p><p> Random ran = new Random();</p><p> String s = "";</p><p> ArrayList counted = new ArrayList();</p><p> TreeSet ts = new TreeSet();</p><p> ArrayList al = new ArrayList();</p><p> HashMap hm = new HashMap();</p><p></p><p> System.out.println("Number of input digits");</p><p> int n = input.nextInt();</p><p> int numbers[] = new int[n];</p><p> System.out.println("Enter your numbers:");</p><p></p><p> for (int i = 0; i < n; i++) {</p><p> numbers[i] = input.nextInt();</p><p> int count = 1;</p><p> for (int j = 0; j < al.size(); j++) {</p><p> if (numbers[i] == (Integer) al.get(j)) {</p><p> count++;</p><p> }</p><p> }</p><p> hm.put(numbers[i], count);</p><p> al.add(numbers[i]);</p><p></p><p> }</p><p> for (int i = 0; i < numbers.length; i++) {</p><p> boolean isCounted = false;</p><p> for (int k = 0; k < counted.size(); k++) {</p><p> if (numbers[i] == (Integer) counted.get(k)) {</p><p> isCounted = true;</p><p> }</p><p> }</p><p> if (((Integer) hm.get(numbers[i]) != 1) && (isCounted == false)) {</p><p> n = n - (Integer) hm.get(numbers[i]) + 1;</p><p> }</p><p> counted.add(numbers[i]);</p><p> }</p><p> System.out.println("Enter number of Places");</p><p> int noOfPlaces = input.nextInt();</p><p></p><p> do {</p><p> s = "";</p><p> for (int k = 1; k <= noOfPlaces; k++) {</p><p> s += "" + numbers[ran.nextInt(n)];</p><p> }</p><p> ts.add(s);</p><p> } while (ts.size() < (int) Math.pow(n, noOfPlaces));</p><p> System.out.println("Number Of Permutations=" + ts.size());</p><p> System.out.println("Permutations");</p><p> System.out.println(ts);</p><p></p><p> }</p><p>}</p><p></p><p>[/code]</p><p></p><p>Hey here is my answer !!! If u have the JVM please run it and see !!!!</p><p>I dont think this is the perfect solution ! Because it will get slow when number of inputs are increased.</p><p></p><p>Example if u have 3 digits [0,1,2]</p><p>and no of places are 3 it will be really fast !!! But when no of places and no of digits get increased (about 6 ,7) it will get slower about 30-45 seconds </p><p></p><p></p><p>Will try a good algorithem than this ,but dont have much time now !!! And specially if u r developing plz remember when there are digits list like [1 0 0]</p><p>( a number is repeated) then u have to consider about that also !! </p><p></p><p>Good problem ,,,,, thanks for it !!!!!!</p></blockquote><p></p>
[QUOTE="rclakmal, post: 5976096, member: 98858"] [code] /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package example1; import java.util.ArrayList; import java.util.HashMap; import java.util.Random; import java.util.Scanner; import java.util.TreeSet; /** * * @author Lakmal */ class Permutations { public static void main(String str[]) { Scanner input = new Scanner(System.in); Random ran = new Random(); String s = ""; ArrayList counted = new ArrayList(); TreeSet ts = new TreeSet(); ArrayList al = new ArrayList(); HashMap hm = new HashMap(); System.out.println("Number of input digits"); int n = input.nextInt(); int numbers[] = new int[n]; System.out.println("Enter your numbers:"); for (int i = 0; i < n; i++) { numbers[i] = input.nextInt(); int count = 1; for (int j = 0; j < al.size(); j++) { if (numbers[i] == (Integer) al.get(j)) { count++; } } hm.put(numbers[i], count); al.add(numbers[i]); } for (int i = 0; i < numbers.length; i++) { boolean isCounted = false; for (int k = 0; k < counted.size(); k++) { if (numbers[i] == (Integer) counted.get(k)) { isCounted = true; } } if (((Integer) hm.get(numbers[i]) != 1) && (isCounted == false)) { n = n - (Integer) hm.get(numbers[i]) + 1; } counted.add(numbers[i]); } System.out.println("Enter number of Places"); int noOfPlaces = input.nextInt(); do { s = ""; for (int k = 1; k <= noOfPlaces; k++) { s += "" + numbers[ran.nextInt(n)]; } ts.add(s); } while (ts.size() < (int) Math.pow(n, noOfPlaces)); System.out.println("Number Of Permutations=" + ts.size()); System.out.println("Permutations"); System.out.println(ts); } } [/code] Hey here is my answer !!! If u have the JVM please run it and see !!!! I dont think this is the perfect solution ! Because it will get slow when number of inputs are increased. Example if u have 3 digits [0,1,2] and no of places are 3 it will be really fast !!! But when no of places and no of digits get increased (about 6 ,7) it will get slower about 30-45 seconds Will try a good algorithem than this ,but dont have much time now !!! And specially if u r developing plz remember when there are digits list like [1 0 0] ( a number is repeated) then u have to consider about that also !! Good problem ,,,,, thanks for it !!!!!! [/QUOTE]
Insert quotes…
Verification
Awruddata maasa keeyada?
Post reply
Top
Bottom