Monday, January 9, 2023

An array is called balanced if it's even numbered elements (a[0], a[2], etc.) are even and its odd numbered elements (a[1], a[3],etc.) are Odd. Write a function named is Balanced that accepts an array of integers and returns 1 if the array is balanced otherwise it returns 0.


 ..

/**
* * An array is called balanced if its even numbered elements (a[0], a[2], etc.) are even and its odd
* * numbered elements (a[1], a[3], etc.) are odd.
* *
* * Write a function named isBalanced that accepts an array of integers and returns 1 if the array is
* * balanced, otherwise it returns 0.
* *
* * Examples: {2, 3, 6, 7} is balanced since a[0] and a[2] are even, a[1] and a[3] are odd. {6, 7, 2, 3, 12}
* * is balanced since a[0], a[2] and a[4] are even, a[1] and a[3] are odd.
* * {7, 15, 2, 3} is not balanced since a[0] is odd.
* * {16, 6, 2, 3} is not balanced since a[1] is even.
* *
* * If you are programming in Java, the function signature is
* * int isBalanced(int[ ] a)
*/

..

public class Balanced {


    public static void main(String[] args) {


        int[] arrayOfNumbers = {2, 3, 6, 7};

          //      int[] arrayOfNumbers = {6, 7, 2, 3, 12};

        //     int[] arrayOfNumbers = {7, 15, 2, 3};

        //     int[] arrayOfNumbers = {16, 6, 2, 3};


        System.out.println(isbalanced(arrayOfNumbers));


    }


    public static int isbalanced(int[] a) {

        int i, j;

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

            if (a[i] % 2 != 0) {

                return 0;

            }

        }


        for (j = 1; j < a.length; j = j + 2) {

            if (a[j] % 2 == 0) {

                return 0;

            }

        }

        return 1;


    }

}



https://github.com/bijay-shrestha/practice/blob/master/src/main/java/com/basic/practice/Balanced.java

/*An array is called balanced if its even numbered elements (a[0], a[2], etc.) are even and its  odd numbered elements (a[1], a[3], etc.) are odd.  Write a function named isBalanced that accepts an array of integers and returns 1 if the array is balanced, otherwise it returns 0. Examples:  {2, 3, 6, 7} is balanced since a[0] and a[2] are even, a[1] and a[3] are odd. {6, 7, 2, 3, 12} is balanced since a[0], a[2] and a[4] are even, a[1] and a[3] are odd. {7, 15, 2, 3} is not balanced since a[0] is odd. {16, 6, 2, 3} is not balanced since a[1] is even.

If you are programming in Java or C#, the function signature is  

int isBalanced(int[ ] a)

If you are programming in C or C++, the function signature is  

int isBalanced(int a[ ], int len) 

where len is the number of elements in the array.   */


public class HelloWorld {

   public static void main(String []args){

       int[] arr={7, 15, 2, 3} ;

        int result=isbalanced(arr);

        System.out.println("isbalanced->"+result);

         

     }

    public static int isbalanced(int[] a)

     {int i,j;

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

      {

          if(a[i]%2!=0)

          {

              return 0;

          }

      }

      

      for(j=1;j<a.length;j=j+2)

      {

          if(a[j]%2==0)

          {

              return 0;

          }

      }

      return 1;

   

         }

     }

https://onecompiler.com/java/3wzmwmh46

Question :
A balanced array is defined to be an array where for every value n in the array, -n also is in the array. For example {-2, 3, 2, -3} is a balanced array. So is {-2, 2, 2, 2}. But {-5, 2, -2} is not because 5 is not in the array.
Write a function named isBalanced that returns 1 if its array argument is a balanced array. Otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isBalanced (int [ ] a);
If you are programming in C or C++, the function signature is


int isBalanced(int a[ ], int len) where len is the number of elements in the array.
Solution :
public static int isBalanced(int [] a)
        {
            int isBalanced = 1;
            for(int i=0;i<a.Length;i++)
            {
                int isValid = 0;
                for(int j=0;j<a.Length;j++)
                {
                    if(a[i]==-a[j])
                    {
                        isValid = 1;
                        break;
                    }
                }
                if (isValid==0)
                    isBalanced = 0;
            }
            return isBalanced;
        }

..

No comments:

Post a Comment

CORBA Java Tutorial using Netbeans and Java 8.

CORBA-Example A simple CORBA implementation using Java Echo.idl module EchoApp{ interface Echo{ string echoString(); }; }; ...