- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ code to find array from given array with conditions
Suppose we have an array A with n elements. There is another hidden array B of size n. The elements can be negative or positive. For each index i in range 1 to n, following operations will be performed −
Set A[i] as 0 initially
Then add B[i] to A[i], subtract B[i+1], then add B[i+2] and so on
We have to find the array B.
So, if the input is like A = [6, -4, 8, -2, 3], then the output will be [2, 4, 6, 1, 3]
Steps
To solve this, we will follow these steps −
for initialize i := 0, when i < size of A, update (increase i by 1), do: print (A[i] + A[i + 1])
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(vector<int> A){ for (int i = 0; i < A.size(); i++) cout << A[i] + A[i + 1] << ", "; } int main(){ vector<int> A = { 6, -4, 8, -2, 3 }; solve(A); }
Input
{ 6, -4, 8, -2, 3 }
Output
2, 4, 6, 1, 3,
- Related Articles
- C++ code to find sorted array with non-divisibility conditions
- C++ code to count local extrema of given array
- Count valid pairs in the array satisfying given conditions in C++
- Find the Initial Array from given array after range sum queries in C++
- Find a pair from the given array with maximum nCr value in C++
- Split the array into equal sum parts according to given conditions in C++
- How to update array with multiple conditions in MongoDB
- C++ code to find minimum correct string from given binary string
- How to find the target sum from the given array by backtracking using C#?
- How to find the distinct subsets from a given array by backtracking using C#?
- Find maximum height pyramid from the given array of objects in C++
- C++ Program to find Number Whose XOR Sum with Given Array is a Given Number k
- C++ code to count operations to make array sorted
- Find a pair from the given array with maximum nCr value in Python
- Program to find array of length k from given array whose unfairness is minimum in python

Advertisements