## Infosys Interview Question

Software Developers**Country:**India

**Interview Type:**Written Test

import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;

public class Solution {

int n;

int input[];

int output[];

int count = 0;

public void get_input(){

Scanner sc = new Scanner(System.in);

n = sc.nextInt();

input = new int[n];

output = new int[n];

for(int i = 0; i<n; i++){

input[i] = sc.nextInt();

}

for(int i = 0; i<n; i++){

output[i] = sc.nextInt();

}

}

public boolean flip(int x,int y){

while(y-x > 1){

int temp = output[y];

output[y] = output[y-1];

output[y-1] = output[y-2];

output[y-2] = temp;

y = y-2;

count = count + 1;

}

if(y-x == 1){

int temp = output[y];

output[y] = output[y+1];

output[y+1] = output[y-1];

output[y-1] = temp;

count = count + 2;

return true;

}else if(y-x == 0){

return true;

}else{

return false;

}

}

public boolean put(int x){

int index = 0;

for(int i = x+1; i<n; i++){

if(input[x] == output[i]){

index = i;

break;

}

}

if(flip(x,index))

return true;

else return false;

}

public void method(){

for(int i = 0; i < n-2;i++){

if(input[i] != output[i])

if(!put(i))

break ;

}

if(input[n-2] == output[n-2] && input[n-1] == output[n-1]){

System.out.println("possible : " + count);

}

else{

System.out.println("not possible");

}

}

public static void main(String[] args) {

Solution asd = new Solution();

asd.get_input();

asd.method();

}

}

m

- Anonymous July 07, 2016