`

Equilibrium index of an array

 
阅读更多

Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an arrya A:

A[0] = -7, A[1] = 1, A[2] = 5, A[3] = 2, A[4] = -4, A[5] = 3, A[6]=0

3 is an equilibrium index, because:
A[0] + A[1] + A[2] = A[4] + A[5] + A[6]

6 is also an equilibrium index, because sum of zero elements is zero, i.e., A[0] + A[1] + A[2] + A[3] + A[4] + A[5]=0

7 is not an equilibrium index, because it is not a valid index of array A.

Write a function int equilibrium(int[] arr, int n); that given a sequence arr[] of size n, returns an equilibrium index (if any) or -1 if no equilibrium indexes exist.

 

public int equilIndex(int[] A) {
    int sum = 0;
    for(int num:A) {
        sum += num;
    }
    int left = 0;
    for(int i=0; i<A.length; i++) {
        sum -= A[i];
        if(sum == left) {
            return i;
        }
        left += A[i];
    }
    return -1;
}

 

Reference:

http://www.geeksforgeeks.org/equilibrium-index-of-an-array/

分享到:
评论

相关推荐

    Effect of prey refuge on the dynamics and equilibrium density of

    Effect of prey refuge on the dynamics and equilibrium density of,马智慧,李自珍,In this paper, we use an analytical approach to study the dynamics of the simplest forms of refuge using by prey....

    平衡点外模式形成pattern formation outside of equilibrium

    平衡点外模式形成pattern formation outside of equilibrium平衡点外模式形成pattern formation outside of equilibrium平衡点外模式形成pattern formation outside of equilibrium平衡点外模式形成pattern ...

    Game Theory and Nash Equilibrium

    is a proposed solution of a non-cooperative game involving two or more players in which each player is assumed to know the equilibrium strategies of the other players, and no player has anything to ...

    Equilibrium

    "Equilibrium"是一个与字体设计相关的主题。在IT领域,字体设计是用户界面(UI)和用户体验(UX)设计中的一个重要组成部分,它不仅关乎信息的可读性,还影响着产品的视觉风格和整体感觉。"Equilibrium"可能指的是一个...

    An equilibrium search problem with endogenous growth rateof human capital

    1. 均衡搜寻模型(Equilibrium Search Model) 均衡搜寻模型是分析市场经济中个体如何通过搜寻来寻找最优匹配的一个理论框架。在这个框架下,研究者通常会考虑个体搜寻过程中面临的信息不对称、搜寻成本、匹配效率等...

    Applications of MATLAB in Science and Engineering

    1 Application of GATES and MATLAB for Resolution of Equilibrium, Metastable and Non-Equilibrium Electrolytic Systems 2 From Discrete to Continuous Gene Regulation Models – A Tutorial Using the Odefy ...

    Applications of Classical Physics

    3.7 Statistical Mechanics of an Ideal Monatomic Gas 3.8 Statistical Mechanics in the Presence of Gravity: Galaxies, Black Holes, the Universe, and Evolution of Structure in the Early Universe 3.9 ...

    equilibrium-point-of-a-static-market-model.rar_Market Equilibriu

    通过分析这个"equilibrium-point-of-a-static-market-model",我们可以深入理解市场如何自我调节以及政策制定者如何利用这些理论来影响经济活动。了解市场均衡不仅有助于经济学家进行预测,也为政府决策提供理论依据...

    Mechanical Behavior of Materials (1)

    4.3.1 Equilibrium Concentration of Point Defects 256 4.3.2 Production of Point Defects 259 4.3.3 Effect of Point Defects on Mechanical Properties 260 4.3.4 Radiation Damage 261 4.3.5 Ion Implantation ...

    Tradable credits for congestion management with mixed equilibrium behaviors

    在本文中,研究者赵晖、张翠平、孙慧军、高子友所提出的可交易信用方案,特别关注了包含用户平衡(User Equilibrium,UE)、古诺-纳什平衡(Cournot–Nash Equilibrium,CNE)和市场平衡(Market Equilibrium,ME)...

    Variational Inequalities and Network Equilibrium Problems

    变分不等式(Variational Inequalities)与网络均衡问题(Network Equilibrium Problems)是运筹学、数学分析以及经济学中重要的概念和研究领域。变分不等式作为研究平衡状态的数学模型,在网络设计、交通、计算、...

    Chemical Equilibrium Applications---纯净版.zip_CEA 热力计算_CEAexec-win

    用CEA进行火箭发动机的热力计算,可以进行化学组分平衡的热力计算

    Equilibrium Approaches to Modern Deep Learning.pdf

    《Equilibrium Approaches to Modern Deep Learning》这篇论文提出了一个全新的思路,即深度均衡(DEQ)模型,它挑战了传统基于层层堆叠的深度学习范式。 DEQ模型的核心思想是将输出视为动态系统的固定点,从而实现...

    The foundations of behavioral economic analysis.pdf

    Part IV describes the evidence on classical game theory and considers several models of behavioral game theory, including level-k and cognitive hierarchy models, quantal response equilibrium, and ...

    Study of air debromination process and Br2 equilibrium concentration during the preparing course of liquid salt from brine waste of desalination plants

    【标题】:“淡化后浓海水制备液体盐过程中除溴工艺及溴的平衡浓度的研究” 在当前的水处理领域,海水淡化技术得到了广泛应用,但其产生的浓海水问题引起了环境关注。浓海水含有高浓度的盐分,特别是溴离子,若不...

    Market Structure and Equilibrium

    3. 均衡价格(Equilibrium Price):市场在某一价格水平上,供给量等于需求量的价格点。 4. 均衡数量(Equilibrium Quantity):在均衡价格下,市场上交易的实际商品数量。 市场均衡会受到多种因素影响,包括技术...

    A Guide to Physics Problems Part 1

    - **Pulling Strings (MIT)**: This problem examines the forces and tensions in a system of strings being pulled, requiring an understanding of Newton's laws and static equilibrium. - **Thru-Earth ...

Global site tag (gtag.js) - Google Analytics