

Check if given Array can be formed by increasing or decreasing element
source link: https://www.geeksforgeeks.org/check-if-given-array-can-be-formed-by-increasing-or-decreasing-element/?utm_campaign=newhomepage
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Check if given Array can be formed by increasing or decreasing element
Given an array arr[] of length N with N integers. Assume all the elements of array arr[] are equal to 0 and start with the first element. The task is to obtain the given array after performing the given operations on it. At last, you must be there from where you start. Return 1 if it is possible to obtain the array arr[] after some operations (possibly zero) else print 0.
- Increase the element at which you are by 1. Then move to the next element if exists.
- Decrease the element at which you are by 1. Then move to the previous element if exists.
Examples:
Input: arr[] = {1, -1, 0}
Output: 1
Explanation: First the array arr[] is ={0, 0, 0} and you are on the first index i.e., 0.
Move right by one, the element at index 0 is increased by 1 arr[] = {1, 0, 0}, Now, you are on index 1.
Move left by one, the element at index 1 is decreased by 1 arr[] = {1, -1, 0}, Now, you are on index 0.
The array arr[] can be achieved by a finite number of operations i.e., 2, So, print 1.Input: arr[] = {3, -1, -2, 0}
Output: 1
Approach:
The basic idea is to eliminate all the consecutive zeroes from the end of the array till we get a non-zero element and then perform the given operations.
Follow the below steps to solve the problem:
- Initialize a variable prev = 0 and n = size of vector v.
- Remove all the consecutive zeros from back till we get a non-zero element.
- If the vector is empty it means all the elements were zeros so return 1.
- If there is only one element left in the array
- If the first element is less than equal to 0 or
- If the last element is greater than 0, then return 0.
- Else run a loop from n-1 to 1 index of the array and subtract v[i] from Prev.
- If Prev is less than or equal to 0 at any point of time then return 0.
- After executing the loop if Prev = v[0] then return 1, else return 0.
Below is the implementation of the above approach:
// C++ code to implement the above approach #include <bits/stdc++.h> #define ll long long using namespace std; // Function to find is it possible to make // the array or not bool solve(vector<int> v) { ll prev = 0, n = v.size(); // Remove all the consecutive zeros from back // till we get a non-zero element while (!v.empty() && v.back() == 0) { v.pop_back(); } // All the elements were zeros so return 1 if (v.empty()) { return 1; } // If there is only one element left in the array // or the first element is less than equal to 0 // or the last element is greater than 0 then return 0 if (v.size() == 1 || v[0] <= 0 || v.back() > 0) { return 0; } for (ll i = v.size() - 1; i > 0; i--) { prev = prev - v[i]; if (prev <= 0) return 0; } if (prev == v[0]) return 1; return 0; } // Driver Code int main() { vector<int> vec = { 1, -1, 0 }; cout << solve(vec) << endl; vector<int> vec2 = { 3, -1, -2, 0 }; cout << solve(vec2) << endl; vector<int> vec3 = { 1, 1, 1, 0 }; cout << solve(vec3) << endl; return 0; }
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Recommend
-
72
README This is a mirror of http://www.vim.org/scripts/script.php?script_id=670 The vis...
-
11
Improve Article Check if a given array can be divided into pairs with even sumDifficulty Level : ExpertLast Updated : 28 Apr, 2021...
-
8
Volume Automatically Increasing or Decreasing in Windows 10? Here’s How to Fix It By Tashreef Shareef Published 20 hours ago ...
-
6
Largest Number formed from an ArrayLargest Number formed from an Array | SDE Sheet | ArraysVideo Player is loading.Loaded: 0%Remaining Time -8:28
-
11
Generate an N-length array having length of non-decreasing subarrays maximized and minimum difference between first and last array elementsLast Updated : 30 Jul, 2021Given...
-
3
Interview Problem — Make array increasing and then decreasing Interview Problem — Make array increasing and then decreasing ...
-
5
Check if given Array can be formed from initial Array having one elementi_am_gauravGiven array A[], the task for this problem is to check whether A...
-
3
Operations to Sort an Array in non-decreasing orderGiven an array arr[] of integers of size n, the task is to check if we can sort the given array in non-decreasing order(i, e.
-
10
Sort an Array by decreasing and increasing elements with adjacent swapsSkip to content
-
9
How to check an Array Contains an Object of Attribute with Given Value in JavaScript ?Sk...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK