📌  相关文章
📜  将数组拆分为三个连续的子数组,分别为负积、0 积和正积

📅  最后修改于: 2021-09-06 05:45:02             🧑  作者: Mango

给定一个数组arr[]大小N使得每个数组元素是-1、01 ,任务是检查是否可以将数组拆分为 3 个连续的子数组,使得第一个、第二个和第三个子数组分别为负、0 和正。

例子:

方法:可以通过贪婪地选择第一个’ -1 ‘之前没有0 ‘s的元素作为第一个子数组,然后选择最后一个元素直到乘积变为1的元素作为第三个子数组,其余元素为第二个子数组。请按照以下步骤解决问题:

  • 将两个变量(例如lr)初始化为-1 ,以存储第一个子数组的最后一个元素和第三个子数组的第一个元素的索引。
  • 遍历数组arr[] ,如果l-1arr[i]0 ,则打印“不可能” 。否则,如果arr[i]-1 ,则将i分配给l并跳出循环。
  • 以相反的顺序遍历数组arr[] ,如果[i, N – 1]范围内的乘积大于0 ,则将i分配给r并跳出循环。
  • 检查是否 lr-1l ≥ r[l, r]范围内的 0 计数为0 。如果任何条件为真,则打印“不可能”
  • 打印范围[0, l] 上的第一个子数组,打印范围[l+1, r-1] 上的第二个子数组,然后打印范围[r, N-1] 上的最后一个子数组。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to split an array into 3 contiguous
// subarrays satisfying the given condition
void PrintAllArrays(int arr[], int N)
{
    // Store the index of rightmost
    // element of the first and leftmost
    // element of the third subarray
    int l = -1, r = -1;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // If l is equal to -1
        if (l == -1) {
 
            // If arr[i] is -1
            if (arr[i] == -1) {
 
                l = i;
                break;
            }
 
            // If arr[i] is 0
            if (arr[i] == 0) {
 
                cout << "Not Possible" << endl;
                return;
            }
        }
    }
 
    // Stores the product
    // of the last subarray
    int p = 1;
 
    // Traverse the array in reverse
    for (int i = N - 1; i >= 0; i--) {
 
        // Update the product
        p *= arr[i];
 
        // If p is greater than 0,
        // assign i to r and break
        if (p > 0) {
            r = i;
            break;
        }
    }
 
    // If l or r is -1 or l to r, there
    // P are no 0's, print "Not possible"
    if (l == -1 || r == -1 || l >= r
        || count(arr + l, arr + r + 1, 0) == 0) {
 
        cout << "Not Possible" << endl;
        return;
    }
 
    // Print the three subarrays
    for (int i = 0; i <= l; i++)
        cout << arr[i] << " ";
 
    cout << "\n";
 
    for (int i = l + 1; i < r; i++)
        cout << arr[i] << " ";
    cout << "\n";
 
    for (int i = r; i < N; i++)
        cout << arr[i] << " ";
}
 
