# 用另一个数组中的元素替换几乎有序数组中的元素。

go评论67阅读模式

Replacing element in an almost sorted array with an element from another array

# 问题

``````int n1 = s.nextInt();
int[] array1 = new int[n1];
for (int i = 0; i < n1; i++) {
array1[i] = s.nextInt();
}

int n2 = s.nextInt();
int[] array2 = new int[n2];
for (int i = 0; i < n2; i++) {
array2[i] = s.nextInt();
}

int temp1 = 0;
int temp2 = 0;

for (int i = 0; i < array1.length - 1; i++) {
if (array1[i] > array1[i + 1]) {
temp1 = array1[i + 1];
temp2 = array1[i + 2];
break;
}
}

int temp3 = 0;
for (int j = 0; j <= array2.length - 1; j++) {
if (array2[j] > temp1 && array2[j] < temp2) {
temp3 = array2[j];
break;
}
}
System.out.println(temp3);
``````

My first array is almost sorted. I need to replace the unsorted element with the max possible element from the second array. The wrongly placed element would not be on the 0th or n-1 index. And if array1[i] is less than array1[i-1], then index i is the wrong index.

For ex:
first input 5
first array: 2 7 8 6 13
second input 4
second array 15 11 9 5
My result would be 11

however, If there is no possible element I could take from the second array, it would print not possible. for instance : 14 15 16 17 or if it has 0 element.

How can I fix this code?

``````   int n1 = s.nextInt();
int[] array1 = new int[n1];
for (int i = 0; i &lt; n1; i++) {
array1[i] = s.nextInt();
}

int n2 = s.nextInt();
int[] array2 = new int[n2];
for (int i = 0; i &lt; n2; i++) {
array2[i] = s.nextInt();
}

int temp1=0;
int temp2=0;

for(int i=0; i &lt; array1.length-1;i++) {
if (array1[i] &gt; array1[i+1]) {
temp1=array1[i+1];
temp2=array1[i+2];
break;
}

}

int temp3 = 0;
for(int j=0; j&lt;=array2.length-1;j++) {
if(array2[j] &gt; temp1 &amp;&amp; array2[j] &lt; temp2){
temp3 = array2[j];
break;
}
}
System.out.println(temp3);

}

}
``````

# 答案1

``````// "static void main" 必须在一个公共类中定义。
public class Main {
public static void main(String[] args) {

Scanner s = new Scanner(System.in);

int n1 = s.nextInt();
s.nextLine();
int[] array1 = new int[n1];
for (int i = 0; i &lt; n1; i++)
array1[i] = s.nextInt();
s.nextLine();

int n2 = s.nextInt();
s.nextLine();
int[] array2 = new int[n2];
for (int i = 0; i &lt; n2; i++)
array2[i] = s.nextInt();
s.nextLine();

int temp1=0;
int temp2=0;

// 执行一些更改，如下所示，对于上述问题中提到的测试用例有效...
// 1. 假设索引 i+1 处的值违反了排序顺序，则应取 temp1 = i 和 temp2 = i+2。
for(int i=0; i &lt; array1.length-1;i++)
if (array1[i] &gt; array1[i+1]){
temp1=array1[i];
temp2=array1[i+2];
}

int temp3 = -1;
// 2. 找到在 temp1 和 temp2 之间的数组2中的最大值。
for(int j=0; j&lt;array2.length-1;j++)
if(array2[j] &gt; temp1 &amp;&amp; array2[j] &lt; temp2)
if(array2[j]&gt;temp3)
temp3 = array2[j];

if(temp3 == -1)
System.out.println("不可能\n");
else
System.out.println(temp3);

}
}
``````

Performing Some changes in your code as shown below, it worked for the test cases mentioned in the problem above...

1. Suppose the value at index i+1 violates the sorted order then you should take temp1 = i and temp2 = i+2.

2. Find the maximum value in array2 which lies between temp1 and temp2.

``````// &quot;static void main&quot; must be defined in a public class.
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n1 = s.nextInt();
s.nextLine();
int[] array1 = new int[n1];
for (int i = 0; i &lt; n1; i++)
array1[i] = s.nextInt();
s.nextLine();
int n2 = s.nextInt();
s.nextLine();
int[] array2 = new int[n2];
for (int i = 0; i &lt; n2; i++)
array2[i] = s.nextInt();
s.nextLine();
int temp1=0;
int temp2=0;
for(int i=0; i &lt; array1.length-1;i++)
if (array1[i] &gt; array1[i+1]){
temp1=array1[i];
temp2=array1[i+2];
}
int temp3 = -1;
for(int j=0; j&lt;array2.length-1;j++)
if(array2[j] &gt; temp1 &amp;&amp; array2[j] &lt; temp2)
if(array2[j]&gt;temp3)
temp3 = array2[j];
if(temp3 == -1)
System.out.println(&quot;Not Possible\n&quot;);
else
System.out.println(temp3);
}
}
``````

• 本文由 发表于 2020年8月3日 01:45:16
• 转载请务必保留本文链接：https://go.coder-hub.com/63219244.html
• algorithm
• arrays
• java
• sorting

go 48

go 47

go 58

go 63