You are here: Home / Topics / Write a program that evenly distributes the elements of the array named array among the array1, array2 and array3 In java

Write a program that evenly distributes the elements of the array named array among the array1, array2 and array3 In java

Filed under: Java on 2024-02-27 13:50:21

Write a program that evenly distributes the elements of the array named array among the array1, array2 and array3 arrays according to the following rule:

0th element in array1, 1st -> array2, 2nd -> array3, 3rd -> array1, etc.

Requirements:
The program must not read anything from the keyboard.
The program must evenly distribute elements of the array array to array1, array2 and array3 arrays.

Code:
import java.util.Arrays;

public class Solution {

   public static int[] array = new int[]{-10, 20, 30, -40, -50, 60, 70, -80, -90};
   public static int[] array1 = new int[3];
   public static int[] array2 = new int[3];
   public static int[] array3 = new int[3];

   public static void main(String[] args) {
       //write your code here
       int num = 0;
       for (int i =0 ; i < array.length; i++){
           array1[num]=array[i];
           i++;
           array2[num] = array[i];
           i++;
           array3[num]=array[i];
           num++;
       }

       System.out.println(Arrays.toString(array1));
       System.out.println(Arrays.toString(array2));
       System.out.println(Arrays.toString(array3));
   }
}

 

OutPut:

[-10, -40, 70]
[20, -50, -80]
[30, 60, -90]

About Author:
J
Java     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.