📌  相关文章
📜  执行给定操作后阵列中的最大可能乘积

📅  最后修改于: 2021-06-26 09:15:23             🧑  作者: Mango

给定一个大小为N的数组。您可以在给定的数组上执行两种类型的操作,如下所述:

  1. 选择某个位置ij ,使(i不等于j) ,用a [i] * a [j]替换a [j]的值,然后从第i单元格中删除该数字。
  2. 选择某个位置i并从第i单元格中删除数字(此操作最多可以在任何时间点执行一次,而不必在开始时执行)。

任务是对阵列执行精确的N-1个操作,以使阵列中剩余的唯一数字最大可能。这个数字可能会很大,因此,除了打印它之外,还要打印导致该最大数字的操作序列。
输出应准确包含N-1行:

注意:数组被认为具有基于1的索引。
例子

Input : a[] = { 5, -2, 0, 1, -3 }
Output : 2 3
         1 1 2
         1 2 4
         1 4 5
Explanation: 
Step 1: a[3] is removed.
Step 2: a[2] = a[2]*a[1] = -10; a[1] is removed.
Step 3: a[4] = a[4]*a[2] = -10; a[2] is removed.
Step 4: a[5] = a[5]*a[4] = 30; a[4] is removed.
So, the maximum product is 30.

Input : a[] = { 0, 0, 0, 0 }
Output : 1 1 2
         1 2 3
         1 3 4

方法:问题中有几种情况。令数组中的零数为cntZero ,负元素的数为cntNeg 。还让maxNeg为数组中最大负元素的位置;如果数组中没有负元素,则为-1。
假设答案部分是答案中所有数字的乘积,删除的部分是第二种类型的操作中将删除的所有数字的乘积。
情况如下:

  1. 第一种情况是cntZero = 0cntNeg = 0时。然后答案部分是数组中所有数字的乘积。删除的部分是空的。
  2. 第二种情况是cntNeg为奇数时。然后答案部分是数组中所有数字(除所有零和a [maxNeg]之外)的乘积。删除的部分是所有零与a [maxNeg]的乘积
  3. 第三种情况是cntNeg为偶数。然后答案部分是数组中所有数字(全零除外)的乘积。删除的部分是数组中所有零的乘积(要小心cntNeg = 0cntZero = n )。

下面是上述想法的实现:

C++
// CPP program for maximum possible product
// with given array of numbers
#include 
using namespace std;
 
// Function that prints operations in each step
void MaximumProduct(int a[], int n)
{
    int cntneg = 0;
    int cntzero = 0;
     
    int used[n] = { 0 };
    int pos = -1;
 
    // count number of zeros and negative numbers
    for (int i = 0; i < n; ++i) {
        if (a[i] == 0) {
            used[i] = 1;
            cntzero++;
        }
         
        if (a[i] < 0) {
            cntneg++;
             
            // To get negative number which
            // is nearest to zero, that is maximum
            // negative number
            if (pos == -1 || abs(a[pos]) > abs(a[i]))
                pos = i;
        }
    }
     
    // if number of negative number are odd then
    // remove negative number at position pos
    if (cntneg % 2 == 1)
        used[pos] = 1;
 
    // initial condition
    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;
        }
    }
     
    // perform second type operation
    if (lst != -1)
        cout << 2 << " " << lst + 1 << endl;
 
    lst = -1;
     
    // for reamining numbers
    for (int i = 0; i < n; ++i) {
        // if it is not removed yet
        if (!used[i]) {
            if (lst != -1)
                cout << 1 << " " << lst + 1 << " "
                                    << i + 1 << endl;
            lst = i;
        }
    }
}
 
