📌  相关文章
📜  查找以循环增量直到索引P产生给定Array的初始序列

📅  最后修改于: 2021-06-25 21:53:56             🧑  作者: Mango

给定一个由N个元素和一个整数P组成的数组arr [] ,任务是查找初始数组,通过以下操作从该数组生成给定的arr []:

  • 从初始数组中选择一个元素arr [i] 。第i索引减少为0
  • arr [i]索引以循环方式增加1,使要增加的最后一个索引为P。

例子:

方法:可以使用以下步骤解决以上问题:

  1. 在数组中找到最小元素,然后从每个索引中减去min – 1
  2. 现在开始从第(P – 1)索引中减去1,并以循环方式对左侧的所有索引重复,直到索引变为0。
  3. 将操作数添加到该索引。
  4. arr []的当前状态给出了所需的初始状态。打印数组。

下面是上述方法的实现:

C++
// C++ program to implement the
// above approach
 
#include 
using namespace std;
 
// Function to generate and return the
// required initial arrangement
void findArray(int* a,
               int n,
               int P)
{
    // Store the minimum element
    // in the array
    int mi = *min_element(a, a + n);
 
    // Store the number of increments
    int ctr = 0;
    mi = max(0, mi - 1);
 
    // Subtract mi - 1 from every index
    for (int i = 0; i < n; i++) {
 
        a[i] -= mi;
 
        ctr += mi;
    }
 
    // Start from the last index which
    // had been incremented
    int i = P - 1;
 
    // Stores the index chosen to
    // distribute its element
    int start = -1;
 
    // Traverse the array cyclically and
    // find the index whose element was
    // distributed
    while (1) {
 
        // If any index has its
        // value reduced to 0
        if (a[i] == 0) {
 
            // Index whose element was
            // distributed
            start = i;
 
            break;
        }
 
        a[i] -= 1;
        ctr += 1;
        i = (i - 1 + n) % n;
    }
 
    // Store the number of increments
    // at the starting index
    a[start] = ctr;
 
    // Print the original array
    for (int i = 0; i < n; i++) {
        cout << a[i] << ", ";
    }
}
 
// Driver Code
int main()
{
    int N = 5;
    int P = 2;
 
    int arr[] = { 3, 2, 0, 2, 7 };
 
    findArray(arr, N, P);
 
    return 0;
}


Java
// Java program to implement the
// above approach
import java.util.*;
 
class GFG{
 
// Function to generate and return the
// required initial arrangement
static void findArray(int []a, int n,
                               int P)
{
     
    // Store the minimum element
    // in the array
    int mi = Arrays.stream(a).min().getAsInt();
 
    // Store the number of increments
    int ctr = 0;
    mi = Math.max(0, mi - 1);
 
    // Subtract mi - 1 from every index
    for(int i = 0; i < n; i++)
    {
        a[i] -= mi;
        ctr += mi;
    }
 
    // Start from the last index which
    // had been incremented
    int i = P - 1;
 
    // Stores the index chosen to
    // distribute its element
    int start = -1;
 
    // Traverse the array cyclically and
    // find the index whose element was
    // distributed
    while (true)
    {
 
        // If any index has its
        // value reduced to 0
        if (a[i] == 0)
        {
 
            // Index whose element was
            // distributed
            start = i;
            break;
        }
        a[i] -= 1;
        ctr += 1;
        i = (i - 1 + n) % n;
    }
 
    // Store the number of increments
    // at the starting index
    a[start] = ctr;
 
    // Print the original array
    for(i = 0; i < n; i++)
    {
        System.out.print(a[i] + ", ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 5;
    int P = 2;
    int arr[] = { 3, 2, 0, 2, 7 };
 
    findArray(arr, N, P);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to implement
# the above approach
 
# Function to generate and return the
# required initial arrangement
def findArray(a, n, P):
 
    # Store the minimum element
    # in the array
    mi = min(a)
 
    # Store the number of increments
    ctr = 0
    mi = max(0, mi - 1)
 
    # Subtract mi - 1 from every index
    for i in range(n):
        a[i] -= mi
        ctr += mi
 
    # Start from the last index which
    # had been incremented
    i = P - 1
 
    # Stores the index chosen to
    # distribute its element
    start = -1
 
    # Traverse the array cyclically and
    # find the index whose element was
    # distributed
    while (1):
 
        # If any index has its
        # value reduced to 0
        if (a[i] == 0):
 
            # Index whose element was
            # distributed
            start = i
            break
 
        a[i] -= 1
        ctr += 1
        i = (i - 1 + n) % n
 
    # Store the number of increments
    # at the starting index
    a[start] = ctr
 
    # Print the original array
    print(*a, sep = ', ')
 
# Driver Code
if __name__ == '__main__':
 
    N = 5
    P = 2
 
    arr = [ 3, 2, 0, 2, 7 ]
 
    findArray(arr, N, P)
 
# This code is contributed by himanshu77


C#
// C# program to implement the
// above approach
using System;
using System.Linq;
class GFG{
 
// Function to generate and return the
// required initial arrangement
static void findArray(int []a, int n,
                               int P)
{
     
    // Store the minimum element
    // in the array
    int mi = a.Min();
 
    // Store the number of increments
    int ctr = 0;
    mi = Math.Max(0, mi - 1);
 
    // Subtract mi - 1 from every index
    int i;
    for(i = 0; i < n; i++)
    {
        a[i] -= mi;
        ctr += mi;
    }
 
    // Start from the last index which
    // had been incremented
     i = P - 1;
 
    // Stores the index chosen to
    // distribute its element
    int start = -1;
 
    // Traverse the array cyclically and
    // find the index whose element was
    // distributed
    while (true)
    {
 
        // If any index has its
        // value reduced to 0
        if (a[i] == 0)
        {
 
            // Index whose element was
            // distributed
            start = i;
            break;
        }
        a[i] -= 1;
        ctr += 1;
        i = (i - 1 + n) % n;
    }
 
    // Store the number of increments
    // at the starting index
    a[start] = ctr;
 
    // Print the original array
    for(i = 0; i < n; i++)
    {
        Console.Write(a[i] + ", ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 5;
    int P = 2;
    int []arr = { 3, 2, 0, 2, 7 };
 
    findArray(arr, N, P);
}
}
 
// This code is contributed by Rohit_ranjan


Javascript


输出:
2, 1, 4, 1, 6,

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

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