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.
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]+" ");
}
}
Comments
Leave a comment