给定由N个元素和整数P组成的数组arr [] ,任务是查找初始数组,通过以下操作从中生成给定arr []:
- 从初始数组中选择一个元素arr [i] 。第i个索引减少为0 。
- arr [i]索引以循环方式增加1,以使要增加的最后一个索引为P。
例子:
Input: arr[] = {4, 3, 1, 6}, P = 4
Output: 3 2 5 4
Explanation:
The element arr[2] is chosen from the initial array. The following arr[i] operations modifies the given array in the following sequence:
{3, 2, 0, 4} -> {3, 2, 0, 5} -> {4, 2, 0, 5} -> {4, 3, 0, 5} -> {4, 3, 1, 5} -> {4, 3, 1, 6}
Input: arr[] = {3, 2, 0, 2, 7}, P = 2
Output: 2 1 4 1 6
方法:可以使用以下步骤解决以上问题:
- 在数组中找到最小元素,然后从每个索引中减去min – 1 。
- 现在开始从第(P – 1)个索引中减去1,并以循环方式对左侧的所有索引重复,直到索引变为0。
- 将操作数添加到该索引。
- 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
输出:
2, 1, 4, 1, 6,
时间复杂度: O(N)
辅助空间: O(1)