Single File Programming QuestionProblem StatementGiven an integer array sequence of size n, write a program to find the equilibrium index of the array.The equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. ExampleInput: 7-7 1 5 2 -4 3 0Output:3Explanation:3 is an equilibrium index, because, A[0] + A[1] + A[2] = A[4] + A[5] + A[6].Note: This is a question asked in Amazon and Microsoft coding tests.Input format :The first line of input consists of an integer n, representing the size of the array.The second line consists of n space-separated integers, representing the array elements.Output format :The output prints an integer representing the equilibrium index of the given array.If there is no such index found, print "No equilibrium index found."Refer to the sample output for formatting specifications.Code constraints :1 ≤ n ≤ 10-10 ≤ array elements ≤ 10Sample test cases :Input 1 :7-7 1 5 2 -4 3 0Output 1 :3Input 2 :31 2 3Output 2 :No equilibrium index found.
Question
Single File Programming QuestionProblem StatementGiven an integer array sequence of size n, write a program to find the equilibrium index of the array.The equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. ExampleInput: 7-7 1 5 2 -4 3 0Output:3Explanation:3 is an equilibrium index, because, A[0] + A[1] + A[2] = A[4] + A[5] + A[6].Note: This is a question asked in Amazon and Microsoft coding tests.Input format :The first line of input consists of an integer n, representing the size of the array.The second line consists of n space-separated integers, representing the array elements.Output format :The output prints an integer representing the equilibrium index of the given array.If there is no such index found, print "No equilibrium index found."Refer to the sample output for formatting specifications.Code constraints :1 ≤ n ≤ 10-10 ≤ array elements ≤ 10Sample test cases :Input 1 :7-7 1 5 2 -4 3 0Output 1 :3Input 2 :31 2 3Output 2 :No equilibrium index found.
Solution
Here is a Python solution for the problem:
def equilibrium_index(arr):
total_sum = sum(arr)
left_sum = 0
for i, num in enumerate(arr):
total_sum -= num
if left_sum == total_sum:
return i
left_sum += num
return 'No equilibrium index found.'
# Test the function
print(equilibrium_index([-7, 1, 5, 2, -4, 3, 0])) # Output: 3
print(equilibrium_index([1, 2, 3])) # Output: 'No equilibrium index found.'
This function works by first calculating the total sum of the array. Then it iterates over the array, subtracting
Similar Questions
Single File Programming QuestionProblem StatementYou are given an integer array of N elements. Find out the sum of the first N/2 elements and the sum of the last N/2 elements using pointers.ExampleInput: 102 2 2 2 2 3 3 3 3 3Output: 1015Explanation:N = 10 -> N/2 = 5sum of the first 5 elements = 2+2+2+2+2 = 10sum of the last 5 elements = 3+3+3+3+3 = 15Note: This question helps in clearing conceptual interviews of service-based companies.Input format :The first line of input consists of an integer N, representing the number of array elements.The second line consists of N space-separated integers, representing the array of elements.Output format :The first line of output prints the sum of the first N/2 elements of the given array.The second line prints the sum of the last N/2 elements of the given array.Refer to the sample output for formatting specifications.Code constraints :N is an even number.10 ≤ N ≤ 50-50 ≤ array elements ≤ 50Sample test cases :Input 1 :102 2 2 2 2 3 3 3 3 3Output 1 :1015Input 2 :207 26 17 23 -6 -23 26 -23 -25 -18 -18 -5 3 22 -9 -22 -28 18 -16 21Output 2 :4-34
Single File Programming QuestionProblem Statement:Thiru is working on a grading system for his class of students. He needs a program that takes input for student scores, inserts a new score at the beginning and end of the existing scores, and then displays the modified list of scores.Write a program to help Thiru achieve this.Input format :The first line of input is an integer, the value n, indicating the number of elements in the array.The second line of input consists of n space-separated integers, representing the elements of the array arr[i].The third line of input consists of two integers M and P, representing the value to be inserted at the beginning and ending of the array, separated by a space.Output format :The output is a single line containing n + 2 space-separated integers, which represent the modified array after inserting the element at the beginning and ending of the existing scores.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases will fall under the following constraints:1 ≤ n ≤ 101 ≤ arr[i] ≤ 1001 ≤ M, P ≤ 100Sample test cases :Input 1 :53 4 5 6 72 8Output 1 :2 3 4 5 6 7 8 Input 2 :14590 78Output 2 :90 45 78 Input 3 :1098 37 48 28 16 18 20 100 25 131 19Output 3 :1 98 37 48 28 16 18 20 100 2
Single File Programming QuestionProblem StatementKrish is a computer science student who loves solving challenging problems. Recently, he came across an interesting problem related to arrays. The problem requires Krish to find all pairs of elements in a given array whose sum equals a specific target value. Write a program for Krish that takes an array of integers and a target sum as input. The program should print all pairs of elements from the array whose sum equals the target sum. If no such pairs are found, it should print "Not found".ExampleInput:5 // number of elements10 20 30 40 50 // elements60 // target sumOutput:10 5020 40Explanation: The pair with 60 as sum are 20 + 40 = 60 and 10 + 50 = 60.Input format :The first line consists of an integer N, representing the number of elements in the array.The second line consists of N space-separated integers, representing the elements of the array.The third line consists of an integer M, representing the target sum.Output format :For each pairs found, print two integers separated by a space, whose sum equals the target sum.If no such pairs are found, print "Not found".Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases will fall under the following constraints:2 ≤ N ≤ 251 ≤ Each element ≤ 1003 ≤ M ≤ 200Sample test cases :Input 1 :32 3 65Output 1 :2 3Input 2 :510 20 30 40 5060Output 2 :10 5020 40Input 3 :42 3 4 78Output 3 :Not foundInput 4 :310 6 410Output 4 :6 4
Initialize an array/list with five integers. Write a program to find the sumand average of these n
Swap the contents and indexes in an array.Max. score: 100Given an array with size n and its contents are from 0 to n-1. Swap the index and contents at that index.INPUT:Accept the size and its corresponding values.OUTPUT:Print the processed array.Explaination:input:a[0] = 3 a[1] = 2 a[2] = 4 a[3] = 1 a[4] = 0output:a[0] = 4 a[1] = 3 a[2] = 1 a[3] = 0 a[4] = 2SAMPLE INPUT 54 2 0 1 3SAMPLE OUTPUT 2 3 1 4 0
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.