Question

Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range

write a java program
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the code with Output:

import java.util.*;
public class Subaaray{
static int maximum(int a, int b, int c)
{
if (a>=b && a>=c)
return a;
else if (b>=a && b>=c)
return b;
return c;
}

// function to find maximum sum of subarray crossing the middle element
static int maxCrossingSubarray(int ar[], int low, int mid, int high)
{
/*
Initial leftSum should be -infinity.
*/
int leftSum = Integer.MIN_VALUE;
int sum = 0;
int i;

/*
iterating from middle
element to the lowest element
to find the maximum sum of the left
subarray containing the middle
element also.
*/
for (i=mid; i>=low; i--)
{
sum = sum+ar[i];
if (sum>leftSum)
leftSum = sum;
}

/*
Similarly, finding the maximum
sum of right subarray containing
the adjacent right element to the
middle element.
*/
int rightSum = Integer.MIN_VALUE;
sum = 0;

for (i=mid+1; i<=high; i++)
{
sum=sum+ar[i];
if (sum>rightSum)
rightSum = sum;
}

/*
returning the maximum sum of the subarray
containing the middle element.
*/
return (leftSum+rightSum);
}

// function to calculate the maximum subarray sum
static int maxSumSubarray(int ar[], int low, int high)
{
if (high == low) // only one element in an array
{
return ar[high];
}

// middle element of the array
int mid = (high+low)/2;

// maximum sum in the left subarray
int maximumSumLeftSubarray = maxSumSubarray(ar, low, mid);
// maximum sum in the right subarray
int maximumSumRightSubarray = maxSumSubarray(ar, mid+1, high);
// maximum sum in the array containing the middle element
int maximumSumCrossingSubarray = maxCrossingSubarray(ar, low, mid, high);

// returning the maximum among the above three numbers
return maximum(maximumSumLeftSubarray, maximumSumRightSubarray, maximumSumCrossingSubarray);
}

public static void main(String[] args) {
int n=0;
   System.out.println("Enter the value of n: ");
   Scanner sc = new Scanner(System.in);
   n=sc.nextInt();
   sc.close();
   int arr[]=new int[n];
       for(int i =0; i<n; i++){
       Random random = new Random();
int randomInt = random.nextInt(2*n) - n;
// int randomInt = (int)(n * Math.random());
arr[i]=randomInt;
}
for(int i =0; i<n; i++){
System.out.println(arr[i]);
}
  
   System.out.println("Maximum contiguous sum is "+
                                       maxSumSubarray(arr, 0, n-1));
/*int a[] = {6, 5, -2, 3, -1, -2, 0};
System.out.println(maxSumSubarray(a, 0, 6));*/
}
}

Output:

Enter the value of n: ANÚNA Maximum contiguous sum is 6

I hope you like the code and if you like please give it thumb's up.

Thanks

Add a comment
Know the answer?
Add Answer to:
write a java program Accept a positive integer n from keyboard and then create an array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Matlab Write a program which will: a. Accept two numbers from the user m and...

    Using Matlab Write a program which will: a. Accept two numbers from the user m and n b. Define an array with the dimensions a(n,m) and fill it with random numbers in the range of -100 to 100 inclusive. c. Provide the user the following menu from which he can select: 1. Sum of all elements in the array. Print the result. 2. Sum of the element in a row that he’ll specify. Print the result. 3. Sum of the...

  • Write a Java program that does the following. a. Declare an integer 2D array with 5...

    Write a Java program that does the following. a. Declare an integer 2D array with 5 rows and 5 columns. b. Initialize the array's elements to random integers between 1 and 10 (inclusive). c. Display all the elements in the 2D array as a table of rows and columns. d. Display the row index and column index of all the even integers in the 2D array. e. Display the sum of first row's elements.

  • using C++ Write a program that: a) Inputs an integer n from the keyboard where n<=100....

    using C++ Write a program that: a) Inputs an integer n from the keyboard where n<=100. If n is out of range then print out an error message and ask for another input. This process repeats until a valid value for n is obtained. b) Inputs two 1D arrays of doubles A and B (of size n) from the keyboard. c) Inputs an integer k (from 1 to 3) from the keyboard. d) If k = 1 then it calculates...

  • Write a C++ program - create a 1-d array. Its data type is integer. It has...

    Write a C++ program - create a 1-d array. Its data type is integer. It has 10 elements. - initialize this array by 10 integer numbers. Five elements of the array are positive, and others are negative. - use a while loop to manipulate the array as below. - If the value of an element is positive, the value is doubled. Otherwise, the value is incremented by 3. - print the new value of each element. - use a for...

  • Write a C++ program that does the following : Accepts a positive integer ( n )...

    Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following 1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20 2. In the second function : Using recursion, Search for...

  • Using MATLAB 15. Write a program that: a. b. c. Accept two numbers n and m...

    Using MATLAB 15. Write a program that: a. b. c. Accept two numbers n and m in the range of 1-6 Define three arrays A, B and Cof size n by m. (n-# of rows, m-H of columns) Fill the elements of array A by A(ij)-itj i-5 d. Fill the elements of array B by B(ij)ij e. Fill the elements of array C with the average of the corresponding elements of A f. g. h. and B Print the results...

  • Answer should be in C programming language Write a program that will create an array of...

    Answer should be in C programming language Write a program that will create an array of 10 floating point numbers and take input 10 floats from keyboard. The program will print the position (index) of the maximum and minimum number among them

  • In Java: Create an array of Integers with 10 elements, loop (use a for loop) through...

    In Java: Create an array of Integers with 10 elements, loop (use a for loop) through the array of integers printing their values to the screen. Create an array of Strings with 10 elements, loop (use a for loop) through the array of Strings printing their values to the screen. Finally, create an ArrayList of Integers. Use the loop from part 1 above and add each integer as you print it to the ArrayList as well, then start a final...

  • Write a C PROGRAM that will read in 10 floating-point numbers from the keyboard and load...

    Write a C PROGRAM that will read in 10 floating-point numbers from the keyboard and load them into an array. Add up all the values and store the answer in a variable. Find the largest and smallest number in the array. Put each into its own variable. Print to the screen the sum, largest value, and smallest value. Copy the array to a second array in reverse order. Print out the second array. Read in 20 integer numbers, all in...

  • Part A Write a Java program that reads an integer n from the keyboard and loops...

    Part A Write a Java program that reads an integer n from the keyboard and loops until −13 ≤ n ≤ 13 is successfully entered. A do-while loop is advised. Part B Write a Java program that reads an integer n from the keyboard and prints the corresponding value n!. [This is n factorial]. You must verify that the input integer satisfies the constraint 0 ≤ n ≤ 13; keep looping until the constraint is satisfied.   Will give thumbs up...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT