Question

In: Other

Given an array of numbers, find the index of the smallest array element (the pivot), for...

Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. 

Example arr=[1,2,3,4,6] 

  • the sum of the first three elements, 1+2+3=6. The value of the last element is 6. 

  • Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. 

  • The index of the pivot is 3. 


Function Description 

Complete the function balancedSum in the editor below. 

balanced Sum has the following 

parameter(s): 

       int arr(n): an array of integers 

Returns: 

       int: an integer representing the index of the pivot

Constraints 

  •  3≤n≤105 

  • 1≤ arr[i]≤ 2 * 104, where 0≤i

  • It is guaranteed that a solution always exists.

Solutions

Expert Solution

Approach used:
>>Maintain two pointers, leftptr, rightptr
>>Traverse array from both sides at once and calculate left_sum and right_sum to store array sum from right and left.

>>If left_sum < right_sum increment leftptr
>>If left_sum > right_sum decrement rightptr
>>When left_sum = right_sum and leftptr and rightptr have only one element in between which is pivot,
>>Return the pivot
-----------------------------------------------------------------------------------

I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.

----------------solution.java---------

import java.util.*;
import java.lang.String;

public class solution
{
   public static void main(String[] args)
   {
       int arr1 [] ={1,2,3,4,6};
       int pivot_index = balancedSum(arr1);
       System.out.print("\nThe pivot index for arr1 is: " + pivot_index+"\n");

       int arr2 [] ={10, 5, 5, 1, 9, 3, 1, 2 , 15};
       pivot_index = balancedSum(arr2);
       System.out.print("\nThe pivot index for arr2 is: " + pivot_index+"\n");
   }

   public static int balancedSum(int [] array) //returns pivot index for array
   {
       int leftptr = 0; //declare leftptr and rightptr
       int rightptr = array.length-1;   
       int left_sum = array[leftptr]; //initialize left sum to first element
       int right_sum = array[rightptr]; //initialize left sum to last element
       while(rightptr - leftptr != 2) //do until there is one element between both pointers
       {
           if(left_sum <= right_sum){ //if left sum is less, increment leftptr and increase sum
               leftptr++;
               left_sum = left_sum + array[leftptr];
           }
           else{ //if right sum is less, decrement rightptr and decrease sum
               rightptr--;
               right_sum = right_sum + array[rightptr];
           }
       }
       if(left_sum == right_sum) //if only one element exists in between and both sums are equal, then return pivot
           return (leftptr+1);
       else
           return -1; //else return -1
   }
}

----------------Screenshot solution.java---------

 


Related Solutions

Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
How would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array.
JAVA ProgrammingHow would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array. index 4 is the 5th smallest element in the array, index 9 is the 10th smallest element in the array and so on...- this array could be small (like 5 indexes) or large (like 100,000 indexes).- all other numbers do not change position
Consider sorting n numbers stored in array A by first finding the smallest element of A...
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A and exchange it with A[2]. Continue in this manner for the first n-1 elements of A. a) (10 points) This algorithm is known as the selection sort. Write the pseudo code for this algorithm. b) (10 points) What loop invariant does this algorithm maintain? Note: You do not...
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1.
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1. return the second smallest index. Use the following header:public static int index of seconds sma11eststenent tint array
Use C Programming - Given an array of integers and a number K, find the smallest...
Use C Programming - Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1". Example: Input:     8     1 3 4 7 8 9 9 10     5     where: First line represents the number of elements in the array. Second line represents the elements in the array. Third line represents the value of K. Output:     7 Explanation:...
You will build some methods that search for the smallest element and the index of the...
You will build some methods that search for the smallest element and the index of the smallest element in an array in a recursive fashion. I know you can already do this without recursion. The point is to use recursion, so use it. public class Driver { public static void main(String[] args) { int[] one = {2, 4, 6, 1, 6, 3, 8}; int[] two = {43, 76, 3, 23, 95, 23}; int[] three = {9, 8, 7, 6, 5,...
(Data structure) Show the contents of the array below, once the “pivot” element is placed at...
(Data structure) Show the contents of the array below, once the “pivot” element is placed at its appropriate location after each call of the “Partition” algorithm, in the process of running Quick-Sort on said array. Arrange the data in descending order (from largest to smallest value). Always select the first element of the partition as “pivot” Apply sorting on the following data set s,       f,       p,      a,      g,      e,       v,      q,      i,        c
. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to...
. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are ≤x and all elements in the right portion A[m:hi] are ≥x) is the second element of the array to be split (i. e., A[lo+1]). For a specific value of n at least 13 (your choice), construct an assignment of the numbers 1...n to the n array elements...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT