Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tests;
- public class Quiz1 {
- /**
- * @param a
- * @author kdaud
- * @since 2017
- * @version 1.1.0
- */
- //Best answer to submit Quiz1
- static int hasSingleMaximum(int[] a) {
- if (a == null || a.length == 0) {
- return 0;
- }
- int max = Integer.MIN_VALUE;
- boolean isSingleMax = true;
- for (int value : a) {
- if (value == max) {
- isSingleMax = false;
- } else if (value > max) {
- max = value;
- isSingleMax = true;
- }
- }
- return isSingleMax ? 1 : 0;
- }
- //Best answer for Quiz 2
- static int isSelfReferential(int[] a) {
- if (a == null || a.length == 0) {
- return 0;
- }
- for (int i = 0; i < a.length; i++) {
- int count = 0;
- for (int x : a) {
- if (x == i) {
- count++;
- }
- }
- if (count != a[i]) {
- return 0;
- }
- }
- return 1;
- }
- //Best answer for Quiz 3
- static int isTwinPaired(int[] a, int y) {
- if (a == null || a.length < 2 || y < 1 || y > a.length) {
- return 0;
- }
- for (int i = 0; i < a.length; i++) {
- for (int j = i + 1; j < a.length; j++) {
- int sumValues = a[i] + a[j];
- int sumIndex = i + j;
- if (sumValues == y && sumIndex == y) {
- return 1;
- }
- }
- }
- return 0;
- }
- }
Add Comment
Please, Sign In to add comment