
- 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 array after inserting new elements where any two elements difference is in array
Suppose we have an array A with n distinct elements. An array B is called nice if for any two distinct elements B[i] and B[j], the |B[i] - B[j]| appears in B at least once, and all elements in B will be distinct. We have to check whether we can add several integers in A to make it nice of size at most 300. If possible return the new array, otherwise return -1.
So, if the input is like A = [4, 8, 12, 6], then the output will be [8, 12, 6, 2, 4, 10], because |4−2| = |6−4| = |8−6| = |10−8| = |12−10| = 2 is in the array, |6−2| = |8−4| = |10−6| = |12−8| = 4 is in the array, |8−2| = |10−4| = |12−6| = 6 is in the array, |10−2| = |12−4| = 8 is in the array, and |12−2| = 10 is in the array, so the array is nice. (Other answers are also possible)
Steps
To solve this, we will follow these steps −
n := size of A t := 0 b := 0 for initialize i := 0, when i < n, update (increase i by 1), do: a := A[i] if a < 0, then: t := 1 b := maximum of a and b if t is non-zero, then: print -1 Otherwise for initialize i := 0, when i <= b, update (increase i by 1), do: print 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(); int t = 0; int b = 0; for (int i = 0; i < n; i++) { int a = A[i]; if (a < 0) t = 1; b = max(a, b); } if (t) cout << "-1"; else { for (int i = 0; i <= b; i++) cout << i << ", "; } } int main() { vector<int> A = { 4, 8, 12, 6 }; solve(A); }
Input
{ 4, 8, 12, 6 }
Output
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
- Related Articles
- Inserting elements in an array using C Language
- Swift Program to get array elements after N elements
- Golang Program to get array elements after N elements
- C++ program to find array after removal of left occurrences of duplicate elements
- Program to find mean of array after removing some elements in Python
- Accumulating array elements to form new array in JavaScript
- C# Program to order array elements
- C program to find the unique elements in an array.
- Find set of m-elements with difference of any two elements is divisible by k in C++
- JavaScript - Constructs a new array whose elements are the difference between consecutive elements of the input array
- Swift Program to Find Common Array Elements
- Golang Program To Find Common Array Elements
- Python program to find common array elements
- Find all elements in array which have at-least two greater elements in C++
- C# program to find all duplicate elements in an integer array
