📌  相关文章
📜  最大化子序列的偶数和奇数索引元素之和之间的差异

📅  最后修改于: 2021-05-05 01:34:11             🧑  作者: Mango

给定一个由N个正整数组成的数组arr [] ,任务是从给定数组中找到子序列的偶数和奇数索引元素之和之间的最大可能差。

例子:

天真的方法:解决此问题的最简单方法是生成给定数组的所有可能子序列,并为每个子序列计算子序列的偶数和奇数索引元素之和之间的差。最后,打印获得的最大差异。

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

高效方法:为了优化上述方法,其思想是将局部最大值存储在子序列的偶数索引处,并将局部最小值存储在子序列的奇数索引处。最后,打印子序列的偶数和奇数索引之和之间的差。
请按照以下步骤解决问题:

  • 初始化变量maxDiff ,以存储子序列的偶数和奇数索引元素之和之间的最大差。
  • 遍历数组arr []并检查arr [i]> arr [i + 1]arr [i] 是否正确。如果发现为真,则更新maxDiff + = arr [i]
  • 否则,检查arr [i]> arr [i + 1]arr [i] 是否正确。如果发现为真,则更新maxDiff-= arr [i]
  • 最后,输出maxDiff的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the maximum possible difference
// between sum of even and odd indices
int maxPossibleDiff(vector& arr, int N)
{
 
    // Convert arr[] into 1-based indexing
    arr.push_back(-1);
 
    // Reverse the array
    reverse(arr.begin(), arr.end());
 
    // Convert arr[] into 1 based index
    arr.push_back(-1);
 
    // Reverse the array
    reverse(arr.begin(), arr.end());
 
    // Stores maximum difference between
    // sum of even and odd indexed elements
    int maxDiff = 0;
 
    // Traverse the array
    for (int i = 1; i <= N; i++) {
 
        // If arr[i] is local maxima
        if (arr[i] > arr[i - 1]
            && arr[i] > arr[i + 1]) {
 
            // Update maxDiff
            maxDiff += arr[i];
        }
 
        // If arr[i] is local minima
        if (arr[i] < arr[i - 1]
            && arr[i] < arr[i + 1]) {
 
            // Update maxDiff
            maxDiff -= arr[i];
        }
    }
    cout << maxDiff;
}
 
// Driver Code
int main()
{
 
    vector arr = { 3, 2, 1, 4, 5,
                        2, 1, 7, 8, 9 };
 
    // Size of array
    int N = arr.size();
 
    // Function Call
    maxPossibleDiff(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum possible
// difference between sum of even and
// odd indices
static void maxPossibleDiff(Vector arr, int N)
{
     
    // Convert arr[] into 1-based indexing
    arr.add(-1);
 
    // Reverse the array
    Collections.reverse(arr);
 
    // Convert arr[] into 1 based index
    arr.add(-1);
 
    // Reverse the array
    Collections.reverse(arr);
 
    // Stores maximum difference between
    // sum of even and odd indexed elements
    int maxDiff = 0;
 
    // Traverse the array
    for(int i = 1; i <= N; i++)
    {
         
        // If arr.get(i) is local maxima
        if (arr.get(i) > arr.get(i - 1) &&
            arr.get(i) > arr.get(i + 1))
        {
             
            // Update maxDiff
            maxDiff += arr.get(i);
        }
 
        // If arr.get(i) is local minima
        if (arr.get(i) < arr.get(i - 1) &&
            arr.get(i) < arr.get(i + 1))
        {
             
            // Update maxDiff
            maxDiff -= arr.get(i);
        }
    }
    System.out.print(maxDiff);
}
 
// Driver Code
public static void main(String[] args)
{
    int[] array = { 3, 2, 1, 4, 5,
                    2, 1, 7, 8, 9 };
    Vector v = new Vector<>();
    for(int i :array)
    {
        v.add(i);
    }
     
    // Size of array
    int N = v.size();
 
    // Function Call
    maxPossibleDiff(v, N);
}
}
 
// This code is contributed by shikhasingrajput


Python3
#Python3 program to implement
#the above approach
 
 
#Function to find the maximum possible difference
#between sum of even and odd indices
def maxPossibleDiff(arr,  N):
 
    #Convert arr[] o 1-based indexing
    arr.append(-1)
 
    #Reverse the array
    arr = arr[::-1]
 
    #Convert arr[] o 1 based index
    arr.append(-1)
 
    #Reverse the array
    arr = arr[::-1]
 
    #Stores maximum difference between
    #sum of even and odd indexed elements
    maxDiff = 0
 
    #Traverse the array
    for i in range(1,N+1):
 
        #If arr[i] is local maxima
        if (arr[i] > arr[i - 1] and arr[i] > arr[i + 1]):
 
            #Update maxDiff
            maxDiff += arr[i]
 
        #If arr[i] is local minima
        if (arr[i] < arr[i - 1] and arr[i] < arr[i + 1]):
 
            #Update maxDiff
            maxDiff -= arr[i]
    print (maxDiff)
 
#Driver Code
if __name__ == '__main__':
 
    arr = [3, 2, 1, 4, 5, 2, 1, 7, 8, 9]
 
    #Size of array
    N = len(arr)
 
    #Function Call
    maxPossibleDiff(arr, N)


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
// Function to find the maximum possible
// difference between sum of even and
// odd indices
static void maxPossibleDiff(List arr, int N)
{
     
    // Convert []arr into 1-based indexing
    arr.Add(-1);
 
    // Reverse the array
    arr.Reverse();
 
    // Convert []arr into 1 based index
    arr.Add(-1);
 
    // Reverse the array
    arr.Reverse();
 
    // Stores maximum difference between
    // sum of even and odd indexed elements
    int maxDiff = 0;
 
    // Traverse the array
    for(int i = 1; i <= N; i++)
    {
         
        // If arr[i] is local maxima
        if (arr[i] > arr[i - 1] &&
            arr[i] > arr[i + 1])
        {
             
            // Update maxDiff
            maxDiff += arr[i];
        }
 
        // If arr[i] is local minima
        if (arr[i] < arr[i - 1] &&
            arr[i] < arr[i + 1])
        {
             
            // Update maxDiff
            maxDiff -= arr[i];
        }
    }
    Console.Write(maxDiff);
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] array = { 3, 2, 1, 4, 5,
                    2, 1, 7, 8, 9 };
    List v = new List();
    foreach(int i in array)
    {
        v.Add(i);
    }
     
    // Size of array
    int N = v.Count;
 
    // Function Call
    maxPossibleDiff(v, N);
}
}
 
// This code is contributed by 29AjayKumar


输出:
15

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