// Driver code
int main()
{
    int a[] = { 5, -2, 0, 1, -3 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    MaximumProduct(a, n);
 
    return 0;
}


Java
// Java program for maximum possible product
// with given array of numbers
class GFG {
 
// Function that prints operations in each step
static void MaximumProduct(int a[], int n) {
    int cntneg = 0;
    int cntzero = 0;
 
    int used[] = new int[n];
    int pos = -1;
     
    // count number of zeros and negative numbers
    for (int i = 0; i < n; ++i) {
        if (a[i] == 0)
        {
            used[i] = 1;
            cntzero++;
             
        }
         
        if (a[i] < 0) {
            cntneg++;
             
            // To get negative number which
            // is nearest to zero, that is maximum
            // negative number
            if (pos == -1 || Math.abs(a[pos]) > Math.abs(a[i])) {
                pos = i;
                 
            }
             
        }
         
    }
     
    // if number of negative number are odd then
    // remove negative number at position pos
    if (cntneg % 2 == 1) {
        used[pos] = 1;
         
    }
     
    // initial condition
    if (cntzero == n || (cntzero == n - 1 && cntneg == 1))
    {             
        for (int i = 0; i < n - 1; ++i) {
            System.out.println(1 + " " + (i + 1) + " "
                        + (i + 2));
             
        }
        return;
         
    }
     
    int lst = -1;
    for (int i = 0; i < n; ++i) {
        if (used[i] == 1) {
            if (lst != -1) {
                System.out.println(1 + " " + (lst + 1) + " "
                            + (i + 1));
                 
            }
            lst = i;
             
        }
         
    }
     
    // perform second type operations
    if (lst != -1) {
        System.out.println(2 + " " + (lst + 1));
         
    }
    lst = -1;
    // for reamining numbers
    for (int i = 0; i < n; ++i)
    {
        // if it is not removed yet
        if (used[i] != 1)
        {
            if (lst != -1)
            {
                System.out.println(1 + " " + (lst + 1) + " "
                            + (i + 1));
                 
            }
            lst = i;
        }
    }
     
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = {5, -2, 0, 1, -3};
    int n = a.length;
    MaximumProduct(a, n);
     
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program for maximum possible product
# with given array of numbers
 
# Function that prints operations
# in each step
def MaximumProduct(a, n):
    cntneg = 0
    cntzero = 0
     
    used = [0] * n
    pos = -1
 
    # count number of zeros and
    # negative numbers
    for i in range(n):
        if (a[i] == 0) :
            used[i] = 1
            cntzero += 1
         
        if (a[i] < 0):
            cntneg += 1
             
            # To get negative number which
            # is nearest to zero, that is maximum
            # negative number
            if (pos == -1 or abs(a[pos]) > abs(a[i])):
                pos = i
     
    # if number of negative number are odd then
    # remove negative number at position pos
    if (cntneg % 2 == 1):
        used[pos] = 1
 
    # initial condition
    if (cntzero == n or (cntzero == n - 1 and
                         cntneg == 1)):
        for i in range(n - 1):
            print (1, " ", i + 1, " ", i + 2)
        return
 
    lst = -1
    for i in range(n) :
        if (used[i]) :
            if (lst != -1):
                print (1, " ", lst + 1, " ", i + 1)
            lst = i
     
    # perform second type operation
    if (lst != -1):
        print (2, " ", lst + 1)
 
    lst = -1
     
    # for reamining numbers
    for i in range( n) :
         
        # if it is not removed yet
        if (not used[i]) :
            if (lst != -1):
                print (1, " ", lst + 1, " ", i + 1)
            lst = i
     
# Driver code
if __name__ == "__main__":
    a = [ 5, -2, 0, 1, -3 ]
 
    n = len(a)
 
    MaximumProduct(a, n)
 
# This code is contributed by ita_c


C#
// C# program for maximum possible product
// with given array of numbers
using System;
 
class GFG
{
     
// Function that prints
// operations in each step
static void MaximumProduct(int []a, int n)
{
    int cntneg = 0;
    int cntzero = 0;
 
    int []used = new int[n];
    int pos = -1;
     
    // count number of zeros
    // and negative numbers
    for (int i = 0; i < n; ++i)
    {
        if (a[i] == 0)
        {
            used[i] = 1;
            cntzero++;
             
        }
         
        if (a[i] < 0)
        {
            cntneg++;
             
            // To get negative number which
            // is nearest to zero, that is
            //  maximum negative number
            if (pos == -1 || Math.Abs(a[pos]) >
                                Math.Abs(a[i]))
            {
                pos = i;
                 
            }
             
        }
         
    }
     
    // if number of negative number are odd then
    // remove negative number at position pos
    if (cntneg % 2 == 1)
    {
        used[pos] = 1;
         
    }
     
    // initial condition
    if (cntzero == n || (cntzero == n - 1 &&
                                cntneg == 1))
    {        
        for (int i = 0; i < n - 1; ++i)
        {
            Console.WriteLine(1 + " " + (i + 1) + " "
                        + (i + 2));
        }
        return;
         
    }
     
    int lst = -1;
    for (int i = 0; i < n; ++i)
    {
        if (used[i] == 1)
        {
            if (lst != -1)
            {
                Console.WriteLine(1 + " " + (lst + 1) + " "
                            + (i + 1));
            }
            lst = i;
             
        }
         
    }
     
    // perform second type operations
    if (lst != -1)
    {
        Console.WriteLine(2 + " " + (lst + 1));
         
    }
    lst = -1;
     
    // for reamining numbers
    for (int i = 0; i < n; ++i)
    {
        // if it is not removed yet
        if (used[i] != 1)
        {
            if (lst != -1)
            {
                Console.WriteLine(1 + " " + (lst + 1) + " "
                            + (i + 1));
            }
            lst = i;
        }
    }
     
}
 
    // Driver code
    static public void Main ()
    {
        int []a = {5, -2, 0, 1, -3};
        int n = a.Length;
        MaximumProduct(a, n);
    }
}
 
// This code is contributed by ajit


Javascript


输出:
2 3
1 1 2
1 2 4
1 4 5

时间复杂度: O(n)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。