check if first array is sorted and if there are consecutive duplicate elements check second array at the index of duplicate elements

huangapple go评论81阅读模式
英文:

check if first array is sorted and if there are consecutive duplicate elements check second array at the index of duplicate elements

问题

我们需要检查第一个数组是否已排序,如果存在连续重复元素,则在重复元素的索引处检查第二个数组。这是我的代码和输出

我期望的输出是false,因为a[2]=a[3],所以我们应该转到第二个数组,在那里b[2]>b[3]

举例:

array1[]={1,2,3,4,5};
array2[]={5,6,4,3,2};

这应该返回true,因为第一个数组已排序。

array1[]={1,2,3,3,4};
array2[]={5,4,3,6,2};

这也应返回true,因为**array1[2]=array1[3],然后我们转到array2,在那里array2[2]<array2[3],因此应返回true**。

array1[]={1,2,3,3,4};
array2[]={5,4,4,4,2};

这也应返回true,因为**array1[2]=array1[3],然后我们转到array2,在那里array2[2]=array2[3],因此应返回true**。

array1[]={1,2,3,3,4};
array2[]={5,6,4,3,2};

这也应返回false,因为**array1[2]=array1[3],然后我们转到array2,在那里array2[2]>array2[3],因此应返回false**。

英文:

We need to check if the first array is sorted, and if there are consecutive duplicate elements, check the second array at the index of duplicate elements. This is my code and output

The output I expect is false since a[2]=a[3] and thus we should move to the 2nd array and there b[2]&gt;b[3].

for ex:
1.

array1[]={1,2,3,4,5};
array2[]={5,6,4,3,2};

this should return true because the first array is sorted

array1[]={1,2,3,3,4};
array2[]={5,4,3,6,2};

this should also return true since array1[2]=array1[3] then we go to the array2 and there
array2[2]<array2[3] and hence it should return true.

array1[]={1,2,3,3,4};
array2[]={5,4,4,4,2};

this should also return true since array1[2]=array1[3] then we go to the array2 and there
array2[2]=array2[3] and hence it should return true.

array1[]={1,2,3,3,4};
array2[]={5,6,4,3,2};

this should also return false since array1[2]=array1[3] then we go to the array2 and there
array2[2]&gt;array2[3] and hence it should return false.

答案1

得分: 0

以下是翻译好的部分:

在您的情况下(4),它返回true,因为在您的代码中,在检查重复值的循环内部有if (a [i] &lt; a[i+1]) return true。所以在array1中(1 < 2),为true,因此它返回true。

如何解决这个问题,您需要将代码分为两部分。

    1. 检查第一个数组是否已排序,一个很好的解释在stackoverflow.com/a/19458302/3429103中,
    1. 您的代码完成对连续相同值的验证,并在第二个数组中进行检查。

类似于

public class Main
{
    public static boolean isSorted(int[] a) 
    {
        for (int i = 0; i &lt; a.length - 1; i++) {
            if (a[i] &gt; a[i + 1]) 
            {
                return false;
            }
        }

        return true;
    }
    
    public static boolean checkDuplicate(int[] a, int b[]) 
    {
        for (int i = 0; i &lt; a.length - 1; i++) 
        {
            if (a[i] == a[i + 1] &amp;&amp; b[i] &gt; b[i + 1]) 
            {
                return false;
            }
        }

        return true;
    }
    
	public static void main(String[] args) 
	{ 
	    int array1[]={1,2,3,3,4}; 
	    int array2[]={5,6,4,3,2};
	    
	    if(isSorted(array1) &amp;&amp; checkDuplicate(array1,array2))
	        System.out.println(&quot;True&quot;);
	    else
	        System.out.println(&quot;False&quot;);
	}
}
英文:

In your case (4) it return true, because in your code you have if (a [i] &lt; a[i+1]) return true inside the loop that check the duplicated values. So in array1 (1 < 2) is true, hence it return true.
How to solve this issue, you have to divide your code in two parts.

    1. check if the first array is sorted a good explanation is in stackoverflow.com/a/19458302/3429103,
    1. your code to complete the verification of same value successive and check in the second array.

something like

public class Main
{
    public static boolean isSorted(int[] a) 
    {
        for (int i = 0; i &lt; a.length - 1; i++) {
            if (a[i] &gt; a[i + 1]) 
            {
                return false;
            }
        }

        return true;
    }
    
    public static boolean checkDuplicate(int[] a, int b[]) 
    {
        for (int i = 0; i &lt; a.length - 1; i++) 
        {
            if (a[i] == a[i + 1] &amp;&amp; b[i] &gt; b[i + 1]) 
            {
                return false;
            }
        }

        return true;
    }
    
	public static void main(String[] args) 
	{ 
	    int array1[]={1,2,3,3,4}; 
	    int array2[]={5,6,4,3,2};
	    
	    if(isSorted(array1) &amp;&amp; checkDuplicate(array1,array2))
	        System.out.println(&quot;True&quot;);
	    else
	        System.out.println(&quot;False&quot;);
	}
}

huangapple
  • 本文由 发表于 2020年10月23日 22:36:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64502049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定