Java Saved Program: beautiful-palace

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


import java.io.*;
import java.util.*;

class Solution {
    public int[] solution(int N,int L, int[] A, int[] V){
    double temp;
    double max=0.0;
    int P = 0, Q = 1;
    
    for(int i=1;i<=N;i++)
        {
            if(V[i]>0)
            {
                temp=(double)(((L-A[i])*1.0))/(Math.abs(V[i]));
                if(temp>max)
                {
                    max=temp;
                    P = L-A[i];
                    Q = Math.abs(V[i]);
                }
            }
            else
            {
                temp=(double)(((A[i])*1.0))/(Math.abs(V[i]));
                if(temp>=max)
                {
                    max=temp;
                    P = A[i];
                    Q = Math.abs(V[i]);
                }
            }
        }
 
        if(P!=0) {
            return new int[]{P/gcd(P,Q),Q/gcd(P,Q)};     
        }
        else {
        return new int[]{0,1};     
        }
        
    }
    static int gcd ( int a, int b ) {
      if ( a==0 ) return b;
      return gcd ( b%a, a );
    }
}

class Script {
    public static void main(String[] args) throws InterruptedException {
            Scanner sc = new Scanner(System.in);
            int N = sc.nextInt();
            int L = sc.nextInt();
            int[] A = new int[N];
            int[] V = new int[N];
            for(int i=0; i< N; i++) {
                A[i]= sc.nextInt();
            }
            for(int i=0; i< N; i++) {
                V[i]= sc.nextInt();
            }
            Solution s = new Solution();
            int[] result = s.solution(N, L, A, V);
            if(result== null){
                System.out.println("returned array is null");
            }else{
                for(int i=0; i< result.length; i++) {
                    System.out.print(result[i] + " ");
                }
            }
        }
    }
Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs