📌  相关文章
📜  通过用和和差交替替换第一个和中间元素来减少数组

📅  最后修改于: 2022-05-13 01:56:08.961000             🧑  作者: Mango

通过用和和差交替替换第一个和中间元素来减少数组

给定一个大小为N的数组arr[] ,任务是在连续删除数组的一个和中间元素并在数组末尾附加它们的和和差后找到数组的最后一个剩余元素。

例子:

方法:给定的问题是一个基于实现的问题,可以通过重复执行给定的步骤来解决:

  • 维护一个变量op ,它存储操作的计数。
  • 删除数组的第一个和中间元素,直到它的大小大于1 ,并根据op将它们的加法或减法附加到数组中(即,如果 op 是偶数,则执行加法,否则执行减法)。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Function to find the last element
// in array after the given operations
int lastElement(vector& arr)
{
 
    // Maintain operation count
    int op = 0, x = 0;
 
    // Loop until array has more
    // than 1 element in it
    while (arr.size() != 1) {
 
        // Stores middle index
        int mid = arr.size() / 2;
 
        // For even iterations perform
        // addition otherwise subtraction
        if (op % 2 == 0) {
            x = arr[mid] + arr[0];
            arr.erase(arr.begin() + mid);
            arr.erase(arr.begin());
        }
        else {
            x = arr[mid] - arr[0];
            arr.erase(arr.begin() + mid);
            arr.erase(arr.begin());
        }
 
        // Append in the end
        arr.push_back(x);
 
        // Increment operation count
        op += 1;
    }
 
    // Return Answer
    return arr[0];
}
 
// Driver Code
int main()
{
    vector arr = { 2, 4, 1, 5, 7 };
    cout << lastElement(arr);
 
    return 0;
}
 
    // This code is contributed by rakeshsahni


Java
// Java program of the above approach
import java.util.*;
 
class GFG{
 
// Function to find the last element
// in array after the given operations
static int lastElement(Vector arr)
{
 
    // Maintain operation count
    int op = 0, x = 0;
 
    // Loop until array has more
    // than 1 element in it
    while (arr.size() != 1) {
 
        // Stores middle index
        int mid = arr.size() / 2;
 
        // For even iterations perform
        // addition otherwise subtraction
        if (op % 2 == 0) {
            x = arr.get(mid) + arr.get(0);
            arr.remove(mid);
            arr.remove(0);
        }
        else {
            x = arr.get(mid) - arr.get(0);
            arr.remove(mid);
            arr.remove(0);
        }
 
        // Append in the end
        arr.add(x);
 
        // Increment operation count
        op += 1;
    }
 
    // Return Answer
    return arr.get(0);
}
 
// Driver Code
public static void main(String[] args)
{
    Integer []arr = { 2, 4, 1, 5, 7 };
    Vector v = new Vector();
    Collections.addAll(v, arr);
    System.out.print(lastElement(v));
 
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python program of the above approach
 
# Function to find the last element
# in array after the given operations
def lastElement(arr):
 
    # Maintain operation count
    op = 0
 
    # Loop until array has more
    # than 1 element in it
    while len(arr) != 1:
 
        # Stores middle index
        mid = len(arr)//2
 
        # For even iterations perform
        # addition otherwise subtraction
        if op % 2 == 0:
            x = arr.pop(mid) + arr.pop(0)
        else:
            x = arr.pop(mid) - arr.pop(0)
 
        # Append in the end
        arr.append(x)
 
        # Increment operation count
        op += 1
 
    # Return Answer
    return arr[0]
 
 
# Driver Code
if __name__ == "__main__":
    arr = [2, 4, 1, 5, 7]
    print(lastElement(arr))


C#
// C# program of the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
  // Function to find the last element
  // in array after the given operations
  static int lastElement(List arr)
  {
 
    // Maintain operation count
    int op = 0, x = 0;
 
    // Loop until array has more
    // than 1 element in it
    while (arr.Count != 1) {
 
      // Stores middle index
      int mid = arr.Count / 2;
 
      // For even iterations perform
      // addition otherwise subtraction
      if (op % 2 == 0) {
        x = arr[mid] + arr[0];
        arr.RemoveAt(mid);
        arr.RemoveAt(0);
      }
      else {
        x = arr[mid] - arr[0];
        arr.RemoveAt(mid);
        arr.RemoveAt(0);
      }
 
      // Append in the end
      arr.Add(x);
 
      // Increment operation count
      op += 1;
    }
 
    // Return Answer
    return arr[0];
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int []arr = { 2, 4, 1, 5, 7 };
    List v = new List(arr);
    Console.Write(lastElement(v));
 
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出
5

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