Files
freeCodeCamp/curriculum/challenges/russian/08-coding-interview-prep/rosetta-code/equilibrium-index.russian.md

3.8 KiB
Raw Blame History

title, id, challengeType, forumTopicId, localeTitle
title id challengeType forumTopicId localeTitle
Equilibrium index 5987fd532b954e0f21b5d3f6 5 302255 Равновесный индекс

Description

Равновесный индекс последовательности является индексом в такой последовательности, что сумма элементов при более низких индексах равна сумме элементов с более высокими индексами.

Например, в последовательности $ A $ :

:::: $ A_0 = -7 $

:::: $ A_1 = 1 $

:::: $ A_2 = 5 $

:::: $ A_3 = 2 $

:::: $ A_4 = -4 $

:::: $ A_5 = 3 $

:::: $ A_6 = 0 $

3 - показатель равновесия, поскольку:

:::: $ A_0 + A_1 + A_2 = A_4 + A_5 + A_6 $

6 также является показателем равновесия, поскольку:

:::: $ A_0 + A_1 + A_2 + A_3 + A_4 + A_5 = 0 $

(сумма нулевых элементов равна нулю)

7 не является индексом равновесия, поскольку он не является допустимым индексом последовательности $ A $ .

Напишите функцию, которая, учитывая последовательность, возвращает свои равновесные индексы (если они есть).

Предположим, что последовательность может быть очень длинной.

Instructions

Write a function that, given a sequence, returns its equilibrium indices (if any). Assume that the sequence may be very long.

Tests

tests:
  - text: <code>equilibrium</code> is a function.
    testString: assert(typeof equilibrium === 'function');
  - text: <code>equilibrium([-7, 1, 5, 2, -4, 3, 0])</code> should return <code>[3,6]</code>.
    testString: assert.deepEqual(equilibrium(equilibriumTests[0]), ans[0]);
  - text: <code>equilibrium([2, 4, 6])</code> should return <code>[]</code>.
    testString: assert.deepEqual(equilibrium(equilibriumTests[1]), ans[1]);
  - text: <code>equilibrium([2, 9, 2])</code> should return <code>[1]</code>.
    testString: assert.deepEqual(equilibrium(equilibriumTests[2]), ans[2]);
  - text: <code>equilibrium([1, -1, 1, -1, 1, -1, 1])</code> should return <code>[0,1,2,3,4,5,6]</code>.
    testString: assert.deepEqual(equilibrium(equilibriumTests[3]), ans[3]);
  - text: <code>equilibrium([1])</code> should return <code>[0]</code>.
    testString: assert.deepEqual(equilibrium(equilibriumTests[4]), ans[4]);
  - text: <code>equilibrium([])</code> should return <code>[]</code>.
    testString: assert.deepEqual(equilibrium(equilibriumTests[5]), ans[5]);

Challenge Seed

function equilibrium(a) {
  // Good luck!
}

After Tests

const equilibriumTests =
  [[-7, 1, 5, 2, -4, 3, 0], // 3, 6
  [2, 4, 6], // empty
  [2, 9, 2], // 1
  [1, -1, 1, -1, 1, -1, 1], // 0,1,2,3,4,5,6
  [1], // 0
  [] // empty
  ];
const ans = [[3, 6], [], [1], [0, 1, 2, 3, 4, 5, 6], [0], []];

Solution

function equilibrium(a) {
  let N = a.length,
    i,
    l = [],
    r = [],
    e = [];
  for (l[0] = a[0], r[N - 1] = a[N - 1], i = 1; i < N; i++)
    { l[i] = l[i - 1] + a[i], r[N - i - 1] = r[N - i] + a[N - i - 1]; }
  for (i = 0; i < N; i++)
    { if (l[i] === r[i]) e.push(i); }
  return e;
}