- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum Possible Product in Array after performing given Operations in C++
In this tutorial, we will be discussing a program to find maximum Possible Product in Array after performing given Operations
For this we will be provided with an array of size N. Our task is to perform N-1 operations (changing a[j] → a[i]*a[j] and remove a[i] value or just remove the value of a[i] (only once)) such that the remaining values are the maximum ones only.
Example
#include <bits/stdc++.h> using namespace std; //printing operations void MaximumProduct(int a[], int n) { int cntneg = 0; int cntzero = 0; int used[n] = { 0 }; int pos = -1; for (int i = 0; i < n; ++i) { if (a[i] == 0) { used[i] = 1; cntzero++; } if (a[i] < 0) { cntneg++; if (pos == -1 || abs(a[pos]) > abs(a[i])) pos = i; } } if (cntneg % 2 == 1) used[pos] = 1; if (cntzero == n || (cntzero == n - 1 && cntneg == 1)) { for (int i = 0; i < n - 1; ++i) cout << 1 << " " << i + 1 << " " << i + 2 << endl; return; } int lst = -1; for (int i = 0; i < n; ++i) { if (used[i]) { if (lst != -1) cout << 1 << " " << lst + 1 << " " << i + 1 << endl; lst = i; } } if (lst != -1) cout << 2 << " " << lst + 1 << endl; lst = -1; for (int i = 0; i < n; ++i) { if (!used[i]) { if (lst != -1) cout << 1 << " " << lst + 1 << " " << i + 1 << endl; lst = i; } } } int main() { int a[] = { 5, -2, 0, 1, -3 }; int n = sizeof(a) / sizeof(a[0]); MaximumProduct(a, n); return 0; }
Output
2 3 1 1 2 1 2 4 1 4 5
- Related Articles
- Maximum count of equal numbers in an array after performing given operations in C++
- Final string after performing given operations in C++
- Maximum value in an array after m range increment operations in C++
- Program to find minimum possible maximum value after k operations in python
- Performing mathematical operations in MySQL IF then ELSE is possible?
- Reduce a number to 1 by performing given operations in C++
- Program to find maximum score from performing multiplication operations in Python
- Maximum possible middle element of the array after deleting exactly k elements in C++
- Performing power operations on an array of numbers in JavaScript
- Program to construct Maximum Stack with given operations in C++
- Print modified array after multiple array range increment operations in C Program.
- Maximum product subset of an array in C++
- Program to find expected sum of subarrays of a given array by performing some operations in Python
- Program to check final answer by performing given stack operations in Python
- Maximum GCD from Given Product of Unknowns in C++

Advertisements