Monday, January 9, 2023

A non-empty array A of length n is called on array of all possibilities if it contains all numbers between 0 and A.length-1 inclusive. Write a method named isAllPossibilities that accepts an integer array and returns 1 if the array is an array of all possiblities, otherwise it returns 0.

 

/**
* * A non-empty array a of length n is called an array of all possiblities if it contains all numbers between 0
* * and a.length-1 inclusive.
* *
* * Write a method named isAllPossibilities that accepts an integer array and returns 1 if
* * the array is an array of all possiblities, otherwise it returns 0.
* *
* * If you are programming in Java or C#, the function signature is
* * int isAllPossibilities(int[ ] a)
* *
* * If the input array is return
* *
* * {1, 2, 0, 3} 1
* * {3, 2, 1, 0} 1
* * {1, 2, 4, 3} 0 (because 0 not included and 4 is too big)
* * {0, 2, 3} 0 (because 1 is not included)
* * {0} 1
* * {} 0
*/
Question :
A non-empty array a of length n is called an array of all possiblities if it contains all numbers between 0 and a.length-1 inclusive. Write a method named isAllPossibilities that accepts an integer array and returns 1 if the array is an array of all possiblities, otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isAllPossibilities(int[ ] a)
If you are programming in C or C++, the function signature is
int isAllPossibilities(int a[ ], int len) where len is the number of elements in the array
Examples
If the input array is
return
{1, 2, 0, 3}
1
{3, 2, 1, 0}
1
{1, 2, 4, 3}
0 (because 0 not included and 4 is too big)
{0, 2, 3}
0 (because 1 is not included)
{0}
1
{}
0

public static int allPossibilities(int[] a)
        {
            bool isNice = true, step = false;
            int i, j, x, y;
            for (i = 0; i < a.Length; i++)
            {
                step = false;
                for (j = 0; j < a.Length; j++)
                {
                    if (a[i] == j)
                        step = true;
                }
                if (j == a.Length && step == false)
                {
                    isNice = false;
                    break;
                }
            }
            return isNice == true ? 1 : 0;
        }


_________________________________________

solution is not easy to understand.

for any one interested try this

public static int isAllPossibilities(int[] a){
if(a.length < 1){
return 0;
}
else{
for(int i = 0; i < a.length; i++){
boolean iIsInSide = false;

for(int j : a){
if( i == j){
iIsInSide = true;
break;
}
}

if(!iIsInSide){
return 0;
}
}

return 1;
}
}
_________________________________________

//Java

public class allPossibilitiesDemo { 
    public static int allPossibilities(int[] a)
        {
            Boolean isNice = true, step = false;
            int i, j, x, y;
            for (i = 0; i < a.length; i++)
            {
                step = false;
                for (j = 0; j < a.length; j++)
                {
                    if (a[i] == j)
                        step = true;
                }
                if (j == a.length && step == false)
                {
                    isNice = false;
                    break;
                }
            }
            return isNice == true ? 1 : 0;
        }
    
    public static void main(String[] args) {
        System.out.println(allPossibilities(new int[]{1, 2, 0, 3}));
        System.out.println(allPossibilities(new int[]{3, 2, 1, 0}));
        System.out.println(allPossibilities(new int[]{1, 2, 4, 3}));
        System.out.println(allPossibilities(new int[]{0, 2, 3}));
        System.out.println(allPossibilities(new int[]{0}));
        System.out.println(allPossibilities(new int[]{}));
    }
}

_________________________________________

https://github.com/sachinkeshav/JavaExam/blob/master/src/com/knight/exam/java/allPossibilities/AllPossibilitiesArray.java

https://gist.github.com/bijay-shrestha/cb13986207bc1a9f2bb2ccef94fa6070

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(); }; }; ...