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 an often-used survey system, customers are asked to rate a companys product between 0 and 11 points, inclusive. Customers who chose scores between 0 and 6 points, inclusive, are interpreted to be...
-
Vermeulen booked a trip to Machu Picchu in Peru through Worldwide Holidays travel agency in Florida. Worldwide received a commission from the Peruvian tour agency Chasquitur, when in Peru, the driver...
-
This case arose after Felix DeWeldon, a well-known sculptor and art collector, sold three paintings to Robert McKean in 1994. Felix DeWeldon declared bankruptcy in 1991. In 1992, DeWeldon, Ltd.,...
-
Determining amount to borrow and pro forma statement balances Athena Sudsberry owns a small restaurant in New York City. Ms. Sudsberry provided her accountant with the following summary information...
-
Provide a definition of a database management system (DBMS) and discuss the purpose(s) in business. Introduce the business scenario to analyze and discuss the benefits of implementing a database...
-
(a) Using the Consolidated Balance Sheets for Walgreen Co. for August 31, 2013 and 2012, prepare a common-size balance sheet. (b) Which current asset is the most significant? Which noncurrent asset...
-
BioFlix Activity: Cellular Respiration and Photosynthesis - Chemical Cycling Can you identify how chemicals cycle in an ecosystem? To review the chemical inputs and outputs of cellular respiration...
-
Umbrella Co's gross profit margin increased from 41.5% in year 1 to 44.3% in year 2. Possible reasons may include: 1. Cost of obsolete product passed on to the customer. 2. Use of early payment...
-
The Grist Mill just paid a dividend of $3.46 per share on its stock. The dividends are expected to grow at a constant rate of 4.5 percent per year, indefinitely. What will the price of this stock be...
-
What changes are needed in resource and supply planning for a health care system to be prepared for a surge in demand during the next epidemic? What are some innovative strategies and ideas that can...
-
Swapping battery system on tesla electric charging station market segmentation, Describe market segment and Provide each segment example.
-
Market Segmentation of Battery Swapping system at Tesla Charging station, 1. Identify all potential market segments that could be served by applying the bases of segmentation. 2. Utilize secondary...
-
Instructions: Choose one of the following prompts and provide an answer that includes any necessary graphs. Be sure to label everything and use the four steps Instructions for narrative: Think of...
-
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.
-
Rank in order, from largest to smallest, the size of the friction forces in the five different situations (one or more friction forces could be zero). The box and the floor are made of the same...
-
The terminal speed of a Styrofoam ball is 15 m/s. Suppose a Styrofoam ball is shot straight down with an initial speed of 30 m/s. Which velocity graph is correct? Vy (m/s) Vy (m/s) v, (m/s) vy (m/s)...
-
Boxes P and Q are sliding to the right across a frictionless table. The hand His slowing them down. The mass of P is larger than the mass of Q. Rank in order, from largest to smallest, the horizontal...
Study smarter with the SolutionInn App