Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Minimize the Maximum Value in Two Arrays
Next article icon

Minimize the Maximum Value in Two Arrays

Last Updated : 08 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two empty arrays arr1[] and arr2[]. You need to add positive integers to these arrays such that they satisfy the following conditions:

  • arr1 contains k1 distinct positive integers, each of which is not divisible by d1.
  • arr2 contains k2 distinct positive integers, each of which is not divisible by d2.
  • No integer is present in both arr1 and arr2.

Given the values of d1, d2, k1, and k2, the task is to return the minimum possible maximum value that can be present in either arr1 or arr2.

Example:

Input: d1 = 2, d2 = 7, k1 = 1, k2 = 3
Output: 4
Explanation:
We can distribute the first 4 natural numbers into arr1 and arr2. arr1 = [1] and arr2 = [2,3,4]. We can see that both arrays satisfy all the conditions. Since the maximum value is 4, we return it.

Input: d1 = 3, d2 = 5, k1 = 2, k2 = 1
Output: 3
Explanation:
Here arr1 = [1,2], and arr2 = [3] satisfy all conditions. Since the maximum value is 3, we return it.

Approach:

When you have to minimise/maximise, there's a very high chance you can binary search on the answer. Here if we know a number X provides us enough numbers to fulfill both conditions, then we can simply look for a number smaller than X which might satisfy the condition, if not we will look for a larger number. Now the question is to just check if a number X satisfies the condition or not.

Now to check if a number X satisfies the condition or not, we will first calculate how many numbers are there from 1...X that are divisible by d1 and d2. We can find out this by dividing X by d1, i.e. divisibleByD1 = X / d1. Then we can do the same for d2, i.e. divisibleByD2 = X / d2. Now the number of integers that we can add to the first array are elements1 = X - divisibleByD1 and number of elements we can add to the second array are elements2 = X - divisibleByD2. If you notice here there might be an overlap. For e.g. d1 = 4 and d2 = 6 and X = 13. Here divisibleByD1 = 3 { 4, 8, 12 } and divisibleByD2 = 2 { 6, 12 }. These are the elements that we cannot use in either array1 or array2. But there's an overlap of 12 which we can't use in either of the arrays but we've calculated it twice. To solve this, we will also calculate the number of elements divisible by both d1 and d2 (which is number of elements divisible by lcm(d1, d2)) (this finds out the overlapping elements). Now X will satisfy the condition if k1 <= elements1 && k2 <= elements2 && k1 + k2 <= X - (X / LCM).

Steps-by-step approach:

  • Calculate LCM: Multiply d1 and d2 and divide by their greatest common divisor (GCD).
  • Iterate Multiples: Go through all multiples of the LCM up to the sum of uc1 and uc2.
  • Count Elements: For each multiple, count how many elements are divisible by d1 and d2.
  • Update Minimum: If the total count is less than the current minimum, update the minimum.
  • Return Minimum: Return the minimum number of elements found.

Below is the implementation of the above approach:

C++
#include <algorithm> #include <iostream> #include <vector>  using namespace std;  // Function to find the minimum number of elements in the // set int minimizeSet(int d1, int d2, int k1, int k2) {     // Calculate the least common multiple (LCM) of d1 and     // d2     int lcm = d1 * d2 / __gcd(d1, d2);      // Initialize the minimum number of elements to the sum     // of k1 and k2     int minElements = k1 + k2;      // Iterate over all multiples of the LCM less than or     // equal to k1 + k2     for (int i = 1; i * lcm <= k1 + k2; ++i) {         // Calculate the number of elements divisible by d1         // and d2         int elementsDivisibleByDivisor1 = (k1 + i - 1) / i;         int elementsDivisibleByDivisor2 = (k2 + i - 1) / i;          // Update the minimum number of elements if         // necessary         minElements             = min(minElements,                   elementsDivisibleByDivisor1                       + elementsDivisibleByDivisor2 - i);     }      return minElements; }  int main() {      int d1 = 2, d2 = 7, k1 = 1, k2 = 3;     int minElements = minimizeSet(d1, d2, k1, k2);      cout << "Minimum number of elements: " << minElements          << endl;      return 0; } 
Java
import java.util.*;  public class Main {     // Function to find the minimum number of elements in     // the set     static int minimizeSet(int d1, int d2, int k1, int k2)     {         // Calculate the least common multiple (LCM) of d1         // and d2         int lcm = d1 * d2 / gcd(d1, d2);          // Initialize the minimum number of elements to the         // sum of k1 and k2         int minElements = k1 + k2;          // Iterate over all multiples of the LCM less than         // or equal to k1 + k2         for (int i = 1; i * lcm <= k1 + k2; ++i) {             // Calculate the number of elements divisible by             // d1 and d2             int elementsDivisibleByDivisor1                 = (k1 + i - 1) / i;             int elementsDivisibleByDivisor2                 = (k2 + i - 1) / i;              // Update the minimum number of elements if             // necessary             minElements = Math.min(                 minElements,                 elementsDivisibleByDivisor1                     + elementsDivisibleByDivisor2 - i);         }          return minElements;     }      // Function to calculate the greatest common divisor     // (GCD)     static int gcd(int a, int b)     {         return b == 0 ? a : gcd(b, a % b);     }      public static void main(String[] args)     {         int d1 = 2, d2 = 7, k1 = 1, k2 = 3;         int minElements = minimizeSet(d1, d2, k1, k2);          System.out.println("Minimum number of elements: "                            + minElements);     } }  // This code is contributed by Shivam Gupta 
Python
import math  # Function to find the minimum number of elements in the set def minimizeSet(d1, d2, k1, k2):     # Calculate the least common multiple (LCM) of d1 and d2     lcm = d1 * d2 // math.gcd(d1, d2)      # Initialize the minimum number of elements to the sum of k1 and k2     minElements = k1 + k2      # Iterate over all multiples of the LCM less than or equal to k1 + k2     for i in range(1, (k1 + k2) // lcm + 1):         # Calculate the number of elements divisible by d1 and d2         elementsDivisibleByDivisor1 = (k1 + i - 1) // i         elementsDivisibleByDivisor2 = (k2 + i - 1) // i          # Update the minimum number of elements if necessary         minElements = min(minElements,                           elementsDivisibleByDivisor1 + elementsDivisibleByDivisor2 - i)      return minElements  if __name__ == "__main__":     d1 = 2     d2 = 7     k1 = 1     k2 = 3     minElements = minimizeSet(d1, d2, k1, k2)      print(f"Minimum number of elements: {minElements}") # This code is contributed by Ayush Mishra 
JavaScript
// Function to find the greatest common divisor (GCD) of two numbers function gcd(a, b) {     if (b === 0) return a;     return gcd(b, a % b); }  // Function to find the minimum number of elements in the set function minimizeSet(d1, d2, k1, k2) {     // Calculate the least common multiple (LCM) of d1 and d2     const lcm = (d1 * d2) / gcd(d1, d2);      // Initialize the minimum number of elements to the sum of k1 and k2     let minElements = k1 + k2;      // Iterate over all multiples of the LCM less than or equal to k1 + k2     for (let i = 1; i * lcm <= k1 + k2; ++i) {         // Calculate the number of elements divisible by d1 and d2         const elementsDivisibleByDivisor1 = Math.ceil(k1 / i);         const elementsDivisibleByDivisor2 = Math.ceil(k2 / i);          // Update the minimum number of elements if necessary         minElements = Math.min(minElements, elementsDivisibleByDivisor1 + elementsDivisibleByDivisor2 - i);     }      return minElements; }  // Driver code const d1 = 2, d2 = 7, k1 = 1, k2 = 3; const minElements = minimizeSet(d1, d2, k1, k2);  console.log("Minimum number of elements:", minElements); 

Output
Minimum number of elements: 4 

Time complexity: O(k1+ k2)
Auxiliary Space: O(1)


Next Article
Minimize the Maximum Value in Two Arrays

V

vforvikram
Improve
Article Tags :
  • DSA
  • Arrays
  • Goldman Sachs
  • Binary Search
Practice Tags :
  • Goldman Sachs
  • Arrays
  • Binary Search

Similar Reads

    Maximum Sum Path in Two Arrays
    Given two sorted arrays having some elements in common. Find the sum of the maximum sum path to reach from the beginning of any array to the end of any of the two arrays. We can switch from one array to another array only at common elements. Note: The common elements do not have to be at the same in
    14 min read
    Minimize product of maximum numbers in two Array using swaps
    Given 2 arrays arr1[] and arr2[] of size N the task is to find the minimum product of maximums of both arrays by performing a swapping operation any number of times. In one swap operation, any index of the array is selected and arr1[index] and arr2[index] are swapped. Examples: Input: N = 2, arr1[2]
    4 min read
    Find the maximum possible value of the minimum value of modified array
    Given an array of size N and a number S . The task is to modify the given array such that: The difference between the sum of the array elements before and after modification is exactly equal to S.Modified array elements should be non-negative.The minimum value in the modified array must be maximized
    9 min read
    Minimize product of maximum numbers in two Array using swaps | Set 2
    Given two arrays arr1[] and arr2[] of N size each. Select any index i and swap the element arr1[i] and arr2[i]. By applying this operation any number of times(possibly none) find the minimum possible product of maximum elements from array arr1[] and arr2[]. Examples: Input: N = 6arr1[] = {1, 2, 6, 5
    6 min read
    Minimum total sum from the given two arrays
    Given two arrays A[] and B[] of N positive integers and a cost C. We can choose any one element from each index of the given arrays i.e., for any index i we can choose only element A[i] or B[i]. The task is to find the minimum total sum of selecting N elements from the given two arrays and if we are
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences