将数组拆分为两个相等的子数组,其中选择了索引

我需要返回元素的索引,其中左侧元素的总和等于右侧元素的总和。例如,对于数组 [-3, 8, 3, 1, 1, 3],返回值是索引 2,因为前 3 ([-3, 8]) 左边元素的总和与其右侧元素的总和 ([1, 1, 3])。


因此,我首先执行线性搜索功能来查找预期的索引,然后我尝试将数组左右拆分为所选索引,但没有成功


我没有成功让它工作


//linear-search portion,x is the index selected to be split point

public static int findindex(int arr[], int x) {

//if array is null  

if (arr == null) {

        return -1;

    }

//find array length

int len = arr.length;

int i = 0;

//traverse the array

while (i < len) {

//if the i-th element is is x then return the index

    if (arr[i] == x) {

        return i;

    } else {

        i = i + 1;

    }

}

//splint array portion,returns index if not possible

int leftsum = 0;

//treverse array elements

for (int i = 0; i < x; i++) {

//adds current elements to left

    leftsum += arr[i];

//find sum of remader the array elements to rightsum

    int rightsum = 0;

    for (int j = i + 1; j < x; J++)

        rightsum += arr[j];

//split pint index

    if (leftsum == rightsum)

        return i + 1;

}

//if not possible return

return -1;

}


// driver code

public static void main(String[] args) {

    int[] array1 = { -3, 8, 3, 1, 1, 3 };

    System.out.println(findindex(array1));

}


慕姐4208626
浏览 105回答 2
2回答

繁星coding

您可以使用以下代码来解决问题static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] array1 = {-3, 8, 3, 1, 1, 3}; // { -3, 8, 3, 1, 1, 3, 6, 1, 19 };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int indexPosition = GetIndex(array1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (indexPosition != -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(indexPosition);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; static int GetIndex(int[] param)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (param.Length < 0) return -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int leftSum = 0, rightSum = 0; int rightIndex = param.Length - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < param.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i < rightIndex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (leftSum > rightSum)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rightSum += param[rightIndex];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rightIndex -= 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i < rightIndex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; leftSum += param[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rightSum += param[rightIndex]; // if you are looking for only index position you can comment this line,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //variable rightSum and leftSum will give you the sum of left and right side of the array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rightIndex -= 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return rightIndex;&nbsp; &nbsp; &nbsp; &nbsp; }希望这可以帮助 。

郎朗坤

您的代码有两个问题。一是变量i在同一个方法中被定义了两次。另一个问题是您只提供一个输入参数而不是两个。我什至不知道 x 应该是什么参数,因此也从我的改进版本中删除了它。我还删除了 while 循环,因为我不明白你在那里试图做什么。无论如何,这是我的代码版本:public static int findindex(int arr[]) {&nbsp; &nbsp; if (arr == null) {&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; }&nbsp; &nbsp; int len = arr.length;&nbsp; &nbsp; int leftsum = 0;&nbsp; &nbsp; for(int i = 0; i < len; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; leftsum += arr[i];&nbsp; &nbsp; &nbsp; &nbsp; int rightsum = 0;&nbsp; &nbsp; &nbsp; &nbsp; for(int j = i+2; j < len; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rightsum += arr[j];&nbsp; &nbsp; &nbsp; &nbsp; if(leftsum == rightsum)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return i+1;&nbsp; &nbsp; }&nbsp; &nbsp; return -1;}public static void main(String[] args) {&nbsp; &nbsp; int[] array1 = {-3, 8, 3, 1, 1, 3};&nbsp; &nbsp; System.out.println(findindex(array1));}当我从你的代码中删除所有不必要的东西时,唯一的错误是你应该用 i+2 初始化 j,因为如果我理解你的要求,你不想在索引本身和右侧包含元素正确编码。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java