BY USING JAVA Eclipse without using null BY USING : Loops Int Random Return arra
ID: 3828356 • Letter: B
Question
BY USING JAVA Eclipse
without using null
BY USING :
Loops
Int
Random
Return
arrays
QUESTION
Create an array with a size of an integer number, n. n will be generated as a random integer number in the interval between 3 and 10 (both included). Also, generate random numbers to fill the array. These numbers must be in the interval between 1 and 20 (both included). Then, find if this array is a “Winner” or a “Loser”. Considering every element in the array, check if there is any element which has the equal summation of the elements in the left-hand side and in the right-hand side. If there is at least 1 element in this array which satisfies this condition, print “Winner”. Otherwise, print “Loser”. You need to check every element in the array if there exists an element to satisfy this condition or not. If there are no elements on the left or right-hand sides, then take the summation as zero. Considering every element in the array, check if sum of the elements in the left-hand side is equal to the sum of the elements in the right-hand side, this array has a Victory. If this condition is satisfied, say “Winner” to user. Otherwise, say “Loser”. You need to check all elements in the array if there exists an element to satisfy this condition or not. If there are no elements to the left or right, then take the summation as zero.
Output Format
Display the size of the array and the elements of the array when it is filled by random numbers. Display “Winner” if there exist at least one element in the array, such that the sum of the elements on its left-hand side and the sum of the elements on its right-hand side are equal. Otherwise display “Loser”.
SAMPLE OUTPUTS
Sample Output 1
4 elements in the array
Elements: 1, 2, 3, 3
Winner
Explanation 1
Element in 2 nd index satisfies the given condition (1 + 2 = 3).
Sample Output 2
8 elements in the array
Elements: 2, 5, 3, 3, 5, 8, 12, 6
Winner
Explanation 2
Element in 5 th index satisfies the given condition (2 + 5 + 3 + 3 + 5 = 12 + 6).
Sample Output 3
5 elements in the array
Elements: 6, 9, 2, 1, 20
Loser
Explanation 3
No elements in the array that satisfies the given condition.
Explanation / Answer
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) {
Random r = new Random();
//Reading n
int n = r.nextInt(9)+2;
int arr[] = new int[n];
//Reading n elements
for(int i=0;i<n;i++){
arr[i] = r.nextInt(20)+1;
}
System.out.println(n+" Elements in the array");
System.out.println("Elements : "+Arrays.toString(arr));
//Calculating the prefix array
int pref[] = new int[n];
pref[0] = arr[0];
for(int i=1;i<n;i++){
pref[i] = pref[i-1]+arr[i];
}
//Calculating the suffix array
int suff[] = new int[n];
suff[n-1] = arr[n-1];
for(int i=n-2;i>=0;i--){
suff[i] = suff[i+1]+arr[i];
}
//Check whether prefix sum is equal to any of the suffix sum
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(pref[i]==suff[i]){
System.out.println("Winner");
return;
}
}
}
System.out.println("Looser");
}
OUTPUT :
6 Elements in the array
Elements : [7, 1, 13, 3, 6, 15]
Winner
OUTPUT :
6 Elements in the array
Elements : [20, 14, 2, 15, 2, 6]
Looser
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.