Check if given Array can be formed by increasing or decreasing element
Last Updated :
25 Jan, 2023
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++
// 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;
}
Java
// Java code to implement the above approach
import java.io.*;
import java.util.*;
class GFG {
static int solve(Vector<Integer> v)
{
long prev = 0;
long n = v.size();
// Remove all the consecutive zeros from back
// till we get a non-zero element
while (!v.isEmpty() && v.lastElement() == 0) {
v.remove(v.size() - 1);
}
// All the elements were zeros so return 1
if (v.isEmpty()) {
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.get(0) <= 0
|| v.lastElement() > 0) {
return 0;
}
for (int i = (int)(v.size() - 1); i > 0; i--) {
prev = prev - v.get(i);
if (prev <= 0)
return 0;
}
if (prev == v.get(0))
return 1;
return 0;
}
public static void main(String[] args)
{
Vector<Integer> vec = new Vector<>();
vec.add(1);
vec.add(-1);
vec.add(0);
System.out.println(solve(vec));
Vector<Integer> vec2 = new Vector<>();
vec2.add(3);
vec2.add(-1);
vec2.add(-2);
vec2.add(0);
System.out.println(solve(vec2));
Vector<Integer> vec3 = new Vector<>();
vec3.add(1);
vec3.add(1);
vec3.add(1);
vec3.add(0);
System.out.println(solve(vec3));
}
}
// This code is contributed by lokesh.
Python3
# Python code to implement the above approach
# Function to find is it possible to make
# the array or not
def solve(v):
prev = 0;
n = len(v);
# Remove all the consecutive zeros from back
# till we get a non-zero element
while (len(v)>0 and v[len(v)-1] == 0):
v.pop();
# All the elements were zeros so return 1
if (len(v)==0) :
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 (len(v) == 1 or v[0] <= 0 or v[len(v)-1] > 0) :
return 0;
for i in range (len(v) - 1, 0, -1) :
prev = prev - v[i];
if (prev <= 0):
return 0;
if (prev == v[0]):
return 1;
return 0;
# Driver Code
vec = [ 1, -1, 0];
print(solve(vec));
vec2 = [ 3, -1, -2, 0];
print(solve(vec2));
vec3 = [ 1, 1, 1, 0];
print(solve(vec3));
JavaScript
// Javascript code to implement the above approach
// Function to find is it possible to make
// the array or not
function solve(v)
{
let prev = 0, n = v.length
// Remove all the consecutive zeros from back
// till we get a non-zero element
while (v.length!=0 && v[v.length-1] == 0) {
v.pop()
}
// All the elements were zeros so return 1
if (v.length==0) {
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.length == 1 || v[0] <= 0 || v[v.length-1] > 0) {
return 0
}
for (let i = v.length - 1; i > 0; i--) {
prev = prev - v[i]
if (prev <= 0)
return 0
}
if (prev == v[0])
return 1
return 0
}
let vec = [1, -1, 0]
console.log(solve(vec))
let vec2 = [3, -1, -2, 0 ]
console.log(solve(vec2))
let vec3 = [1, 1, 1, 0 ]
console.log(solve(vec3))
// This code is contributed by poojaagarwal2.
C#
// C# code to implement the above approach
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
static int Solve(List<int> v)
{
long prev = 0;
long n = v.Count;
// Remove all the consecutive zeros from back
// till we get a non-zero element
while (v.Count > 0 && v.Last() == 0)
{
v.RemoveAt(v.Count - 1);
}
// All the elements were zeros so return 1
if (v.Count == 0)
{
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.Count == 1 || v[0] <= 0
|| v.Last() > 0)
{
return 0;
}
for (int i = v.Count - 1; i > 0; i--)
{
prev = prev - v[i];
if (prev <= 0)
return 0;
}
if (prev == v[0])
return 1;
return 0;
}
// Driver code
public static void Main(string[] args)
{
List<int> vec = new List<int> { 1, -1, 0 };
Console.WriteLine(Solve(vec));
List<int> vec2 = new List<int> { 3, -1, -2, 0 };
Console.WriteLine(Solve(vec2));
List<int> vec3 = new List<int> { 1, 1, 1, 0 };
Console.WriteLine(Solve(vec3));
}
}
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Similar Reads
Check if Array element can be incremented to X by using given operations Given an array A[] of size N, and an integer X, the following operation can be performed on that array: Choose any element, say Y from the array.Then every element in the array except Y are incremented.In the next step, only the value Y+1 can be chosen and the steps are repeated until Y+1 is not pre
7 min read
Check whether an array can be made strictly decreasing by modifying at most one element Given an array arr[] of positive integers, the task is to find whether it is possible to make this array strictly decreasing by modifying at most one element.Examples: Input: arr[] = {12, 9, 10, 5, 2} Output: Yes {12, 11, 10, 5, 2} is one of the valid solutions.Input: arr[] = {1, 2, 3, 4} Output: No
8 min read
Check if an array is increasing or decreasing Given an array arr[] of N elements where N ? 2, the task is to check the type of array whether it is: Increasing.Decreasing.Increasing then decreasing.Decreasing then increasing. Note that the given array is definitely one of the given types.Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: Increasin
6 min read
Check if given Array can be rearranged as increasing, decreasing or a hill sequence Given an array arr[] of size N. The task is to find the number of sequences in which any one of the following conditions is met: The given sequence can be re-arranged into strictly increasing order, orThe given sequence can be re-arranged into strictly decreasing order, orThe given sequence can be r
10 min read
Check if given Array can be formed from initial Array having one element Given array A[], the task for this problem is to check whether A[] can be formed from array B[] = {1} by performing the following operation any number of times, select a subsequence of B[] and insert total sum of this subsequence in B[] to any position. Print "YES" if it is possible "NO otherwise. E
7 min read