简体   繁体   中英

Given an array A of N integers between(-100 and 100) Java

I'm practicing Java and my prof has given me this problem:

You're given an array A of N integers (between -100 and 100), calculate the multiplied value of all elements inside the array and return (-1, 0, 1) based on the output.

For instance:

A[1,2,3] will return 1 because the output is (6), 
A[-1,2,3] will return -1 because the output is (-6), 
A[1,2,0] will return 0 because the output is (0)

Method Definition:

public static int solution(int[] A) {
}

My approach: I was thinking this would be a recursive process because I will multiple have inputs.

Process one block A[1,2,3], store the value (1) into a newArr[], process another block A[-1,2,3], store the value (-1) into newArr[] and so on... at the end return newArr[] with the proper values in it.

What I have so far, now I am kind of stuck..

public static void main(String[] args) {
        // TODO Auto-generated method stub
         int arr[] = {6,-7,8,9};
         int arr1[] = {-6,-7,8,9};
         int arr2[] = {0,-7,8,9};
         System.out.println("[ " + solution(arr) + " ]  => Needs to be -1" );
         System.out.println("[ " + solution(arr1) + " ] => Needs to be  1");
         System.out.println("[ " + solution(arr2) + " ] => Needs to be 0");
         System.out.println("Final return should be (-1, 1, 0)");
    }
    
    public static int solution(int[] A) {
        int temp = 1;
        for (int i = 0; i < A.length; i++){
            temp *= A[i];
        }
        return temp;
    }

I don't know if this is the right way to proceed, any ideas as to how I should go about doing this?

Thanks in advance!

No need to do any multiplication.

  • if any number is 0 return 0 immediately since the product will be zero.
  • if the count of negative numbers is even, return 1 since the product of an even number of negatives is a positive.
  • else return -1
public static int solution(int[] ints) {
    int count = 0;
    for (int i : ints) {
        if (i == 0) {
            return 0;
        }
        if (i < 0) {
            count++;
        }
    }
    return count % 2 == 0 ? 1 : -1;
}

Array multiplication left to OP.

Look at Integer.signum() - which will convert the result to 1, 0, -1 for >0, 0, <0.

Implementing with a wrapper, along with @Mr R's suggestion

public class test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         int arr[] = {6,-7,8,9};
         int arr1[] = {-6,-7,8,9};
         int arr2[] = {0,-7,8,9};

         int[][] array_of_arrays = {arr, arr1, arr2};
         int[] results_array;
         results_array = solution_wrapper(array_of_arrays); 
         
         // then do what you neeed to do with the results_array
         
         System.out.println(results_array[0]);
         System.out.println(results_array[1]);
         System.out.println(results_array[2]);
         System.out.println("[ " + solution(arr) + " ]  => Needs to be -1" );
         System.out.println("[ " + solution(arr1) + " ] => Needs to be  1");
         System.out.println("[ " + solution(arr2) + " ] => Needs to be 0");
         System.out.println("Final return should be (-1, 1, 0)");
    }
    
    public static int solution(int[] A) {
        int temp = 1;
        for (int i = 0; i < A.length; i++){
            temp *= A[i];
        }
        return Integer.signum(temp);
    }

    public static int[] solution_wrapper(int[][] allA) {
        int[] rv = new int[allA.length];
        for (int j = 0; j < allA.length; j++) {
            rv[j] = solution(allA[j]);
        }
        return rv;
    }
}

You can declare and initialize a final array by using the method you created:

int[] finalArr = {solution(arr), solution(arr1), solution(arr2)};

However, I'm not sure where you want to return the final array from. You have your main method which doesn't have a return type and your solution method. Are you certain that you're supposed to return the final array, or is simply having one enough?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM