Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
Count pairs from an array with absolute difference not less than the minimum element in the pair
Next article icon

C++ Program for Minimum product pair an array of positive Integers

Last Updated : 02 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array of positive integers. We are required to write a program to print the minimum product of any two numbers of the given array.
Examples: 

Input : 11 8 5 7 5 100 Output : 25  Explanation : The minimum product of any  two numbers will be 5 * 5 = 25.  Input : 198 76 544 123 154 675  Output : 7448 Explanation : The minimum product of any  two numbers will be 76 * 123 = 7448.

Simple Approach : A simple approach will be to run two nested loops to generate all possible pair of elements and keep track of the minimum product. 
Time Complexity: O( n * n) 
Auxiliary Space: O( 1 )
Better Approach: An efficient approach will be to first sort the given array and print the product of first two numbers, sorting will take O(n log n). Answer will be then a[0] * a[1] .

C++
// C++ program to calculate minimum // product of a pair #include <bits/stdc++.h> using namespace std;  // Function to calculate minimum product // of pair int printMinimumProduct(int arr[], int n) {     //Sort the array     sort(arr,arr+n);          // Returning the product of first two numbers     return arr[0] * arr[1]; }  // Driver program to test above function int main() {     int a[] = { 11, 8 , 5 , 7 , 5 , 100 };     int n = sizeof(a) / sizeof(a[0]);     cout << printMinimumProduct(a,n);     return 0; }  // This code is contributed by Pushpesh Raj 

Output
25


Time Complexity: O( n * log(n)) 
Auxiliary Space: O( 1 )
Best Approach: The idea is linearly traverse given array and keep track of minimum two elements. Finally return product of two minimum elements.
Below is the implementation of above approach. 

C++
// C++ program to calculate minimum // product of a pair #include <bits/stdc++.h> using namespace std;  // Function to calculate minimum product // of pair int printMinimumProduct(int arr[], int n) {     // Initialize first and second     // minimums. It is assumed that the     // array has at least two elements.     int first_min = min(arr[0], arr[1]);     int second_min = max(arr[0], arr[1]);      // Traverse remaining array and keep     // track of two minimum elements (Note     // that the two minimum elements may     // be same if minimum element appears     // more than once)     // more than once)     for (int i=2; i<n; i++)     {        if (arr[i] < first_min)        {           second_min = first_min;           first_min = arr[i];        }        else if (arr[i] < second_min)           second_min = arr[i];     }      return first_min * second_min; }  // Driver program to test above function int main() {     int a[] = { 11, 8 , 5 , 7 , 5 , 100 };     int n = sizeof(a) / sizeof(a[0]);     cout << printMinimumProduct(a,n);     return 0; } 

Output
25


Time Complexity: O(n) 
Auxiliary Space: O(1) Please refer complete article on Minimum product pair an array of positive Integers for more details!


Next Article
Count pairs from an array with absolute difference not less than the minimum element in the pair
author
kartik
Improve
Article Tags :
  • Searching
  • Sorting
  • C++ Programs
  • C++
  • DSA
  • Arrays
Practice Tags :
  • CPP
  • Arrays
  • Searching
  • Sorting

Similar Reads

  • C++ Program for Minimum product subset of an array
    Given an array a, we have to find the minimum product possible with the subset of elements present in the array. The minimum product can be a single element also. Examples:  Input : a[] = { -1, -1, -2, 4, 3 } Output : -24 Explanation : Minimum product will be ( -2 * -1 * -1 * 4 * 3 ) = -24 Input : a
    3 min read
  • Minimum possible sum of array elements after performing the given operation
    Given an array arr[] of positive integers and an integer x, the task is to minimize the sum of elements of the array after performing the given operation at most once. In a single operation, any element from the array can be divided by x (if it is divisible by x) and at the same time, any other elem
    8 min read
  • How to Find the Smallest Number in an Array in C++?
    In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma
    2 min read
  • C++ Program to Find closest number in array
    Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples: Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input :arr[] = {2, 5, 6, 7, 8, 8, 9};
    4 min read
  • Count pairs from an array with absolute difference not less than the minimum element in the pair
    Given an array arr[] consisting of N positive integers, the task is to find the number of pairs (arr[i], arr[j]) such that absolute difference between the two elements is at least equal to the minimum element in the pair. Examples: Input: arr[] = {1, 2, 2, 3}Output: 3Explanation:Following are the pa
    14 min read
  • Find k ordered pairs in array with minimum difference d
    Given an array arr[] and two integers K and D, the task is to find exactly K pairs (arr[i], arr[j]) from the array such that |arr[i] - arr[j]| ? D and i != j. If it is impossible to get such pairs then print -1. Note that a single element can only participate in a single pair.Examples: Input: arr[]
    6 min read
  • C++ Program for Find k pairs with smallest sums in two arrays
    Given two integer arrays arr1[] and arr2[] sorted in ascending order and an integer k. Find k pairs with smallest sums such that one element of a pair belongs to arr1[] and other element belongs to arr2[]Examples: Input : arr1[] = {1, 7, 11} arr2[] = {2, 4, 6} k = 3 Output : [1, 2], [1, 4], [1, 6] E
    7 min read
  • Kth smallest number in array formed by product of any two elements from two arrays
    Given two sorted arrays A[] and B[] consisting of N and M integers respectively, the task is to find the Kth smallest number in the array formed by the product of all possible pairs from array A[] and B[] respectively. Examples: Input: A[] = {1, 2, 3}, B[] = {-1, 1}, K = 4Output: 1Explanation: The a
    15+ min read
  • Smallest Pair Sum in an array
    Given an array of distinct integers arr[], the task is to find a pair which has the minimum sum and print the sum. Examples: Input: arr[] = {1, 2, 3} Output: 3 The pair (1, 2) will have the minimum sum pair i.e. 1 + 2 = 3 Input: arr[] = {3, 5, 6, 2} Output: 5 Approach: Find the minimum element from
    5 min read
  • Kth smallest pair and number of pairs less than a given pair (x, y)
    Given N pairs, the task is to find the Kth smallest pair and the number of pairs less than the given pair (x, y). Examples: Input: pairs[][] = {{23, 20}, {23, 10}, {23, 30}, {12, 35}, {12, 22}}, K = 3, (x, y) = (23, 20) Output: {23, 10} 3 Input: pairs[][] = {{23, 20}, {23, 10}, {23, 30}, {12, 35}, {
    10 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