Question

In: Computer Science

Program in java 1- Implement an algorithms to calculate the sum of all numbers in an...

Program in java

1- Implement an algorithms to calculate the sum of all numbers in an array.  

2- Implement an algorithm to determine if an array has all unique integer values.

3- Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

Return the running sum of nums.

Example 1:

Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].

Solutions

Expert Solution

/*If you have any query do comment in the comment section else like the solution*/

import java.util.*;
public class ArrayUtil{
     public static void main(String []args){
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter size of array : ");
        int n = sc.nextInt();
        int arr[] = new int[n];
        for(int i=0;i<n;i++) {
            arr[i] = sc.nextInt();
        }
        runningSum(arr, n);
        if(isUnique(arr, n)) {
            System.out.println("Array is unique");
        } else {
            System.out.println("Array is not unique");
        }
     }
     
     public static void runningSum(int arr[], int n) {
        int i, sum = 0;
        int prefixSum[] = new int[n];
        prefixSum[0] = arr[0];
        for(i=1;i<n;i++) {
            prefixSum[i] = arr[i] + prefixSum[i-1];
        }
        for(i=0;i<n;i++) {
            System.out.print(prefixSum[i] + " ");
        }
        System.out.println();
     }
     
     public static boolean isUnique(int arr[], int n) {
        int i, j;
        for(i=0;i<n;i++) {
            for(j=i+1;j<n;j++) {
                if(arr[i] == arr[j]) {
                    return false;
                }
            }
        }
        return true;
     }
}

Input:

1 2 3 4

Output:

1 3 6 10 
Array is unique

Related Solutions

JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
Java program to print all the powers of 2 below a certain number. Calculate sum, accept...
Java program to print all the powers of 2 below a certain number. Calculate sum, accept upper limit and make sure sum variable is long type.   Run- Enter the upper limit: 100 5 + 8 + 9 + 11 + 20 + 32 + 30 = 115
Modify the code below to implement the program that will sum up 1000 numbers using 5...
Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will sum up numbers from 801 - 1000 Make main thread wait for other threads to finish execution and sum up all the results. Display the total to the user. #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define N_OF_THREADS 4 void * print(void...
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Design a program in JAVA that allows you to experiment with different sort algorithms. The algorithms...
Design a program in JAVA that allows you to experiment with different sort algorithms. The algorithms are shell sort and quick sort. Assume that input data is stored in a text file. Experimenting with a prototype data (integers from 1 to 10) to ensure that your implementation works correctly and the results match expectations. The program will sort what is in the text file and print the amount of comparisons and exchanges for both algorithms.
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored...
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored in a 16-bit integers array. The size of the array is provided in a separate variable. Use the following design: Sum = 0 FOR i = 0 TO SizeOfArray - 1     IF Arr[i] % 2 = 1           Sum = Sum + Arr[i]     END IF END FOR
Write a loop that will calculate the sum of all even numbers from 2 to 30...
Write a loop that will calculate the sum of all even numbers from 2 to 30 ( including 30) store the result in the variable called thirtySum. Declare and initialize all variables. Answer using programming in c.
Objective: Write a C++ -program that will implement 4 Memory Management algorithms Algorithms: A) Best-Fit B)...
Objective: Write a C++ -program that will implement 4 Memory Management algorithms Algorithms: A) Best-Fit B) First-Fit C) Next-Fit D) Worst-Fit Your program must do the following: 1. Program Input:             User will input to the program a. Main Memory information, including i. The Number of Memory partitions. ii. The Size of each memory partition. b. Process information (assign a unique identifier to each job) i. User will input the number of processes ii. Memory requirements for each process/job 2....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT