📌  相关文章
📜  通过重复组合两个相同元素形成的给定数组中的最大数

📅  最后修改于: 2021-10-27 08:03:52             🧑  作者: Mango

给定一个数组arr[] ,任务是在给定的 Array 中找到最大的数,由两个相同的元素重复组合而成。如果最初数组中没有相同的元素,则将输出打印为 -1。
例子:

方法:这个问题可以使用给定数组中元素的频率来解决。

  • 在地图中查找并存储给定数组的每个元素的频率。
  • 在遍历每个不同元素的频率图时,如果图中存在频率大于 1 的任何重复元素K ,则将元素2*K的频率增加K元素频率的一半。
  • 最后,从地图中找到最大数量。

下面是上述方法的实现:

CPP
// C++ implementation of the above approach
 
#include 
using namespace std;
 
// Function to return the largest sum
int largest_sum(int arr[], int n)
{
    // Variable to store the largest sum
    int maximum = -1;
 
    // Map to store the frequencies
    // of each element
    map m;
 
    // Store the Frequencies
    for (int i = 0; i < n; i++) {
        m[arr[i]]++;
    }
 
    // Loop to combine duplicate elements
    // and update the sum in the map
    for (auto j : m) {
 
        // If j is a duplicate element
        if (j.second > 1) {
 
            // Update the frequency of 2*j
            m[2 * j.first]
                = m[2 * j.first]
                  + j.second / 2;
 
            // If the new sum is greater than
            // maximum value, Update the maximum
            if (2 * j.first > maximum)
                maximum = 2 * j.first;
        }
    }
 
    // Returns the largest sum
    return maximum;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 1, 2, 4, 7, 8 };
    int n = sizeof(arr)
            / sizeof(arr[0]);
 
    // Function Calling
    cout << largest_sum(arr, n);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG {
 
     
    // Function to return the largest sum
    static int largest_sum(int arr[], int n)
    {
        // Variable to store the largest sum
        int maximum = -1;
     
        // Map to store the frequencies
        // of each element
        HashMap m = new HashMap();
     
        // Store the Frequencies
        for (int i = 0; i < n; i++) {
             
            if (m.containsKey(arr[i])){
            m.put(arr[i], m.get(arr[i]) + 1);
            }
            else{
                m.put(arr[i], 1);
            }
        }
     
        // Loop to combine duplicate elements
        // and update the sum in the map
        for(int i = 0; i < n; i++){
 
            // If j is a duplicate element
            if (m.get(arr[i]) > 1) {
                 
                if (m.containsKey(2*arr[i]))
                {
                    // Update the frequency of 2*j
                    m.put(2*arr[i],m.get(2 * arr[i])+ m.get(arr[i]) / 2);
                }
                else
                {
                    m.put(2*arr[i],m.get(arr[i]) / 2);
                }
                 
                // If the new sum is greater than
                // maximum value, Update the maximum
                if (2 * arr[i] > maximum)
                    maximum = 2 * arr[i];
            }
            }
     
        // Returns the largest sum
        return maximum;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int arr[] = { 1, 1, 2, 4, 7, 8 };
        int n = arr.length;
         
        // Function Calling
        System.out.println(largest_sum(arr, n));
    }
}
 
// This code is contributed by Yash_R


Python3
# Python3 implementation of the above approach
 
# Function to return the largest sum
def largest_sum(arr, n):
     
    # Variable to store the largest sum
    maximum = -1
 
    # Map to store the frequencies
    # of each element
    m = dict()
 
    # Store the Frequencies
    for i in arr:
        m[i] = m.get(i,0) + 1
 
    # Loop to combine duplicate elements
    # and update the sum in the map
    for j in list(m):
 
        # If j is a duplicate element
        if ((j in m) and m[j] > 1):
 
            # Update the frequency of 2*j
            x, y = 0, 0
            if 2*j in m:
                m[2*j] = m[2 * j]+ m[j]// 2
            else:
                m[2*j] = m[j]//2
 
            # If the new sum is greater than
            # maximum value, Update the maximum
            if (2 * j > maximum):
                maximum = 2 * j
 
    # Returns the largest sum
    return maximum
 
# Driver Code
if __name__ == '__main__':
    arr= [1, 1, 2, 4, 7, 8]
    n = len(arr)
 
    # Function Calling
    print(largest_sum(arr, n))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
     
    // Function to return the largest sum
    static int largest_sum(int []arr, int n)
    {
        // Variable to store the largest sum
        int maximum = -1;
     
        // Map to store the frequencies
        // of each element
        Dictionary m = new Dictionary();
     
        // Store the Frequencies
        for (int i = 0; i < n; i++) {
             
            if (m.ContainsKey(arr[i])){
                m[arr[i]]++;
            }
            else{
                m.Add(arr[i] , 1);
            }
        }
     
        // Loop to combine duplicate elements
        // and update the sum in the map
        for(int i = 0; i < n; i++){
 
            // If j is a duplicate element
            //Console.Write(m[arr[i]]);
             
            if (m[arr[i]] > 1) {
                 
                if (m.ContainsKey(2*arr[i]))
                {
                    // Update the frequency of 2*j
                    m[2*arr[i]] = m[2 * arr[i]]+ m[arr[i]] / 2;
                }
                else
                {
                    m.Add(2*arr[i],m[arr[i]] / 2);
                }
                 
                // If the new sum is greater than
                // maximum value, Update the maximum
                if (2 * arr[i] > maximum)
                    maximum = 2 * arr[i];
            }
             
            }
     
        // Returns the largest sum
        return maximum;
    }
     
    // Driver Code
    public static void Main ()
    {
        int[] arr = { 1, 1, 2, 4, 7, 8 };
        int n = arr.Length;
         
        // Function Calling
        Console.Write(largest_sum(arr, n));
    }
}
 
// This code is contributed by chitranayal


Javascript


输出:
16