Write a program to find the Mini-Max Sum of (n-1) elements of the given array.
Example 1:
Input: arr[] = {1,5,3,4,2}
Output: 10 14
Approach 1:
Java
import java.math.BigInteger;public class MinMax {public static void main(String[] args) {int arr[] = { 1,5,3,4,2};miniMaxSum(arr);}static void miniMaxSum(int[] arr) {BigInteger min = new BigInteger("0");BigInteger max = new BigInteger("0");for (int i = 0; i < 5; i++) {int c = arr[i];arr[i] = 0;BigInteger sum = new BigInteger("0");for (int k = 0; k < 5; k++) {sum = sum.add(BigInteger.valueOf(arr[k]));}if (i == 0) {min = sum;max = sum;}if (min.compareTo(sum) == 1)min = sum;if (sum.compareTo(max) == 1)max = sum;arr[i] = c;}System.out.println(min + " " + max);}}//Time Complexity: O(n^2)
C++
#include <bits/stdc++.h>using namespace std;//Function to find min sum and max sum// of n-1 elements in arrayvoid miniMaxSum(long long int arr[],int n){long long int min1,max1;for(long long int i=0;i<n;i++){long long int sum=0;long long int temp=arr[i];arr[i]=0;for(int j=0;j<n;j++)sum+=arr[j];if(i==0){min1=sum;max1=sum;}if(sum<min1)min1=sum;if(sum>max1)max1=sum;arr[i]=temp;}cout<<min1<<" "<<max1<<"\n";}int main(){long long int arr[]={1,5,3,4,2};long long int n=sizeof(arr)/sizeof(arr[0]);miniMaxSum(arr,n);return 0;}//Time Complexity: O(n^2)//Space Complexity:O(1)
Approach 2:
Java
import java.math.BigInteger;import java.util.Arrays;public class MinMax {public static void main(String[] args) {int arr[] = { 1,5,3,4,2 };miniMaxSum(arr);}static void miniMaxSum(int[] arr) {Arrays.sort(arr);BigInteger min = BigInteger.valueOf(arr[0]);BigInteger max = BigInteger.valueOf(arr[arr.length - 1]);BigInteger sum = BigInteger.valueOf(arr[0]);for (int i = 1; i < arr.length; i++) {sum = sum.add(BigInteger.valueOf(arr[i]));}System.out.println((sum.subtract(max)) + " " + (sum.subtract(min)));}}// Time Complexity: O(nlog(n))
C++
#include <bits/stdc++.h>using namespace std;//Function to find min sum and max sum// of n-1 elements in arrayvoid miniMaxSum(long long int arr[],long long int n){long long int min1,max1;sort(arr,arr+n);min1=arr[0];max1=arr[n-1];long long int sum=0;for(long long int i=0;i<n;i++)sum+=arr[i];cout<<sum-max1<<" "<<sum-min1<<"\n";}int main(){long long int arr[]={1,5,3,4,2};long long int n=sizeof(arr)/sizeof(arr[0]);miniMaxSum(arr,n);return 0;}//Time Complexity: O(nlog(n))//Space Complexity:O(1)
Approach 3:
Java
public class MinMax {public static void main(String[] args) {int arr[] = { 1, 2, 3, 4, 5 };miniMaxSum(arr);}static void miniMaxSum(int[] arr) {BigInteger min = BigInteger.valueOf(arr[0]);BigInteger max = BigInteger.valueOf(arr[0]);BigInteger sum = BigInteger.valueOf(arr[0]);for (int i = 1; i < arr.length; i++) {sum = sum.add(BigInteger.valueOf(arr[i]));if (min.compareTo(BigInteger.valueOf(arr[i])) == 1)min = BigInteger.valueOf(arr[i]);if (BigInteger.valueOf(arr[i]).compareTo(max) == 1)max = BigInteger.valueOf(arr[i]);}System.out.println((sum.subtract(max)) + " " + (sum.subtract(min)));}}// Time Complexity : O(n)
C++
#include <bits/stdc++.h>using namespace std;//Function to find min sum and max sum// of n-1 elements in arrayvoid miniMaxSum(long long int arr[],long long int n){long long int min1=arr[0],max1=arr[0];for(int i=1;i<n;i++){if(arr[i]<min1)min1=arr[i];if(arr[i]>max1)max1=arr[i];}long long int sum=0;for(long long int i=0;i<n;i++)sum+=arr[i];cout<<sum-max1<<" "<<sum-min1<<"\n";}int main(){long long int arr[]={1,5,3,4,2};long long int n=sizeof(arr)/sizeof(arr[0]);miniMaxSum(arr,n);return 0;}//Time Complexity: O(n)//Space Complexity:O(1)
No comments:
Post a Comment