Largest number in given Array formed by repeatedly combining two same elements
Given an array arr[], the task is to find the largest number in given Array, formed by repeatedly combining two same elements. If there are no same elements in the array initially, then print the output as -1.
Examples:
Input: arr = {1, 1, 2, 4, 7, 8}
Output: 16
Explanation:
Repetition 1: Combine 1s from the array and insert the sum 2 in it. Updated Array = {2, 2, 4, 7, 8}
Repetition 2: Combine 2s from the array and insert the sum 4 in it. Updated Array = {4, 4, 7, 8}
Repetition 3: Combine 4s from the array and insert the sum 8 in it. Updated Array = {8, 7, 8}
Repetition 4: Combine 8s from the array and insert the sum 16 in it. Updated Array = {7, 16}
Largest sum = 16
Input: arr = {1, 2, 3}
Output: -1
Explanation:
There are no duplicate elements in the array initially. Hence no combination can be performed.
Approach: This problem can be solved using the frequency of elements in the given array.
- Find and store the frequencies of each element of the given array in a map.
- Upon traversing the frequency map for each distinct element, if there is any duplicate element K in the map with a frequency more than 1, then increase the frequency of element 2*K by half the frequency of the K element.
- Finally, find the maximum number from the map.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
int largest_sum( int arr[], int n)
{
int maximum = -1;
map< int , int > m;
for ( int i = 0; i < n; i++) {
m[arr[i]]++;
}
for ( auto j : m) {
if (j.second > 1) {
m[2 * j.first]
= m[2 * j.first]
+ j.second / 2;
if (2 * j.first > maximum)
maximum = 2 * j.first;
}
}
return maximum;
}
int main()
{
int arr[] = { 1, 1, 2, 4, 7, 8 };
int n = sizeof (arr)
/ sizeof (arr[0]);
cout << largest_sum(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int largest_sum( int arr[], int n)
{
int maximum = - 1 ;
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
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 );
}
}
for ( int i = 0 ; i < n; i++){
if (m.get(arr[i]) > 1 ) {
if (m.containsKey( 2 *arr[i]))
{
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 ( 2 * arr[i] > maximum)
maximum = 2 * arr[i];
}
}
return maximum;
}
public static void main (String[] args)
{
int arr[] = { 1 , 1 , 2 , 4 , 7 , 8 };
int n = arr.length;
System.out.println(largest_sum(arr, n));
}
}
|
Python3
def largest_sum(arr, n):
maximum = - 1
m = dict ()
for i in arr:
m[i] = m.get(i, 0 ) + 1
for j in list (m):
if ((j in m) and m[j] > 1 ):
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 ( 2 * j > maximum):
maximum = 2 * j
return maximum
if __name__ = = '__main__' :
arr = [ 1 , 1 , 2 , 4 , 7 , 8 ]
n = len (arr)
print (largest_sum(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int largest_sum( int []arr, int n)
{
int maximum = -1;
Dictionary< int , int > m = new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
if (m.ContainsKey(arr[i])){
m[arr[i]]++;
}
else {
m.Add(arr[i] , 1);
}
}
for ( int i = 0; i < n; i++){
if (m[arr[i]] > 1) {
if (m.ContainsKey(2*arr[i]))
{
m[2*arr[i]] = m[2 * arr[i]]+ m[arr[i]] / 2;
}
else
{
m.Add(2*arr[i],m[arr[i]] / 2);
}
if (2 * arr[i] > maximum)
maximum = 2 * arr[i];
}
}
return maximum;
}
public static void Main ()
{
int [] arr = { 1, 1, 2, 4, 7, 8 };
int n = arr.Length;
Console.Write(largest_sum(arr, n));
}
}
|
Javascript
<script>
function largest_sum(arr, n)
{
let maximum = -1;
let m = new Map();
for (let i = 0; i < n; i++) {
if (m.has(arr[i])){
m.set(arr[i], m.get(arr[i]) + 1);
}
else {
m.set(arr[i], 1);
}
}
for (let i = 0; i < n; i++){
if (m.get(arr[i]) > 1) {
if (m.has(2*arr[i]))
{
m.set(2*arr[i],m.get(2 * arr[i])+ m.get(arr[i]) / 2);
}
else
{
m.set(2*arr[i],m.get(arr[i]) / 2);
}
if (2 * arr[i] > maximum)
maximum = 2 * arr[i];
}
}
return maximum;
}
let arr = [ 1, 1, 2, 4, 7, 8 ];
let n = arr.length;
document.write(largest_sum(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Last Updated :
26 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...