Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
Split array in three equal sum subarrays

Write a solution for the input array which elements of the array are sorted into three groups. The sum of the elements of each group would should be approximately equal.

Example 1: [7,5,3,1,2,3] Result: [7] // 7, [5,2] //7, [3,3,1] // 7

I managed to do it when there is sum which can be divided in three equal parts. My question is how to upgrade code so it works in cases when sum is not equal or when some element of array is lager than divided sum? Here is my code so far:

    var input=document.getElementById('niz').value;
    var arr=[];
    for (var i = 0; i < input.length; i++) {
        if(input[i]!=" " && !isNaN(input[i])){
            arr.push(input[i]);
        }
        
        
    }
    console.log(arr);
    var n=arr.length;
    var status=true;
    if(n<3){
        status=false;
    }
    else{
        var sum = 0;
        var auxiliary = 0;
        var i=0;
        var j=0;
        //calculate sum of elements
        for(i=0;i<n;i++){
            sum+=arr[i];
        }
        var point = Array(2).fill(0);
        if (sum % 3 == 0)
        {
            // Find that three equal subarray exists in given array
            for (i = 0; i < n && j < 2; ++i)
            {
                // Add current element into auxiliary variable
                auxiliary += arr[i];
                if (auxiliary == parseInt(sum / 3))
                {
                    point[j] = i + 1;
                    // Set zero sum
                    auxiliary = 0;
                    j++;
                }
            }
            if (j == 2)
            {
                // When equal three subarray exist
                j = 0;
                // Print the elements of subarray
                for (i = 0; i < n; ++i)
                {
                    if (j < 2 && point[j] == i)
                    {
                            
                        j++;
                    }
                    
                    console.log(arr[i]);
                }
            }
            else
            {
                status=false;
            }
        }
        else
        {
            status=false;
        }

    }if(status!=true){
        console.log("\n is is not possible")
    }
    
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda