Skip to main content

Bean Array

A Bean array is defined to be an array where for every value n in the array, there is
 also an element n-1 or n+1 in the array.
  For example, {2, 10, 9, 3} is a Bean array because
 2 = 3-1
 10 = 9+1
 3 = 2 + 1
 9 = 10 -1
  Other Bean arrays include {2, 2, 3, 3, 3}, {1, 1, 1, 2, 1, 1} and {0, -1, 1}.
  The array {3, 4, 5, 7} is not a Bean array because of the value 7 which requires that the
 array contains either the value 6 (7-1) or 8 (7+1) but neither of these values are in the
 array. Write a function named isBean that returns 1 if its array argument is a Bean array.
 Otherwise it returns a 0.

 */
package dev;

/**
 *
 * @author Tej Bist
 */
public class BeanArray {

    public static void main(String arg[]) {
        System.out.println(new BeanArray().isBean(new int[]{3, 4, 5, 7}));
    }

    private int isBean(int[] nu) {
        for (int i = 0; i < nu.length; i++) {
            int count = 0;
            for (int j = 0; j < nu.length; j++) {
                if (nu[i] == (nu[j] - 1) || nu[i] == (nu[j] + 1)) {
                    count++;
                }
            }
            if (count == 0) {
                return 0;
            }
        }
        return 1;
    }
}

Comments

Popular posts from this blog

Learn Kubernetes for Microservices

As a microservice developer working with Kubernetes, there are several key concepts and components you should familiarize yourself with to effectively deploy and manage microservices in a Kubernetes environment. Here's a list to get you started:   Containerization Basics: Understand the fundamentals of containerization, particularly Docker, as Kubernetes relies heavily on containerized applications.  Kubernetes Architecture: Learn about the key components of the Kubernetes architecture, such as the control plane (API server, controller manager, scheduler, etcd), and nodes (worker machines).  Kubectl Command-Line Tool: Get familiar with kubectl, the primary command-line interface for interacting with Kubernetes clusters. Learn common commands for deploying, managing, and monitoring applications.  Pods: Understand the concept of pods, which are the smallest deployable units in Kubernetes. Pods encapsulate one or more containers and share network and storage resourc...

Spring Boot And Angular 4 with Angular CLI Integration

I recently spent the time to setup Angular CLI front-end using IntelliJ and Maven tool. Angular 4 is the next version of Angular 2. In this article, I am going to show how to integrate Angular 4 with SpringBoot RestAPI using IntelliJ step by step. In this article,  1. I used various technologies.  -> Maven  -> IntelliJ  -> SpringBoot  -> Java 8  -> Angular 4  ->Node.js  2. Setup Node.js and NPM.  Install Node.js from Download . And then check Node.js and NPM by node -v and npm -v command respectively. -> Open CMD in Windows and check Node.js and NPM: node -v and npm -v.   3. Install the Angular-CLI using the command line as:  -> open cmd and type npm install -g @angular/cli -> and check Angular-CLI after installing as: ng -v 4. Setup SpringBoot App using IntelliJ as: -> create a simple spring-boot restful app and dependency for the web in pom.xml file as < dependency > ...

String vs StringBuffer vs StringBuilder

What is the difference between String, StringBuffer and StringBuilder? String: Java String object is an immutable i.e. unmodifiable. It can be created by two ways by literal and new keyword. StringBuffer: Java StringBuffer is mutable. It can be created by using the new keyword. It is a synchronized means thread-safe. StringBuilder: Java StringBuilder is mutable. It is non-synchronized means not thread-safe.