kdaud

Interview Solution for Elezy for MITs

Aug 27th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. package com.tests;
  2.  
  3. public class Quiz1 {
  4.    
  5.     /**
  6.      * @param a
  7.      * @author kdaud
  8.      * @since 2017
  9.      * @version 1.1.0
  10.      */
  11.     //Best answer to submit Quiz1
  12.    
  13.     static int hasSingleMaximum(int[] a) {
  14.         if (a == null || a.length == 0) {
  15.             return 0;
  16.         }
  17.         int max = Integer.MIN_VALUE;
  18.         boolean isSingleMax = true;
  19.         for (int value : a) {
  20.             if (value == max) {
  21.                 isSingleMax = false;
  22.             } else if (value > max) {
  23.                 max = value;
  24.                 isSingleMax = true;
  25.             }
  26.         }
  27.        
  28.         return isSingleMax ? 1 : 0;
  29.     }
  30.    
  31.     //Best answer for Quiz 2
  32.    
  33.     static int isSelfReferential(int[] a) {
  34.         if (a == null || a.length == 0) {
  35.             return 0;
  36.         }
  37.        
  38.         for (int i = 0; i < a.length; i++) {
  39.             int count = 0;
  40.             for (int x : a) {
  41.                 if (x == i) {
  42.                     count++;
  43.                 }
  44.             }
  45.             if (count != a[i]) {
  46.                 return 0;
  47.             }
  48.         }
  49.        
  50.         return 1;
  51.     }
  52.    
  53.     //Best answer for Quiz 3
  54.     static int isTwinPaired(int[] a, int y) {
  55.         if (a == null || a.length < 2 || y < 1 || y > a.length) {
  56.             return 0;
  57.         }
  58.        
  59.         for (int i = 0; i < a.length; i++) {
  60.             for (int j = i + 1; j < a.length; j++) {
  61.                 int sumValues = a[i] + a[j];
  62.                 int sumIndex = i + j;
  63.                 if (sumValues == y && sumIndex == y) {
  64.                     return 1;
  65.                 }
  66.             }
  67.         }
  68.        
  69.         return 0;
  70.     }
  71.    
  72. }
Add Comment
Please, Sign In to add comment