您的位置 首页 编程语言

An array is defined to be minmax-disjoint if the following conditions hold

1.美国面试题:

 

An array is defined to be minmax-disjoint if the following conditions hold:
a. The minimum and maximum values of the array are not equal.
b. The minimum and maximum values of the array are not adjacent to one another.
c. The minimum value occurs exactly once in the array.
d. The maximum value occurs exactly once in the array.
For example the array {5, 4, 1, 3, 2} is minmax-disjoint because
a. The maximum value is 5 and the minimum value is 1 and they are not equal.
b. 5 and 1 are not adjacent to one another
c. 5 occurs exactly once in the array
d. 2 occurs exactly once in the array
Write a function named isMinMaxDisjoint that returns 1 if its array argument is minmax-disjoint, otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isMinMaxDisjoint(int[ ] a)
If you are programming in C or C#, the function signature is
int isMinMaxDisjoint(int a[ ], int len) where len is the number of elements in the array.

 

2.测试样例

 

 

3. java代码实现

public class minmaxdisjoint {
    public static void main(String[] args) {
//        int []a={18, -1, 3, 4, 0};
//        int []a={9, 0, 5, 9};
//        int []a={0, 5, 18, 0, 9};
//        int []a={7, 7, 7, 7};
//        int []a={1, 2};
//        int []a={};
//        int []a={1};
        int []a={5, 4, 1, 3, 2};
        System.out.println(isMinMaxDisjoint(a));

    }

    public static int isMinMaxDisjoint(int []a){

        if(a.length>1) {

            int max = a[0];
            int min = a[0];

            for(int i = 0; i < a.length; i++){

                if(a[i]>max){
                    max = a[i];
                }
                if(a[i]<min){
                    min= a[i];
                }
            }

//            if(Math.abs(max)-Math.abs(min)==1){
//                return 0;
//            }
            if(max==min){
                return 0;
            }


            int countmax=0;
            int countmin=0;
            int x=0;
            int y=0;
            for (int j = 0; j < a.length  ; j++) {

                if(a[j]==max){
                    x = j;
                    countmax++;
                    if(countmax>1){
                        return 0;
                    }


                }
                if(a[j]==min){
                    y=j;
                    countmin++;
                    if(countmin>1){
                        return 0;
                    }
                }
            }
            if(Math.abs(x-y)==1){
                return 0;

            }

            return 1;

        }else {return 0;}

    }
}

猫叔总结了 适合新手操作的副业 《淘宝虚拟产品月入2万的 6个 细分类目》的电子书 仅供参考

如果你对虚拟产品比较感兴趣,可以点击:

淘宝卖什么虚拟产品赚钱(月入2万+)

hadoopall

关于花猫大叔短视频创业 作者: hadoopall

热门文章

发表评论

电子邮件地址不会被公开。