Answer to Question #216837 in Java | JSP | JSF for harsh

Question #216837

 Assume that the coin information is given to you in the form of an ‘n’ element array A. A[i] is a number between 1 and n and A[i] = j means that the j’th smallest pancake is in position i from the top; in other words A[1] is the size of the top most coin (relative to the others) and

A[n] is the size of the bottommost coin. 

operation(A, n) should be assumed to be O(1) can be used directly in pseudo code, it flips first n coins.




1
Expert's answer
2021-07-13T17:20:15-0400
package com.company;

import java.util.Scanner;

public class Main {
   public void operation(int[]a,int n)
   {
       //flips n coins
       int l=0;
       int r=n-1;
       while(l<r)
       {
           int tm=a[l];
           a[l]=a[r];
           a[r]=tm;
           l++;
           r--;
       }

   }
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        System.out.println("Please enter size(n):");
        int n=cin.nextInt();
        int []a=new int[n];
        System.out.println("Please eneter numbers(information of coins)");
        for(int i=0;i<n;i++)
            a[i]=cin.nextInt();
        Main mn=new Main();
        mn.operation(a,n);//Flips coins
        //Output
        System.out.println("===========After opeation (A,n)");
        for(int i=0;i<n;i++)
            System.out.print(a[i]+" ");
    }


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS