2022 Task 3 Fair Index Count

2024-03-13 21:36
文章标签 2022 count index fair task

本文主要是介绍2022 Task 3 Fair Index Count,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Task 3
You are given two arrays A and B consisting of N integers each.
Index K is named fair if the four sums (A[0]+…+A[K-1]), (A[K]+…+A[N-1]), (B[0]+…+B[K-1]) and (B[K]+…+B[N-1]) are all equal.
In other words, K is the index where the two arrays A and B can be split( into two non-empty arrays each) in such a way that the sums of the resulting arrays’ elements are equal.

For example, given arrays A = [0, 4, -1, 0, 3] and B = [0, -2, 5, 0, 3], index K=3 is fair. The sum of the four subarrays are all equals to 3. On the other hand, index K=2 is not fair, the sums of subarrays are not equal.

Write a function:

class Solution { public int solution(int[] A, int[] B); }

which, given two arrays of integers A and B, returns the total number of fair indexes.

Examples

  1. Given A = [0, 4, -1, 0, 3] and B = [0, -2, 5, 0, 3], your function should return 2. The fair indexes are 3 and 4. In both cases, the sums of elements of the subarrays are equals to 3.
  2. Given A = [2, -2, -3, 3] and B = [0, 0, 4, -4], your function should return 1. The only fair index is 2. Index 4 is not fair as the subarrays containing indexes from K to N-1 would be empty.
  3. Given A = [4, -1, 0, 3] and B = [-2, 6, 0, 4], your function should return 0. There are no fair indexes.
  4. Given A = [3, 2, 6] and B = [4, 1, 6], your function should return 0.
  5. Given A = [1, 4, 2, -2, 5] and B = [7, -2, -2, 2, 5], your function should return 2. The fair indexes are 2 and 4.
public class FairIndexCount {public int solution(int[] A, int[] B) {int sumA = 0;int sumB = 0;for (int i = 0; i < A.length; i++) {sumA += A[i];sumB += B[i];}int count = 0;int leftASum = A[0];int leftBSum = B[0];for (int k = 1; k < A.length; k++) {if (leftASum == leftBSum && leftBSum== (sumA-leftASum) && leftBSum == (sumB-leftBSum)) {count++;}leftASum += A[k];leftBSum += B[k];}return count;}public static void main(String[] args) {FairIndexCount fairIndexCount = new FairIndexCount();System.out.println(fairIndexCount.solution(new int[]{0, 4, -1, 0, 3}, new int[]{0, -2, 5, 0, 3})); // 2System.out.println(fairIndexCount.solution(new int[]{2, -2, -3, 3}, new int[]{0, 0, 4, -4})); // 1System.out.println(fairIndexCount.solution(new int[]{4, -1, 0, 3}, new int[]{-2, 6, 0, 4})); // 0System.out.println(fairIndexCount.solution(new int[]{3, 2, 6}, new int[]{4, 1, 6})); // 0System.out.println(fairIndexCount.solution(new int[]{1, 4, 2, -2, 5}, new int[]{7, -2, -2, 2, 5})); // 2}
}

这篇关于2022 Task 3 Fair Index Count的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/806265

相关文章

C# Task Cancellation使用总结

《C#TaskCancellation使用总结》本文主要介绍了在使用CancellationTokenSource取消任务时的行为,以及如何使用Task的ContinueWith方法来处理任务的延... 目录C# Task Cancellation总结1、调用cancellationTokenSource.

IEEE会议投稿资料汇总http://cadcg2015.nwpu.edu.cn/index.htm

最近投了篇IEEE的顶级会议文章,一下是比较有用的一些资料,以供参考。 1.会议主页:http://cadcg2015.nwpu.edu.cn/index.htm     (The 14th International Conference on Computer-Aided Design and Computer Graphics (CAD/Graphics 2015)) 2.I

INDEX+SMALL+IF+ROW函数组合使用解…

很多人在Excel中用函数公式做查询的时候,都必然会遇到的一个大问题,那就是一对多的查找/查询公式应该怎么写?大多数人都是从VLOOKUP、INDEX+MATCH中入门的,纵然你把全部的多条件查找方法都学会了而且运用娴熟,如VLOOKUP和&、SUMPRODUCT、LOOKUP(1,0/....,但仍然只能对这种一对多的查询望洋兴叹。   这里讲的INDEX+SMALL+IF+ROW的函数组合,

CTFHub技能树-Git泄漏-Index

目录 一、Git索引(Index)的基本概念 二、解题过程 主旨:使用git泄漏恢复源代码 方法一:使用GitHack手动恢复 方法二:直接使用Git_Extract获取网站源代码拿去flag   当前大量开发人员使用git进行版本控制,对站点自动部署。如果配置不当,可能会将.git文件夹直接部署到线上环境。这就引起了git泄露漏洞。请尝试使用BugScanTeam的Gi

android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5

描述: 01-02 00:13:43.380: E/flyLog:ChatManager(963): getUnreadChatGroupandroid.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5 01-02 00:13:43.380: E/flyLog:ChatManager(

leetcode#38. Count and Say

The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 12. 113. 214. 12115. 111221 1 is read off as “one 1” or 11. 11 is read off

关于OceanBase MySQL 模式中全局索引 global index 的常见问题

在OceanBase的问答区和开源社区钉钉群聊中,时常会有关于全局索引 global index的诸多提问,因此,借这篇博客,针对其中一些普遍出现的问题进行简要的解答。 什么是 global index ? 由于 MySQL 不具备 global index 的概念,因此这一问题会经常被社区版用户提及。就在前几天,就要人询问下面这个语法的意义。 create table part_tes

上海大学《2022年836+915自动控制原理真题及答案》 (完整版)

Part1:2022年上海大学真题题目 学硕836 专硕915 Part2:2022年上海大学真题答案 学硕836 专硕915

运行PHP程序时提示“Notice: Undefined index”的解决办法

最近在调试网站程序的时候,不知道怎么经常出现“Notice:Undefined index”的提示,程序又可以正常运行,就是看到这个提示感觉有点不爽,把模板搞乱了,经查其实这个不是错误,是警告。如果服务器不能改,那每个变量使用前应当先定义。怎么样解决呢?很多网友的说法不一致,程序不一样你也根本没办法照着解决,要是自己慢慢研究的话一大堆代码得半天试,在这里提供一个最简单有效经本人测试有效的办法给大家

兔子--Android Studio出现错误:Error:Execution failed for task ':myapp:dexDebug'. com.android.ide.common.pro

重点在:finished with non-zero exit value 2. 这里表明了有重复的内容存在。 由于:Android Studio中引入包的方式有如下2种:    compile 'com.android.support:support-v4:22.0.0'    compile files('libs/support-v