
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to find permutation for which sum of adjacent elements sort is same as given array
Suppose we have an array A with n elements. A function F(p) is a sorted array of sums of adjacent elements in p. So F(p) = sort([p1 + p2, p2 + p3, ... pn-1 + pn]). We have a permutation represented in A. We have to find the different permutation of A where F(A) is same.
So, if the input is like A = [2, 1, 6, 5, 4, 3], then the output will be [1, 2, 5, 6, 3, 4], because F(A)=sort([2+1, 1+6, 6+5, 5+4, 4+3]) = sort([3, 7, 11, 9, 7]) = [3,7,7,9,11]. And sort([1+2, 2+5, 5+6, 6+3, 3+4]) = sort([3, 7, 11, 9, 7]) = [3, 7, 7, 9, 11]. (There can be other answers too)
Steps
To solve this, we will follow these steps −
n := size of A for initialize i := n - 1, when i >= 0, update (decrease i by 1), do: print A[i]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(vector<int> A) { int n = A.size(); for (int i = n - 1; i >= 0; i--) cout << A[i] << ", "; } int main() { vector<int> A = { 2, 1, 6, 5, 4, 3 }; solve(A); }
Input
{ 2, 1, 6, 5, 4, 3 }
Output
3, 4, 5, 6, 1, 2,
- Related Articles
- Adjacent elements of array whose sum is closest to 0 - JavaScript
- Program to find number of elements in all permutation which are following given conditions in Python
- Program to find smallest index for which array element is also same as index in Python
- Program to find sum of elements in a given array in C++
- C/C++ Program to find the sum of elements in a given array
- C++ Program for BogoSort or Permutation Sort?
- Python Program for BogoSort or Permutation Sort
- Program to find largest sum of non-adjacent elements of a list in Python
- Pair of (adjacent) elements of an array whose sum is lowest JavaScript
- Program to count index pairs for which array elements are same in Python
- C++ program to find range whose sum is same as n
- Program to find sum of non-adjacent elements in a circular list in python
- C++ Program for the BogoSort or Permutation Sort?
- Array range queries for elements with frequency same as value in C Program?
- Program to check we can find four elements whose sum is same as k or not in Python

Advertisements