Monday 17 August 2015

Magnificent Fountains

Standard
I and my flatmate ,Sayan, went to see the magnificient fountains in the Jubilee park on 3rd March.It was the eve of the 184rd Bithday of the late Mr. J.N.Tata and the fountains were set to blow at regular intervals. I sat down praising the scenic beauty of the fountains.But Sayan, who is a bit wierd, came up with a challenge.
He asked me, "Given that all fountains start off at the same time ,can you tell me when will all the fountains fire simultaneously again". Now, I being a bit dumbo, am asking for your help.You will be given the intervals at which each of the fountain fires.And all you need to do is tell me precisely at what time the fountains will fire simultaneously again.
For example, say there are 4 fountains with firing intervals of 2, 3, 1, and 1 respectively, then if they fire all together at t=0, they will fire together again at t=6.
INPUT
First line contains the T , the number of test cases. T test cases follow. Each of the test case contains 2 lines of input. The first line contains a single number n , denoting the number of fountains. The next line contains n space separated numbers denoting the interval time of the n fountains.
OUTPUT
Output T lines each containing only one number - the answer to the question (modulo 1000000007).
CONSTRAINTS
1<=T<=10
1<=n<=1000
1<=Time intervals<=10000

Sample Input
1
4
2 4 6 8
Sample Output
24

Time Limit: 3 sec(s) for all input files combined.
Memory Limit: 256 MB
Source Limit: 1024 KB 
 
Code: 
 
/* IMPORTANT: class must not be public. */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class TestClass {
    public static void main(String args[] ) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int numOfTests = Integer.parseInt(br.readLine());
        String[] strArr;
        long lcm;
        long divisor = 1000000007;
        List<Integer> primeList = new ArrayList<Integer>();
        Set<Integer> inputSet = new HashSet<Integer>();
        List<Integer> inputList = new ArrayList<Integer>();
        List<Integer> inputList2 = new ArrayList<Integer>();
        List<Integer> indicesList = new ArrayList<Integer>();
        boolean isDivisible = false;
        int inputNum, primeNum2, num;

        primeList.add(2);
        for(int i=3; i<10000; i+=2){
            for(int primeNum : primeList){
                if(i%primeNum == 0){
                    break;
                }else if(primeNum*primeNum > i){
                    primeList.add(i);
                    break;
                }
            }
        }
       
        for(int i=0; i<numOfTests; i++){
            br.readLine();
            strArr = br.readLine().split(" ");
           
            for(int j=0; j<strArr.length; j++){
                inputNum = Integer.parseInt(strArr[j]);
                if(inputNum != 1)
                    inputSet.add(inputNum);
            }
           
            inputList.addAll(inputSet);
            lcm = 1;
           
            for(int k=0; k<primeList.size(); k++){
                primeNum2 = primeList.get(k);
                isDivisible = false;
                for(int l=0; l<inputList.size(); l++){
                    num = inputList.get(l);
                    if(num % primeNum2 == 0){
                        isDivisible = true;
                        indicesList.add(l);
                        if(num/primeNum2 != 1)
                            inputList2.add(num/primeNum2);
                    }
                }
                if(isDivisible){
                    lcm = (lcm*primeNum2)%divisor;
                   
                    if(inputList.size()-indicesList.size() == 0 && inputList2.size() == 0){
                        break;
                    }else{
                        for(int l=indicesList.size()-1; l>=0; l--){
                            inputList.remove((int)indicesList.get(l));
                        }
                        inputList.addAll(inputList2);
                        inputList2.clear();
                        indicesList.clear();
                        k--;
                    }
                }
                if(inputList.size() == 1){
                    lcm = (lcm*inputList.get(0))%divisor;
                    break;
                }
            }
            System.out.println(lcm);
        }
    }
}
 

0 comments:

Post a Comment