Thread for Programming and Realeted Topics

rclakmal

Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    Hey Guys here a little bit hard(little bit) question !! I found it on Internet !! And I got the answer !! Give it a try .if u can find out the answer it will give u the same happiness that i got !! :D

    Post ur answer .....then i will give the correct one and my code
    Code:
    Find the largest product of five consecutive digits in the 1000-digit number.Below the number is given!
    
    73167176531330624919225119674426574742355349194934
    96983520312774506326239578318016984801869478851843
    85861560789112949495459501737958331952853208805511
    12540698747158523863050715693290963295227443043557
    66896648950445244523161731856403098711121722383113
    62229893423380308135336276614282806444486645238749
    30358907296290491560440772390713810515859307960866
    70172427121883998797908792274921901699720888093776
    65727333001053367881220235421809751254540594752243
    52584907711670556013604839586446706324415722155397
    53697817977846174064955149290862569321978468622482
    83972241375657056057490261407972968652414535100474
    82166370484403199890008895243450658541227588666881
    16427171479924442928230863465674813919123162824586
    17866458359124566529476545682848912883142607690042
    24219022671055626321111109370544217506941658960408
    07198403850962455444362981230987879927244284909188
    84580156166097919133875499200524063689912560717606
    05886116467109405077541002256983155200055935729725
    71636269561882670428252483600823257530420752963450

    try it !!!!!!!!:D :P
     

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    And also bro these are not application programmes which are meant to serve u on particular task !!! these are questions simply to encourage u on programming and to give some introductions to algorithms !! Okiiiiii Hope u got it !!! :)
     

    pjayampathi

    Well-known member
  • Jan 20, 2008
    6,253
    39
    48
    And also bro these are not application programmes which are meant to serve u on particular task !!! these are questions simply to encourage u on programming and to give some introductions to algorithms !! Okiiiiii Hope u got it !!! :)

    noo, I mean the code on the first page... not that algorithm...
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    Here's new problem, I think you all know permutations; I need an algorithm that populates an array with all the possible permutations with a given list.
    ex:
    inputs
    Code:
    list={0,1}
    places/length=3
    output
    Code:
    000 
    001
    010
    011
    100
    101
    110
    111

    Note that the output need not to be arranged
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    Hey Guys here a little bit hard(little bit) question !! I found it on Internet !! And I got the answer !! Give it a try .if u can find out the answer it will give u the same happiness that i got !! :D

    Post ur answer .....then i will give the correct one and my code
    Code:
    Find the largest product of five consecutive digits in the 1000-digit number.Below the number is given!
    
    73167176531330624919225119674426574742355349194934
    96983520312774506326239578318016984801869478851843
    85861560789112949495459501737958331952853208805511
    12540698747158523863050715693290963295227443043557
    66896648950445244523161731856403098711121722383113
    62229893423380308135336276614282806444486645238749
    30358907296290491560440772390713810515859307960866
    70172427121883998797908792274921901699720888093776
    65727333001053367881220235421809751254540594752243
    52584907711670556013604839586446706324415722155397
    53697817977846174064955149290862569321978468622482
    83972241375657056057490261407972968652414535100474
    82166370484403199890008895243450658541227588666881
    16427171479924442928230863465674813919123162824586
    17866458359124566529476545682848912883142607690042
    24219022671055626321111109370544217506941658960408
    07198403850962455444362981230987879927244284909188
    84580156166097919133875499200524063689912560717606
    05886116467109405077541002256983155200055935729725
    71636269561882670428252483600823257530420752963450

    try it !!!!!!!!:D :P

    47136154.jpg
     
    Last edited:

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    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);
    
        }
    }

    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 !!!!!!
     

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away

    Great machan !!! answer is Correct !!!! here is my answer

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package example1;
    
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    /**
     *
     * @author Lakmal
     */
    class Numbers {
    
        static String s = "";
        static String s1 = "";
    
        @SuppressWarnings("empty-statement")
        public static void main(String[] args) throws IOException {
            int MAX = 0;
            FileInputStream fis = new FileInputStream("lakmal.txt");
            DataInputStream dis = new DataInputStream(fis);
            BufferedReader bis = new BufferedReader(new InputStreamReader(dis));
            {
                while ((s = bis.readLine()) != null) {
                    s1 = s1 + s;
                }
                bis.close();
                System.out.println(s1);
                System.out.println(s1.charAt(0));
                System.out.println(s1.charAt(s1.length() - 1));
                for (int i = 0; i < s1.length() - 4; i++) {
                    int value = 1;
                    for (int j = i; j < i + 5; j++) {
                        value *= Integer.parseInt(Character.toString(s1.charAt(j)));
                    }
                    if (value > MAX) {
                        MAX = value;
                    }
                }
                System.out.println(MAX);
            }
    
        }
    }

    I read that number from a file .that is "lakmal.txt" .........oki !!
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    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);
    
        }
    }

    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 !!!!!!

    Mine takes 17.625 seconds when there are 6 digits :D lol it should be slow man! its not taking time to fill the memory its for the video buffer(console display) anyway good that you made the algorithm ! :D
    So far i don't handle duplicates :P, I'm trying to make an algorithm for non repeating digits (පුනරාවර්තනය රහිත) we'll see! :P i'm still thinking the algo will most probably be made tomorrow
    heres the pascal/delphi procedure
    Code:
    const ELEMENTS  = 5;           //element count -1
    const PLACES    = 5;           //place count  -1
    
    var
        lst:array [0..ELEMENTS] of integer;
        nlst:array of array [0..PLACES] of integer;
    
    procedure SetListWR();
    var i,j,LEN,k:integer;
    begin
        LEN:=Pow(ELEMENTS+1,PLACES+1);
        SetLength(nlst,LEN);
        k:=0;
        for j:=0 to PLACES do
            for i:=0 to LEN-1 do
            begin
                if ((i) mod pow(ELEMENTS+1,j)) = 0 then
                begin
                    if (k=ELEMENTS) then
                        k:=0
                    else
                        k:=k+1;
                end;
                nlst[i,j]:=lst[k];
            end;
        k:=0 ;
        for j:=0 to LEN -1 do
        begin
            for i:=0 to PLACES do
            begin
                write(nlst[j,i]);
            end;
            writeln('');
            k:=k+1;
        end;
        Writeln('Number of permutations : ',k);
    end;
     
    Last edited:

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    ok here's a challenge :D
    make an algorithm that will generate NON REPEATING permutations from a given list of integers. Please note that algorithms using a search and remove system(generating the repeating permutations first and removing the repeating ones) is NOT considered as a real answer.

    E.g:

    List : 1,2,3

    Output :
    123
    132
    231
    213
    312
    321

    PS: I'm still thinking of a way of my own, don't worry if this seems too hard this problem is a problem asked at a Google or MS(don't remember what company for sure) job interview. Njoy!
     
    Last edited:

    pjayampathi

    Well-known member
  • Jan 20, 2008
    6,253
    39
    48
    jeez .. dude ?. really is this a prob which is asked from MS and google.?.. my mind solved it.. but Im not feeling any kick to code.. let's see if someone hve solved it...