// Driver Code
int main()
{
    // Given Input
    int arr[] = { -1, 0, 1 };
    int N = sizeof(arr) / sizeof(int);
 
    PrintAllArrays(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to split an array into 3 contiguous
// subarrays satisfying the given condition
static void PrintAllArrays(int arr[], int N)
{
     
    // Store the index of rightmost
    // element of the first and leftmost
    // element of the third subarray
    int l = -1, r = -1;
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If l is equal to -1
        if (l == -1)
        {
             
            // If arr[i] is -1
            if (arr[i] == -1)
            {
                l = i;
                break;
            }
 
            // If arr[i] is 0
            if (arr[i] == 0)
            {
                System.out.println("Not Possible");
                return;
            }
        }
    }
 
    // Stores the product
    // of the last subarray
    int p = 1;
 
    // Traverse the array in reverse
    for(int i = N - 1; i >= 0; i--)
    {
         
        // Update the product
        p *= arr[i];
 
        // If p is greater than 0,
        // assign i to r and break
        if (p > 0)
        {
            r = i;
            break;
        }
    }
 
    // If l or r is -1 or l to r, there
    // P are no 0's, print "Not possible"
    if (l == -1 || r == -1 || l >= r ||
       count(arr, l, r + 1, 0) == 0)
    {
        System.out.println("Not Possible");
        return;
    }
 
    // Print the three subarrays
    for(int i = 0; i <= l; i++)
        System.out.print(arr[i] + " ");
         
    System.out.println();
 
    for(int i = l + 1; i < r; i++)
        System.out.print(arr[i] + " ");
         
    System.out.println();
 
    for(int i = r; i < N; i++)
        System.out.print(arr[i] + " ");
}
 
// Function to return the number of occurrence of
// the element 'val' in the range [start, end).
static int count(int arr[], int start,
                 int end, int val)
{
    int count = 0;
    for(int i = start; i < end; i++)
    {
        if (arr[i] == val)
        {
            count++;
        }
    }
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Input
    int arr[] = { -1, 0, 1 };
    int N = arr.length;
 
    PrintAllArrays(arr, N);
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to split an array into 3 contiguous
# subarrays satisfying the given condition
def PrintAllArrays(arr, N):
     
    # Store the index of rightmost
    # element of the first and leftmost
    # element of the third subarray
    l = -1
    r = -1
 
    # Traverse the array arr[]
    for i in range(N):
         
        # If l is equal to -1
        if (l == -1):
             
            # If arr[i] is -1
            if (arr[i] == -1):
                l = i
                break
             
            # If arr[i] is 0
            if (arr[i] == 0):
                print("Not Possible")
                return
             
    # Stores the product
    # of the last subarray
    p = 1
 
    # Traverse the array in reverse
    for i in range(N - 1, -1, -1):
         
        # Update the product
        p *= arr[i]
 
        # If p is greater than 0,
        # assign i to r and break
        if (p > 0):
            r = i
            break
         
    # If l or r is -1 or l to r, there
    # P are no 0's, pr"Not possible"
    if (l == -1 or r == -1 or l >= r or
       count(arr, l, r + 1, 0) == 0):
        print("Not Possible")
        return
 
    # Printthe three subarrays
    for i in range(0, l + 1, 1):
        print(arr[i], end = " ")
         
    print()
 
    for i in range(l + 1, r, 1):
        print(arr[i], end = " ")
         
    print()
 
    for i in range(r, N, 1):
        print(arr[i], end = " ")
 
# Function to return the number of occurrence of
# the element 'val' in the range [start, end).
def count(arr, start, end, val):
     
    count = 0
    for i in range(start, end, 1):
        if (arr[i] == val):
            count += 1
         
    return count
 
# Driver Code
 
# Given Input
arr = [ -1, 0, 1 ]
N = len(arr)
 
PrintAllArrays(arr, N)
  
# This code is contriobuted by code_hunt


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to split an array into 3 contiguous
// subarrays satisfying the given condition
static void PrintAllArrays(int[] arr, int N)
{
     
    // Store the index of rightmost
    // element of the first and leftmost
    // element of the third subarray
    int l = -1, r = -1;
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If l is equal to -1
        if (l == -1)
        {
             
            // If arr[i] is -1
            if (arr[i] == -1)
            {
                l = i;
                break;
            }
 
            // If arr[i] is 0
            if (arr[i] == 0)
            {
                Console.WriteLine("Not Possible");
                return;
            }
        }
    }
 
    // Stores the product
    // of the last subarray
    int p = 1;
 
    // Traverse the array in reverse
    for(int i = N - 1; i >= 0; i--)
    {
         
        // Update the product
        p *= arr[i];
 
        // If p is greater than 0,
        // assign i to r and break
        if (p > 0)
        {
            r = i;
            break;
        }
    }
 
    // If l or r is -1 or l to r, there
    // P are no 0's, print "Not possible"
    if (l == -1 || r == -1 || l >= r ||
       count(arr, l, r + 1, 0) == 0)
    {
        Console.WriteLine("Not Possible");
        return;
    }
 
    // Print the three subarrays
    for(int i = 0; i <= l; i++)
        Console.Write(arr[i] + " ");
         
    Console.WriteLine();
 
    for(int i = l + 1; i < r; i++)
        Console.Write(arr[i] + " ");
         
    Console.WriteLine();
 
    for(int i = r; i < N; i++)
        Console.Write(arr[i] + " ");
}
 
// Function to return the number of occurrence of
// the element 'val' in the range [start, end).
static int count(int[] arr, int start,
                 int end, int val)
{
    int count = 0;
    for(int i = start; i < end; i++)
    {
        if (arr[i] == val)
        {
            count++;
        }
    }
    return count;
}
 
// Driver code
public static void Main(String []args)
{
     
    // Given Input
    int[] arr = { -1, 0, 1 };
    int N = arr.Length;
     
    PrintAllArrays(arr, N);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
-1 
0 
1

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live