3. Coding Question: For the index position in an array where the sum of elements at...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
3. Coding Question: For the index position in an array where the sum of elements at lower indexes is equal to the sum of elements at higher indexes. Write a function int balanced(int[] arr); that given a sequence arr[] of size n, returns the index (if any) or -1 if no index position is found. a. Include comments within your code. Be detailed. b. What other approaches did you consider to solve this question and why did you choose this approach? c. List any assumptions. Examples Input: A[] = [-8, 2, 6, 2, -5, 4, 1] Output: 3 3 is an index that works, because: A[0] + A[1] + A[2] = A[4] + A[5] + A[6] Input: A[] = {2, 4, 6} Output: -1 because no appropriate index exists Index 3 Sum: 6 Sum: 6 Input: [4, -3, 5, 1, 2, 5, -1] Output: 3 Microsoft CYBORGMOBILE Index 1 Sum: 16 Sum: 16 Input: [16, 8, 2, 4, 4, 6] Output. 1 Index 5 Sum:7 Sum: 7 Input: [2, 4, 4, 0, -3, 3, 1, 6] Output: 5 Input: [2, 3, 5, 5, 2] Output: -1 Input: [2, -1, 1] Output. 5 3. Coding Question: For the index position in an array where the sum of elements at lower indexes is equal to the sum of elements at higher indexes. Write a function int balanced(int[] arr); that given a sequence arr[] of size n, returns the index (if any) or -1 if no index position is found. a. Include comments within your code. Be detailed. b. What other approaches did you consider to solve this question and why did you choose this approach? c. List any assumptions. Examples Input: A[] = [-8, 2, 6, 2, -5, 4, 1] Output: 3 3 is an index that works, because: A[0] + A[1] + A[2] = A[4] + A[5] + A[6] Input: A[] = {2, 4, 6} Output: -1 because no appropriate index exists Index 3 Sum: 6 Sum: 6 Input: [4, -3, 5, 1, 2, 5, -1] Output: 3 Microsoft CYBORGMOBILE Index 1 Sum: 16 Sum: 16 Input: [16, 8, 2, 4, 4, 6] Output. 1 Index 5 Sum:7 Sum: 7 Input: [2, 4, 4, 0, -3, 3, 1, 6] Output: 5 Input: [2, 3, 5, 5, 2] Output: -1 Input: [2, -1, 1] Output. 5
Expert Answer:
Answer rating: 100% (QA)
include int main int n a30ijklsum0rsum0f0 here we take the size of array cinn cout ai this ... View the full answer
Related Book For
Discrete Mathematics and Its Applications
ISBN: 978-0073383095
7th edition
Authors: Kenneth H. Rosen
Posted Date:
Students also viewed these programming questions
-
Consider the diode bridge rectifier shown below with the voltage regulating capacitor in parallel with the load resistor. 60Hz 120V 10:1 C-470F IL R=1kQ a) For the input signal, sketch waveforms of...
-
You are given a sequence of n elements to sort. The input sequence consists of n/k subsequences, each containing k elements. The elements in a given subsequence are all smaller than the elements in...
-
Q1. Suppose player A and player B are playing the following game. Player A can choose "Up" and "Down", while player B can choose "Left" and "Right". The payoff matrix is depicted below. Player A's...
-
All of the following complications are more common in ventouse assisted delivery than forceps except: a) Subgaleal hemorrhage b) Cephalhematoma c) Intracranial hemorrhage d) Transient lateral rectus...
-
In 2011, Milton Thayne, president of Carbondale Electronics, received a report indicating that quality costs were 31 percent of sales. Faced with increasing pressures from imported goods, Milton...
-
What is the MFD? UFD? How are they related?
-
Compute the following items for the statement of cash flows: a. Beginning and ending Retained Earnings are \($45,000\) and \($70,000,\) respectively. Net income for the period is \($60,000.\) How...
-
Cortez Co. began operations on July 1. It uses a perpetual inventory system. During July, the company had the following purchases and sales. Instructions(a) Determine the ending inventory under a...
-
Paula Corporation reports the following for this month: The beginning inventory is 30 units. It produces 140 units during this month. And 150 units are sold at $3,000 each. The budgeted level of...
-
Kiss Cosmetics Inc. has a 4-year 5% coupon bond (annual coupon payments) with a face value of $1,000. The spot rate term structure is shown in the table below. A) Calculate the discount factors, PV...
-
What are the major objectives addressed by the test data method? When should test data be used; that is, when does test data have a competitive advantage over other audit approaches? What are the...
-
Embedded audit modules were recommended as an approach that could work well in various environments. Briefly describe an embedded audit module, how it works, and the audit objectives that it could...
-
Which of the following statements is true? a. If control risk is assessed at maximum, the nature of related substantive tests should be changed from more to less effective. b. If control risk is...
-
What types of considerations go into selecting and developing a deployment site?
-
What is an intelligent agent? Identify two major ways in which intelligent agents might be utilized to enhance the efficiency of an audit engagement.
-
What method should you use for this Amazon yen transaction to avoid paying extra fees? To pay with the Japanese Yen or US dollars?
-
Suppose you won a financial literacy competition and are given FJS10000 to invest, with the condition that investment can be done either in, i) Invest in Unit trust of Fiji or Invest in Fijian...
-
In Exercise find the number of vertices, the number of edges, and the degree of each vertex in the given undirected graph. Identify all isolated and pendant vertices. d e
-
How many 4-element RNA sequences a) Contain the base U? b) Do not contain the sequence CUG? c) Do not contain all four bases A, U, C, and G? d) Contain exactly two of the four bases A, U, C, and G?
-
Devise an algorithm that finds all equal pairs of sums of two terms of a sequence of n numbers, and determine the worst-case complexity of your algorithm.
-
What is the IQR? What does the IQR represent? The population parameters below describe the full-time equivalent number of students (FTES) each year at Lake Tahoe Community College from 19761977...
-
How many standard deviations away from the mean is the median? The population FTES for 20052006 through 20102011 was given in an updated report. The data are reported here. The population parameters...
-
A class of 20 students has a mean grade of 80 on a test. Nineteen of the students has a mean grade between 79 and 82, inclusive. a. What is the lowest possible grade of the other student? b. What is...
Study smarter with the SolutionInn App