通过将每个元素替换为位于相邻元素平均值的元素来重新排列给定的数组
给定一个数组arr[] 。该数组仅包含从0到N-1的数字,其中N是arr[]的大小。任务是修改数组,使得对于0≤i
有几个例外:
- 对于数组的第一个元素, arr[i-1] = 0 ;因为以前的索引不存在。
- 对于数组的最后一个元素, arr[i+1] = 0 ;因为下一个索引不存在。
例子:
Input: arr[]= {3, 2, 1, 4, 1, 8, 6, 8, 7}
Output: [2, 1, 4, 2, 6, 4, 7, 6, 1]
Explanation: Following are the operations performed to reach the desired output.
For index 0, arr[i-1]=0 and arr[i+1]=2, arr[0]= arr[(0+2)/2] = arr[1] = 2
For index 3, arr[i-1]=1 and arr[i+1]=1, arr[3]= arr[(1+1)/2] = arr[1] = 2
Input: arr[]= {2, 5, 3, 4, 0, 1}
Output: [3, 3, 0, 5, 3, 2]
Explanation: Following are the operations performed to reach the desired output.
For index 1, arr[i-1]=2 and arr[i+1]=3, arr[1]= arr[(2+3)/2] = arr[2] = 3
For index 4, arr[i-1]=4 and arr[i+1]=1, arr[4]= arr[(4+1)/2] = arr[2] = 3
天真的方法:最简单的方法是创建一个新数组并使用给定数组计算每个索引的值,但这会占用额外的空间。对于每个索引,获取上一个和下一个索引的值并计算它们的平均值。该平均值将是必须将其值复制到新数组中所选索引处的索引。因此,将实现arr[i] = arr[ (arr[i-1] + arr[i+1]) / 2 ] 。
时间复杂度: O(N),N是arr[]的大小。
辅助空间: O(N)
高效的方法:这种方法遍历同一个数组一次,最后会找到所需的数组。跟踪前一个索引元素,这样即使修改了它的值,也可以计算旧值。在这种情况下仅使用O(1 ) 空间,即临时变量用于存储前一个元素的值。
如何做到这一点?
考虑两个数字x和y ,它们都小于N 。目的是更新x (x = y)的值,使得旧值(x=x)不会丢失。基本上,只需在同一个变量中保存两个x值。为此,首先,将值 x 增加因数y*N 。更新后的 x 变为x+y*N 。 x的旧值可以通过取更新值的 mod 得到; (x+y*N % N) 。 x 的新值可以通过除以 N 的商(x+y*N/N)获得。请按照以下步骤解决给定的问题。
- 从左到右迭代数组arr[] 。
- 对于每个索引,元素都会增加arr[(arr[i-1]+arr[i+1])/2] * N 。
- 要获得第 i 个元素的前一个,请找到与N的模数,即arr[i-1]%N 。
- 要获得第 i 个元素的下一个元素,请找到与N的商,即arr[i+1]/N 。
- 再次从头到尾遍历数组。
- 将第 i 个元素除以N后打印第 i 个元素,即array[i]/N 。
下面是上述方法的实现。
C++
// Program to modify the given array
// as per given constraint.
#include
using namespace std;
// Function to find the previous val
int FindPrev(int i, int a, int n)
{
if (i == 0)
return 0;
else
return a % n;
}
// Function to find the next value
int FindNext(int i, int a, int n)
{
if (i == n - 1)
return 0;
else
return a;
}
// The function to rearrange an array
// in-place so that arr[i] becomes
// arr[(arr[i-1]+arr[i+1])/2].
void ModifyTheArray(int arr[], int n)
{
int new_ind, new_ind_val, next, prev;
// Traverse the array arr[]
for (int i = 0; i < n; i++) {
prev = FindPrev(i, arr[i - 1], n);
next = FindNext(i, arr[i + 1], n);
new_ind = (prev + next) / 2;
new_ind_val = arr[new_ind] % n;
arr[i] = arr[i] + n * new_ind_val;
}
for (int i = 0; i < n; i++)
arr[i] /= n;
}
// A utility function to display the array of size n
void DisplayArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
// Driver Code
int main()
{
int arr[] = { 3, 2, 1, 4, 1, 8, 6, 8, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << "Given array is=> \n";
DisplayArray(arr, N);
ModifyTheArray(arr, N);
cout << "Modified array is=> \n";
DisplayArray(arr, N);
return 0;
}
Java
// Java Program to modify the given array
// as per given constraint.
import java.util.*;
public class GFG
{
// Function to find the previous val
static int FindPrev(int i, int a, int n)
{
if (i == 0)
return 0;
else
return a % n;
}
// Function to find the next value
static int FindNext(int i, int a, int n)
{
if (i == n - 1)
return 0;
else
return a;
}
// The function to rearrange an array
// in-place so that arr[i] becomes
// arr[(arr[i-1]+arr[i+1])/2].
static void ModifyTheArray(int arr[], int n)
{
int new_ind, new_ind_val, next, prev;
// Traverse the array arr[]
for (int i = 0; i < n; i++) {
if(i - 1 >= 0 ){
prev = FindPrev(i, arr[i - 1], n);
}
else{
prev = 0;
}
if(i + 1 < n){
next = FindNext(i, arr[i + 1], n);
}
else{
next = 0;
}
new_ind = (prev + next) / 2;
new_ind_val = arr[new_ind] % n;
arr[i] = arr[i] + n * new_ind_val;
}
for (int i = 0; i < n; i++)
arr[i] /= n;
}
// A utility function to display the array of size n
static void DisplayArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver code
public static void main(String args[])
{
int arr[] = { 3, 2, 1, 4, 1, 8, 6, 8, 7 };
int N = arr.length;
System.out.println("Given array is=>");
DisplayArray(arr, N);
ModifyTheArray(arr, N);
System.out.println("Modified array is=>");
DisplayArray(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# python3 Program to modify the given array
# as per given constraint.
# Function to find the previous val
def FindPrev(i, a, n):
if (i == 0):
return 0
else:
return a % n
# Function to find the next value
def FindNext(i, a, n):
if (i == n - 1):
return 0
else:
return a
# The function to rearrange an array
# in-place so that arr[i] becomes
# arr[(arr[i-1]+arr[i+1])/2].
def ModifyTheArray(arr, n):
# Traverse the array arr[]
for i in range(0, n):
prev = FindPrev(i, arr[i - 1], n)
next = FindNext(i, arr[i + 1], n) if i + 1 < n else 0
new_ind = (prev + next) // 2
new_ind_val = arr[new_ind] % n
arr[i] = arr[i] + n * new_ind_val
for i in range(0, n):
arr[i] //= n
# A utility function to display the array of size n
def DisplayArray(arr, n):
for i in range(0, n):
print(arr[i], end=" ")
print()
# Driver Code
if __name__ == "__main__":
arr = [3, 2, 1, 4, 1, 8, 6, 8, 7]
N = len(arr)
print("Given array is=> ")
DisplayArray(arr, N)
ModifyTheArray(arr, N)
print("Modified array is=> ")
DisplayArray(arr, N)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections;
class GFG
{
// Function to find the previous val
static int FindPrev(int i, int a, int n)
{
if (i == 0)
return 0;
else
return a % n;
}
// Function to find the next value
static int FindNext(int i, int a, int n)
{
if (i == n - 1)
return 0;
else
return a;
}
// The function to rearrange an array
// in-place so that arr[i] becomes
// arr[(arr[i-1]+arr[i+1])/2].
static void ModifyTheArray(int []arr, int n)
{
int new_ind, new_ind_val, next, prev;
// Traverse the array arr[]
for (int i = 0; i < n; i++) {
if(i - 1 >= 0 ){
prev = FindPrev(i, arr[i - 1], n);
}
else{
prev = 0;
}
if(i + 1 < n){
next = FindNext(i, arr[i + 1], n);
}
else{
next = 0;
}
new_ind = (prev + next) / 2;
new_ind_val = arr[new_ind] % n;
arr[i] = arr[i] + n * new_ind_val;
}
for (int i = 0; i < n; i++)
arr[i] /= n;
}
// A utility function to display the array of size n
static void DisplayArray(int []arr, int n)
{
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
}
// Driver code
public static void Main()
{
int []arr = { 3, 2, 1, 4, 1, 8, 6, 8, 7 };
int N = arr.Length;
Console.WriteLine("Given array is=>");
DisplayArray(arr, N);
ModifyTheArray(arr, N);
Console.WriteLine("Modified array is=>");
DisplayArray(arr, N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
Given array is=>
3 2 1 4 1 8 6 8 7
Modified array is=>
2 1 4 2 6 4 7 6 1
时间复杂度: O(N),N是arr[]的大小。
空间复杂度: O(1)