The sum of Elements Between k1th and k2th smallest elements17 Mar 2025 | 4 min read We need to find the sum of the elements between the given two indexes of the given vector. Where the user provides the two indexes and the input vector. Let us understand through an example: If the vector is [ 1 4 2 6 8 11 10] and k1, k2 are 2,5. Here, we must find the sum of numbers between the k1 and k2 indexes. But first, we need to sort the given vector and sum the values from the k1 to the k2 index. So, after sorting, the vector becomes as follows: [1 2 4 6 8 10 11]. If we consider the indexes form 0, the sum of values from index 2 to index 5 is 4+6+8+10 = 28. Now we need to implement a code to find the sum of values between the k1 and k2 index, including the k1 and k2 indexes: CODE: Output: ![]() Explanation: First, we have included the required libraries for the execution of the code. This part of the code will take the vector elements as input and get stored in the vector named vec. Next, we need to sort the vector to easily traverse the smallest elements from The above line of code will sort the given input vector, where sort is an inbuilt function that takes parameters as the address of the vector beginning and ending. Next, we need to prompt the user to enter the k1 and k2 indexes to find the sum of elements between the k1 and k2 indexes. This part of the code will handle taking input for ka and k2 values, and we also swap k1 k2 if k2<k1 because if k2<k1, we cannot iterate through the loop. So, we swapped both elements. Next, we need to calculate the sum from index k1 to index k2, and we will also print the values that make that sum. The above loop calculates the sum of elements between the k1 and k2 indexes, including k1 and k2 indexes. It will also print the elements that are making the sum. And finally, we will print the sum of values from index k1 to index k2. We will also handle cases where the user gives a k1 or k2 value greater than or equal to n. In this case, we cannot calculate the sum because we will get an error called vector index out of bound, i.e., we are trying to access the elements that are not in the vector. The below code is responsible for handling those cases. Output for handling this case is: ![]() Time and space complexity: Tim complexity: O(N*log(N)) for sorting the vector to calculate the sum in worst case O(N). Total time complexity is O(N)+O(N*log(N)). Space complexity: If we don't consider the space for input, then the code takes a constant amount of space. Next TopicPrint Matrix in a Snake Pattern |
In this article, we will look at how to implement left and right string rotation in Python. The step-by-step algorithms and code examples demonstrate the mechanics of rotating strings in either direction. We also discuss use cases and applications where string rotation is useful. The linear...
6 min read
Understanding s A stacked bar chart is a sort of bar chart that displays information in a stacked format. Each bar is split into a couple of sections, with each phase representing an exceptional category or subcategory of statistics. The peak of every segment represents the fee...
4 min read
Problem statement We are given a 0-indexed integer array nums. There exists an array arr of length nums. length, where arr[i] is the sum of |i - j| overall j such that nums[j] == nums[i] and j != i. If there is no such j, set arr[i]...
12 min read
Postfix notation: The general mathematical way of representing algebraic expressions is to write the operator between operands: Example: a + b. Such representation is called the Infix representation. If we write the operator after the operands Example: a b +, it is called the Postfix representation. It...
5 min read
There are a lot of operations that can be performed on a Linked List, like insertion operation where we can add a new node to the already existing linked list, deletion operation where we can delete a node from the linked list, and displaying the data...
30 min read
Ever had a situation where you needed to locate the equilibrium point in an array? Finding the equilibrium point on a scale, where the sum of the elements on either side equals the sum of the elements on the other, is analogous to doing so. The...
4 min read
Introduction Computer science relies heavily on sorting arrays, and there are a number of sorting algorithms available to make this process move quickly. Still, there are situations in which typical sorting techniques need to be improved, including sorting strings according to a user-specified alphabetical order. In these...
4 min read
Introduction: In the field of graph theory, finding the minimum spanning tree (MST) of a given graph is a common problem with numerous applications. MSTs are used in various fields, such as network design, clustering, and optimization. Two popular algorithms to solve this problem are Prim's and...
12 min read
Introduction Burning a binary tree beginning from a particular node is a fascinating issue in computer science, frequently experienced in algorithmic interviews and competitive programming. This task includes reproducing the spread of fire from a given node all through the binary tree and deciding the time it...
4 min read
Introduction Dividing a set into disjoint subsets is an issue that the Union-Find Algorithm addresses, sometimes referred to as the Disjoint-Set Data Structure. Union joins two subsets and Find which ascertains which subset a specific element belongs to, are its two main operations. Every element is first...
8 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India