Wednesday, November 20, 2024
Google search engine
HomeData Modelling & AIGenerate an N-length array having maximum element minimized and sum of array...

Generate an N-length array having maximum element minimized and sum of array elements divisible by K

Given two positive integers N and K, the task is to minimize the maximum element of the array formed such that the sum of array elements is positive and divisible by K.

Examples: 

Input: N = 4, K = 50
Output: 13
Explanation The generated array is {12, 13, 12, 13}. Sum of the array is 50, which is divisible by K (= 50). Maximum element present in the array is 13, which is minimum possible.

Input: N = 3, K = 3
Output: 1
 

 

Approach: The given problem can be solved on the basis of the following observations: 

From the above observations, the minimized maximum value of the constructed array is the ceil value of (K/N).

Below is the implementation of the above approach: 

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to minimize the maximum
// element present in an N-length array
// having sum of elements divisible by K
int minimumValue(int N, int K)
{
    // Return the ceil value of (K / N)
    return ceil((double)K / (double)N);
}
 
// Driver Code
int main()
{
    int N = 4, K = 50;
    cout << minimumValue(N, K);
 
    return 0;
}


Java




// Java program for the above approach
class GFG{
 
// Function to minimize the maximum
// element present in an N-length array
// having sum of elements divisible by K
static int minimumValue(int N, int K)
{
    
    // Return the ceil value of (K / N)
    return(int)Math.ceil((double)K / (double)N);
}
 
// Driver code
public static void main(String[] args)
{
    int N = 4, K = 50;
      
    System.out.print(minimumValue(N, K));
}
}
 
// This code is contributed by code_hunt.


Python3




# Python3 program for the above approach
import math
 
# Function to minimize the maximum
# element present in an N-length array
# having sum of elements divisible by K
 
 
def minimumValue(N, K):
    # Return the ceil value of (K / N)
    return math.ceil(K / N)
 
 
# Driver Code
N = 4
K = 50
print(minimumValue(N, K))
 
# This code is contributed by abhinavjain194


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to minimize the maximum
// element present in an N-length array
// having sum of elements divisible by K
static int minimumValue(int N, int K)
{
   
    // Return the ceil value of (K / N)
    return(int)Math.Ceiling((double)K / (double)N);
}
 
// Driver Code
public static void Main()
{
    int N = 4, K = 50;
     
    Console.WriteLine(minimumValue(N, K));
}
}
 
// This code is contributed by ukasp


Javascript




<script>
 
// javascript program for the above approach
 
// Function to minimize the maximum
// element present in an N-length array
// having sum of elements divisible by K
function minimumValue(N, K)
{
     
    // Return the ceil value of (K / N)
    return Math.ceil(K / N);
}
 
// Driver Code
 
    let N = 4, K = 50;
       
    document.write(minimumValue(N, K));
 
</script>


Output: 

13

 

Time Complexity: O(1)
Auxiliary Space: O(1)

Last Updated :
19 May, 2021
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

RELATED ARTICLES

Most Popular

Recent Comments