Click here to Skip to main content
15,899,754 members

Comments by subhash nmkns (Top 2 by date)

subhash nmkns 3-Dec-21 3:10am View    
i have pasted the code.
subhash nmkns 3-Dec-21 3:08am View    
Deleted
So this is what i have tried using stack. But this is not efficient solution. How could i improve this solution or i have to change approach to the solution like instead of stack , any other data structure or algorithm ?


public static void stackApproach(long[] input) {
Stack<long> stack = new Stack<>();
stack.push(input[0]);
int i = 1, len = input.length;
while( i < len) {
long temp = input[i];
while ( !stack.isEmpty()) {
long isCoPrimes = coPrimes(stack.peek(), temp);
if(isCoPrimes != 1) {
long lcm = lcm(stack.peek(), temp);
stack.pop();
if(stack.isEmpty()){
stack.push(lcm);
break;
}
temp = lcm;
}else{
stack.push(input[i]);
break;
}
}
i++;
}
System.out.println(stack.toString());
}