📌  相关文章
📜  将数组元素拆分为严格递增和递减的序列

📅  最后修改于: 2021-10-27 07:09:44             🧑  作者: Mango

给定一个包含N 个元素的数组。任务是将元素分成两个数组,比如 a1[] 和 a2[],其中一个包含严格递增的元素,另一个包含严格递减的元素和a1.size() + a2.size() = a.size() .如果不可能这样做,打印-1或打印两个数组。

注意:可以有多个答案,并且子数组中元素的顺序不必相同。

例子:

方法:按照以下步骤解决上述问题:

  • 初始化两个向量 v1 和 v2,它们存储递增和递减的数字。
  • 使用散列来知道数组中数字的出现。
  • 如果数字似乎是第一次出现,则将其存储在 v1 中。
  • 如果该号码第二次出现,则将其存储在 v2 中。
  • 如果数字出现超过 2 次,则无法存储创建严格递增和严格递减的数组。
  • 最后,将第一个向量按升序排序,将第二个向量按降序排序并打印。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to print both the arrays
void PrintBothArrays(int a[], int n)
{
 
    // Store both arrays
    vector v1, v2;
 
    // Used for hashing
    unordered_map mpp;
 
    // Iterate for every element
    for (int i = 0; i < n; i++) {
 
        // Increase the count
        mpp[a[i]]++;
 
        // If first occurrence
        if (mpp[a[i]] == 1)
            v1.push_back(a[i]);
 
        // If second occurrence
        else if (mpp[a[i]] == 2)
            v2.push_back(a[i]);
 
        // If occurs more than 2 times
        else {
            cout << "Not possible";
            return;
        }
    }
 
    // Sort in increasing order
    sort(v1.begin(), v1.end());
 
    // Print the increasing array
    cout << "Strictly increasing array is:\n";
    for (auto it : v1)
        cout << it << " ";
 
    // Sort in reverse order
    sort(v2.begin(), v2.end(), greater());
 
    // Print the decreasing array
    cout << "\nStrictly decreasing array is:\n";
    for (auto it : v2)
        cout << it << " ";
}
 
// Driver code
int main()
{
 
    int a[] = { 7, 2, 7, 3, 3, 1, 4 };
    int n = sizeof(a) / sizeof(a[0]);
    PrintBothArrays(a, n);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG
{
     
// Function to print both the arrays
static void PrintBothArrays(int a[], int n)
{
 
    // Store both arrays
    Vector v1 = new Vector(),
                    v2 = new Vector();
 
    // Used for hashing
    HashMap mpp = new HashMap<>();
 
    // Iterate for every element
    for (int i = 0; i < n; i++)
    {
 
        // Increase the count
        mpp.put(a[i],(mpp.get(a[i]) == null?0:mpp.get(a[i]))+1);
 
        // If first occurrence
        if (mpp.get(a[i]) == 1)
            v1.add(a[i]);
 
        // If second occurrence
        else if (mpp.get(a[i]) == 2)
            v2.add(a[i]);
 
        // If occurs more than 2 times
        else
        {
            System.out.println( "Not possible");
            return;
        }
    }
 
    // Sort in increasing order
    Collections.sort(v1);
 
    // Print the increasing array
    System.out.println("Strictly increasing array is:");
    for (int i = 0; i < v1.size(); i++)
        System.out.print(v1.get(i) + " ");
 
    // Sort
    Collections.sort(v2);
    Collections.reverse(v2);
 
    // Print the decreasing array
    System.out.println("\nStrictly decreasing array is:");
    for (int i = 0; i < v2.size(); i++)
        System.out.print(v2.get(i) + " ");
}
 
// Driver code
public static void main(String args[])
{
    int a[] = { 7, 2, 7, 3, 3, 1, 4 };
    int n = a.length;
    PrintBothArrays(a, n);
}
}
 
// This code is contributed by Arnab Kundu


Python3
# Python3 program to implement
# the above approach
 
# Function to print both the arrays
def PrintBothArrays(a, n) :
 
    # Store both arrays
    v1, v2 = [], [];
 
    # Used for hashing
    mpp = dict.fromkeys(a, 0);
 
    # Iterate for every element
    for i in range(n) :
 
        # Increase the count
        mpp[a[i]] += 1;
 
        # If first occurrence
        if (mpp[a[i]] == 1) :
            v1.append(a[i]);
 
        # If second occurrence
        elif (mpp[a[i]] == 2) :
            v2.append(a[i]);
 
        # If occurs more than 2 times
        else :
            print("Not possible");
            return;
 
    # Sort in increasing order
    v1.sort();
 
    # Print the increasing array
    print("Strictly increasing array is:");
    for it in v1:
        print(it, end = " ");
 
    # Sort in reverse order
    v2.sort(reverse = True);
 
    # Print the decreasing array
    print("\nStrictly decreasing array is:");
    for it in v2 :
        print(it, end = " ")
 
# Driver code
if __name__ == "__main__" :
 
    a = [ 7, 2, 7, 3, 3, 1, 4 ];
    n = len(a);
    PrintBothArrays(a, n);
 
# This code is contributed by Ryuga


C#
// C# program to implement
// the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG
{
         
    // Function to print both the arrays
    static void PrintBothArrays(int [] a, int n)
    {
     
        // Store both arrays
        List v1 = new List();
        List v2 = new List();
         
        // Used for hashing
        Dictionary mpp = new Dictionary();
     
        // Iterate for every element
        for (int i = 0; i < n; i++)
        {
     
            // Increase the Count
            if(mpp.ContainsKey(a[i]))
                mpp[a[i]] = mpp[a[i]] + 1;
            else
                mpp[a[i]] = 1;
     
            // If first occurrence
            if (mpp[a[i]] == 1)
                v1.Add(a[i]);
     
            // If second occurrence
            else if (mpp[a[i]] == 2)
                v2.Add(a[i]);
     
            // If occurs more than 2 times
            else
            {
                Console.WriteLine( "Not possible");
                return;
            }
        }
     
        // Sort in increasing order
        v1.Sort();
     
        // Print the increasing array
        Console.WriteLine("Strictly increasing array is:");
        for (int i = 0; i < v1.Count; i++)
            Console.Write(v1[i] + " ");
     
        // Sort
        v2.Sort();
        v2.Reverse();
     
        // Print the decreasing array
        Console.WriteLine("\nStrictly decreasing array is:");
        for (int i = 0; i < v2.Count; i++)
            Console.Write(v2[i] + " ");
    }
     
    // Driver code
    public static void Main()
    {
        int [] a = { 7, 2, 7, 3, 3, 1, 4 };
        int n = a.Length;
        PrintBothArrays(a, n);
    }
}
 
// This code is contributed by ihritik


Javascript


输出:
Strictly increasing array is:
1 2 3 4 7 
Strictly decreasing array is:
7 3

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程