Created
          May 22, 2021 12:40 
        
      - 
      
 - 
        
Save bijay-shrestha/7bcebf0d51d36c6a8f2865a0437617fe to your computer and use it in GitHub Desktop.  
    An integer array is defined to be sequentially-bounded if it is in ascending order and each value, n, in the array occurs less than n times in the array.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package com.hawa.practice; | |
| import lombok.extern.slf4j.Slf4j; | |
| @Slf4j | |
| public class SequentiallyBounded { | |
| public static void main(String[] args) { | |
| // int[] arrayOfNumbers = {1, 2, 3, 3, 3, 99, 99, 99, 99}; | |
| // int[] arrayOfNumbers = {1, 2, 3}; | |
| // int[] arrayOfNumbers = {2, 3, 3, 3, 3}; | |
| // int[] arrayOfNumbers = {12, 12, 9}; | |
| // int[] arrayOfNumbers = {0, 1}; | |
| // int[] arrayOfNumbers = {-1, 2}; | |
| // int[] arrayOfNumbers = {}; | |
| // int[] arrayOfNumbers = {5, 5, 5, 5}; | |
| int[] arrayOfNumbers = {5, 5, 5, 2, 5}; | |
| log.info("Checking if given array {} is sequentially bounded. --> Result: 1 for yes and 0 for no.", | |
| arrayOfNumbers); | |
| log.info("Actual result: {}", isSequentiallyBounded(arrayOfNumbers)); | |
| } | |
| static int isSequentiallyBounded(int[] a) { | |
| if (a.length == 0) { | |
| return 1; | |
| } | |
| int elementToCompare = a[0]; | |
| for (int i = 0; i < a.length; i++) { | |
| if (a[i] < 0) return 0; | |
| if (elementToCompare > a[i]) { | |
| log.info("The array {} is not in ascending order!!", a); | |
| return 0; | |
| } | |
| elementToCompare = a[i]; | |
| } | |
| for (int i = 0; i < a.length; i++) { | |
| int allowedCount = a[i] - 1; | |
| int actualCount = 0; | |
| for (int j = 0; j < a.length; j++) { | |
| if (a[j] == a[i]) { | |
| actualCount++; | |
| } | |
| } | |
| if (actualCount > allowedCount) { | |
| log.info("Actual Count of {} exceeded!", a[i]); | |
| return 0; | |
| } | |
| } | |
| return 1; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment