Java program to change the elements of the array with the product of remaining elements

Hi Everyone, In this blog post I will be discussing about an interesting program, which is to change the "elements of the array with the product of remaining elements". This program is really interesting as well as tricky. If an array contains 0, the product of all the elements in the array will be 0. For eg: If an array is [0,1,2], after performing the algorithm, it will be [2,0,0].

Logic

The logic of the program is as follows:

  1. Initialize an integer variable product = 1. (This variable is used to store the product of all elements of array)
  2. Initialize an integer variable countOfZeros =0
  3. Find the product of all the non-zero elements in the array as well as count the number of zero's in the array.
  4. If countOfZeros is greater than or equal to 2, then change all the elements of array to 0.
  5. If countOfZeros is 1, then change all non zero elements to 0 and 0 to product.
  6. If countOfZeros is 0, then change all elements to product/(element).
IDE used - IntelliJ

Program

The program is as follows:

package com.techjourney;

/**
 * Created by Sujith on 3/5/2015.
 */

public class ReplaceElementsInArray {

    public static void main(String args[])
    {
        int numbers_set1[] = {1,2,5,0,8,4,6,1,4};
        int numbers_set2[]   = {3,7,2,4,8,1,6};
        int numbers_set3[]  = {0,3,4,9,100,2,0};

        System.out.println("\n The contents of numbers_set1 are :");
        printArray(numbers_set1);

        numbers_set1=ReturnReplacedArray(numbers_set1);

        System.out.println("\n The contents of numbers_set1 after modification :");
        printArray(numbers_set1);

        System.out.println("\n The contents of numbers_set2 are :");
        printArray(numbers_set2);

        numbers_set2=ReturnReplacedArray(numbers_set2);

        System.out.println("\n The contents of numbers_set2 after modification :");
        printArray(numbers_set2);

        System.out.println("\n The contents of numbers_set3 are :");
        printArray(numbers_set3);

        numbers_set3=ReturnReplacedArray(numbers_set3);

        System.out.println("\n The contents of numbers_set3 after modification :");
        printArray(numbers_set3);

        System.out.println();
    }

    public static int[] ReturnReplacedArray(int arr[])
    {
        int product =1;
        int productWithZeros =1;
        int countOfZeros =0;
        for(int i=0; i<arr.length;i++)        
       {
            if(arr[i]!=0)
            {
                product = product*arr[i];
            }

            else
            {
                countOfZeros++;
            }
        }

        if(countOfZeros>=2)
        {
            for(int i=0; i<arr.length;i++)

            {
               arr[i]=0;
            }
        }

        else if(countOfZeros==1)
        {
            for(int i=0; i<arr.length;i++)

            {
                if(arr[i]==0)
                {
                    arr[i]=product;
                }
                else
                {
                    arr[i]=0;
                }
            }
        }

        else
        {
            for(int i=0; i<arr.length;i++)

            {
                arr[i]=product/arr[i];
            }
        }

        return arr;
    }

    public static void printArray(int arr[])
    {
        for(int i=0;i<arr.length;i++)        

        {
            System.out.print("\t"+arr[i]);
        }
    }


}


In the above program, I have created 3 different examples of arrays having where one is having 2 or more zero elements, one with 1 zero element and other with all non zero elements.

Output of the above program



 The contents of numbers_set1 are :
    1    2    5    0    8    4    6    1    4
 The contents of numbers_set1 after modification :
    0    0    0    7680    0    0    0    0    0
 The contents of numbers_set2 are :
    3    7    2    4    8    1    6
 The contents of numbers_set2 after modification :
    2688    1152    4032    2016    1008    8064    1344
 The contents of numbers_set3 are :
    0    3    4    9    100    2    0
 The contents of numbers_set3 after modification :
    0    0    0    0    0    0    0
Picked up _JAVA_OPTIONS: -Xmx512M

Process finished with exit code 0

I hope everyone understood this program very well. Please do comment if you have any doubts.

Similar programs

  1. Finding third largest element in an array.
  2. Java program to convert integer to string
  3. How to find whether number is power of multiple of 2.


Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++