Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Given an array of numbers A. Perform below operations

1.Choose the adjacent numbers A[i] and A[i + 1] such that they are not co prime.
2.Delete those two numbers and replace with the LCM of two numbers.

Example :: [2, 3 , 4 , 6, 7] output = [12,7]

--> [2, 3, 4, 6, 7] ; choosen numbers(4, 6) is replaced with the lcm of two numbers - 12 So array becomes [2, 3,12,7]<br/>
--> [2,3,12,7] ; ChoosenNumbers(3,12) so array becomes [2, 12, 7]<br/>
--> [2,12,7] ; choosenNumbers(2,12) so array becomes [12,7]<br/>
--> [12, 7] Since no adjacent numbers are co prime this is the solution


What I have tried:

I am beginner in coding. I have tried various methods to solve using java but faced problem during deletion of elements.
Please suggest efficient method and provide efficient code for this problem

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());
   }
Posted
Updated 5-Dec-21 13:53pm
v3
Comments
Patrice T 3-Dec-21 2:20am    
Show your work so far.
merano99 7-Dec-21 18:04pm    
As I have already written in my solution, this is not C ++ code and that it is rather unsolvable with a stack has already been written.
Please comment on the existing solutions and, if necessary, evaluate.

Quote:
Please ... and provide efficient code for this problem

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
The algorithm requires you to access elements at random positions. This make both stack and queue (in case you considered that) a bad choice, because they only allow you to access elements from the end(s)!

Furthermore, the algorithm requires you to remove elements in the middle of the list and insert another element. This is easier to accomplish with std::list than with std::vector. Therefore you should use std::list - cppreference.com[^] , not stack!

Moving around a list, removing elements or inserting new ones isn't hard. Just watch out for the position where you're inserting or erasing.

You could implement this with std::vector too: this container is a lot faster than std::list, but it can be a bit tricky to keep track of your position when inserting or erasing elements in the middle of the list.
 
Share this answer
 
Step one: make your code work, producing the correct output.
Step two: make, if posssible, your code more efficient.
If you need help on the above steps, please post here your code and ask for specific help..
 
Share this answer
 
Comments
subhash nmkns 3-Dec-21 3:10am    
i have pasted the code.
A C ++ solution is asked for here, but the code presented is something else.
It should look something like this:
C++
typedef std::vector<int> arrtyp;

int main()
{
	// Create a vector containing integers
	arrtyp v = { 2, 3, 4, 6, 7 };

	printout(v);

	for (long i=(long)v.size()-2; i>=0; i--) {
		long isCoPrimes = coPrimes(v.at(i), v.at(i+1));
		if (isCoPrimes != 1) {
			long l = lcm(v[i], v[i+1]);
			v.erase(v.begin() + i);
			v[i] = l;
			// printout(v);
		}
	}
	
	std::cout << "Solution: ";
	printout(v);

	return 0;
}

Any missing headers and functions would have to be added.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900