Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Find Value after performing Increment Decrement queries

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given a variable X having an initial value 0 and an array of queries Q[] of size N containing the type of operations, the task is to return the value of N after performing all the below operations:

  • Type-1: Increment the value of X by 1.
  • Type-2: Decrement the value of X by 1. 

Examples: 

Input: Q = {2, 1, 1}
Output: 1
Explanation: The operations are performed as follow:
Initially, X = 0, 
Query 1(Type 2): X is decremented by 1, X =  0 – 1 = -1 
Query 2(Type 1): X is incremented by 1, X = -1 + 1 = 0
Query 3(Type 1): X is incremented by 1, X = 0 + 1 = 1
Hence, the output will be 1

Input: Q = {1, 1, 1}
Output: 3

 

Approach: To solve the problem follow the below idea: 

  • Traverse the given array of Queries. 
  • Whenever the array element is 1, increment N by 1, and 
  • Whenever the array element is 2, decrement N by 1.
  • Return the final value of N as the required answer.

Below is the implementation for the above approach:

C++




// C++ code for the above approach.
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to findvalue after queries
int findvalue(vector<int>& queries)
{
    // Storing array size in n
    int n = queries.size();
    int x = 0;
    for (int i = 0; i < n; i++) {
 
        // If query is of type 1, increment
        if (queries[i] == 1) {
            x = x + 1;
        }
 
        // Else query is of type 2, decrement
        else {
            x = x - 1;
        }
    }
    return x;
}
 
// Drivers code
int main()
{
    // Declaring array of integers
    // which contains queries
    vector<int> queries = { 2, 1, 1 };
    int ans = findvalue(queries);
 
    // Function Call
    cout << ans;
    return 0;
}

Java




// Java code for the above approach.
public class GFG {
     
    // Function to findvalue after queries
    static int findvalue(int []queries)
    {
        // Storing array size in n
        int n = queries.length;
        int x = 0;
        for (int i = 0; i < n; i++) {
 
            // If query is of type 1, increment
            if (queries[i] == 1) {
                x = x + 1;
            }
 
            // Else query is of type 2, decrement
            else {
                x = x - 1;
            }
        }
        return x;
    }
 
    // Drivers code
    public static void main(String[] args)
    {
        // Declaring array of integers
        // which contains queries
        int []queries = { 2, 1, 1 };
        int ans = findvalue(queries);
 
        // Function Call
        System.out.println(ans);
    }
}
 
// This code is contributed by AnkThon

Python3




# Python3 code for the above approach.
 
# Function to findvalue after queries
def findvalue(queries)  :
 
    # Storing array size in n
    n = len(queries);
    x = 0;
    for i in range(n) :
 
        # If query is of type 1, increment
        if (queries[i] == 1) :
            x = x + 1;
 
        # Else query is of type 2, decrement
        else :
            x = x - 1;
    return x;
 
# Drivers code
if __name__ == "__main__" :
 
    # Declaring array of integers
    # which contains queries
    queries = [ 2, 1, 1 ];
    ans = findvalue(queries);
 
    # Function Call
    print(ans);
     
    # This code is contributed by AnkThon

C#




// C# code for the above approach.
 
using System;
using System.Collections.Generic;
 
class GFG {
    // Function to findvalue after queries
    static int findvalue(List<int> queries)
    {
        // Storing array size in n
        int n = queries.Count;
        int x = 0;
        for (int i = 0; i < n; i++) {
 
            // If query is of type 1, increment
            if (queries[i] == 1) {
                x = x + 1;
            }
 
            // Else query is of type 2, decrement
            else {
                x = x - 1;
            }
        }
        return x;
    }
 
    // Drivers code
    public static void Main(string[] args)
    {
        // Declaring array of integers
        // which contains queries
        List<int> queries = new List<int>{ 2, 1, 1 };
        int ans = findvalue(queries);
 
        // Function Call
        Console.Write(ans);
    }
}
 
// This code is contributed by phasing17

Javascript




<script>
// Javascript code for the above approach.
 
// Function to findvalue after queries
function findvalue( queries)
{
    // Storing array size in n
    let n = queries.length;
    let x = 0;
    for (let i = 0; i < n; i++) {
 
        // If query is of type 1, increment
        if (queries[i] == 1) {
            x = x + 1;
        }
 
        // Else query is of type 2, decrement
        else {
            x = x - 1;
        }
    }
    return x;
}
 
// Drivers code
 
    // Declaring array of integers
    // which contains queries
    let queries = [ 2, 1, 1 ];
    let ans = findvalue(queries);
 
    // Function Call
   document.write(ans);
    
   // This code is contributed by satwik4409.
   </script>

Output

1

Time Complexity: O(N). 
Auxiliary Space: O(1). 


My Personal Notes arrow_drop_up
Last Updated : 21 Jul, 2022
Like Article
Save Article
Similar Reads
Related Tutorials