Maximize sum of max and min of each of K Arrays obtained by dividing given Array into given sizes
Given two arrays, arr[] of N size and div[] of size K. Divide arr[] into K different arrays, each of div[i] size. The task is to find the total sum after maximizing the sum of maximum and minimum of each divided array.
Examples:
Input: arr[] = {3, 1, 7, 4}, div[] = {1, 3}, N = 4, K = 2
Output: 19
Explanation: Divide the array in the following way:
- {7}, sum of maximum and minimum = (7 + 7) = 14
- {1, 3, 4}, sum of maximum and minimum = (4 + 1) = 5
Total sum = 14 + 5 = 19.
Input: arr[] = {10, 12, 10, 12, 10, 12}, div[] = {3, 3}, N = 6, K = 2
Output: 44
Approach: Follow the below steps to solve the problem:
- Take a variable say count1 to count a number of 1s in div[].
- Sort both arrays, arr[] in descending order and div[] in ascending order.
- Take a variable say, ans to store the answer and another variable say t which represents from which index iteration is to be started in div[].
- Iterate the array till K, in every iteration add the elements to ans, and add that element again to ans while count1 is greater than 0 because arrays of size 1 while having the same element as maximum and minimum.
- Again iterate a loop from the Kth index to the end of the array. Add an element to ans and update the index.
- Return the ans.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the total sum after // maximizing the sum of maximum and // minimum of each divided array int maximizeSum( int arr[], int divi[], int N, int K) { // Variable to count 1s in divi[] int count1 = 0; for ( int i = 0; i < K; i++) { if (divi[i] == 1) { count1++; } } // Sort arr[] in descending order sort(arr, arr + N, greater< int >()); // Sort divi[] in ascending order sort(divi, divi + K); // Temporary variable to store // the count of 1s in the divi[] int t = count1; // Variable to store answer int ans = 0; // Iterate over the array till K for ( int i = 0; i < K; i++) { // Add the current element to ans ans += arr[i]; // If count1 is greater than 0, // decrement it by 1 and update the // ans by again adding the same element if (count1 > 0) { count1--; ans += arr[i]; } } // Traverse the array from Kth index // to the end for ( int i = K; i < N; i++) { // Update the index i += divi[t] - 2; // Add the value at that index to ans ans += arr[i]; t++; } // Return ans return ans; } // Driver Code int main() { int arr[] = { 3, 1, 7, 4 }; int divi[] = { 1, 3 }; int N = sizeof (arr) / sizeof (arr[0]); int K = sizeof (divi) / sizeof (divi[0]); cout << maximizeSum(arr, divi, N, K); return 0; } |
Java
// Java code for the above approach import java.util.Arrays; import java.util.Collections; class GFG { // Function to find the total sum after // maximizing the sum of maximum and // minimum of each divided array static int maximizeSum( int arr[], int divi[], int N, int K) { // Variable to count 1s in divi[] int count1 = 0 ; for ( int i = 0 ; i < K; i++) { if (divi[i] == 1 ) { count1++; } } // Sort arr[] in descending order Arrays.sort(arr); reverse(arr); // Sort divi[] in ascending order Arrays.sort(divi); // Temporary variable to store // the count of 1s in the divi[] int t = count1; // Variable to store answer int ans = 0 ; // Iterate over the array till K for ( int i = 0 ; i < K; i++) { // Add the current element to ans ans += arr[i]; // If count1 is greater than 0, // decrement it by 1 and update the // ans by again adding the same element if (count1 > 0 ) { count1--; ans += arr[i]; } } // Traverse the array from Kth index // to the end for ( int i = K; i < N; i++) { // Update the index i += divi[t] - 2 ; // Add the value at that index to ans ans += arr[i]; t++; } // Return ans return ans; } public static void reverse( int [] array) { // Length of the array int n = array.length; // Swapping the first half elements with last half // elements for ( int i = 0 ; i < n / 2 ; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1 ]; // Assigning the last half to the first half array[n - i - 1 ] = temp; } } // Driver Code public static void main(String[] args) { int arr[] = { 3 , 1 , 7 , 4 }; int divi[] = { 1 , 3 }; int N = arr.length; int K = divi.length; System.out.println(maximizeSum(arr, divi, N, K)); } } // This code is contributed by Potta Lokesh |
Python3
# Python program for the above approach # Function to find the total sum after # maximizing the sum of maximum and # minimum of each divided array def maximizeSum(arr, divi, N, K): # Variable to count 1s in divi[] count1 = 0 for i in range (K): if (divi[i] = = 1 ): count1 + = 1 # Sort arr[] in descending order arr.sort() arr.reverse() # Sort divi[] in ascending order divi.sort() # Temporary variable to store # the count of 1s in the divi[] t = count1 # Variable to store answer ans = 0 # Iterate over the array till K for i in range (K): # Add the current element to ans ans + = arr[i] # If count1 is greater than 0, # decrement it by 1 and update the # ans by again adding the same element if (count1 > 0 ): count1 - = 1 ans + = arr[i] # Traverse the array from Kth index # to the end i = K while (i < N): # Update the index i + = divi[t] - 2 # Add the value at that index to ans ans + = arr[i] t + = 1 i + = 1 # Return ans return ans # Driver Code arr = [ 3 , 1 , 7 , 4 ] divi = [ 1 , 3 ] N = len (arr) K = len (divi) print (maximizeSum(arr, divi, N, K)) # This code is contributed by gfgking |
C#
// C# code for the above approach using System; public class GFG { // Function to find the total sum after // maximizing the sum of maximum and // minimum of each divided array static int maximizeSum( int []arr, int []divi, int N, int K) { // Variable to count 1s in divi[] int count1 = 0; for ( int i = 0; i < K; i++) { if (divi[i] == 1) { count1++; } } // Sort arr[] in descending order Array.Sort(arr); reverse(arr); // Sort divi[] in ascending order Array.Sort(divi); // Temporary variable to store // the count of 1s in the divi[] int t = count1; // Variable to store answer int ans = 0; // Iterate over the array till K for ( int i = 0; i < K; i++) { // Add the current element to ans ans += arr[i]; // If count1 is greater than 0, // decrement it by 1 and update the // ans by again adding the same element if (count1 > 0) { count1--; ans += arr[i]; } } // Traverse the array from Kth index // to the end for ( int i = K; i < N; i++) { // Update the index i += divi[t] - 2; // Add the value at that index to ans ans += arr[i]; t++; } // Return ans return ans; } public static void reverse( int [] array) { // Length of the array int n = array.Length; // Swapping the first half elements with last half // elements for ( int i = 0; i < n / 2; i++) { // Storing the first half elements temporarily int temp = array[i]; // Assigning the first half to the last half array[i] = array[n - i - 1]; // Assigning the last half to the first half array[n - i - 1] = temp; } } // Driver Code public static void Main( string [] args) { int []arr = { 3, 1, 7, 4 }; int []divi = { 1, 3 }; int N = arr.Length; int K = divi.Length; Console.WriteLine(maximizeSum(arr, divi, N, K)); } } // This code is contributed by AnkThon |
Javascript
<script> // JavaScript program for the above approach // Function to find the total sum after // maximizing the sum of maximum and // minimum of each divided array const maximizeSum = (arr, divi, N, K) => { // Variable to count 1s in divi[] let count1 = 0; for (let i = 0; i < K; i++) { if (divi[i] == 1) { count1++; } } // Sort arr[] in descending order arr.sort(); arr.reverse(); // Sort divi[] in ascending order divi.sort(); // Temporary variable to store // the count of 1s in the divi[] let t = count1; // Variable to store answer let ans = 0; // Iterate over the array till K for (let i = 0; i < K; i++) { // Add the current element to ans ans += arr[i]; // If count1 is greater than 0, // decrement it by 1 and update the // ans by again adding the same element if (count1 > 0) { count1--; ans += arr[i]; } } // Traverse the array from Kth index // to the end for (let i = K; i < N; i++) { // Update the index i += divi[t] - 2; // Add the value at that index to ans ans += arr[i]; t++; } // Return ans return ans; } // Driver Code let arr = [3, 1, 7, 4]; let divi = [1, 3]; let N = arr.length let K = divi.length document.write(maximizeSum(arr, divi, N, K)); // This code is contributed by rakeshsahni </script> |
Output
19
Time Complexity: O(NlogN), time required for sorting
Auxiliary Space: O(1)
Please Login to comment...