## Infosys Interview Question for Software Developers

Country: India
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
0
of 0 vote

m

Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
}
}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.