Blog Archive

Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Sort an array of 0s, 1s and 2s

 Sort an array of 0s, 1s and 2s-Data Structures

 Sort an array of 0s, 1s and 2s-Data Structures

Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order

Example :

Input: 
N = 4
arr[] = {0 1 0 2}
Output:
0 0 1 2
Explanation:
0s 1s and 2s are segregated 
into ascending order

Solution:

void sort012(int a[], int n)
{
    
    int low = 0;
    int high = n - 1;
    int mid = 0;
    while (mid <= high)
    {
        if (a[mid] == 0)
        {
            swap(a[low], a[mid]);
            low++;
            mid++;
        }
        else if (a[mid] == 1)
        {
            mid++;
        }
        else
        {
            swap(a[mid], a[high]);
            high--;
        }
    }
}

Data Structures and Algorithm- Array Data Structyure

Thank You.

Count the Reversals

 Count the Reversals- Data Structures and Algorithms

Given a string S consisting only of opening and closing curly brackets '{' and '}' find out the minimum number of reversals required to make a balanced expression

Input
The first line of input contains an integer T, denoting the number of test cases. Then T test cases
follow. The first line of each test case contains a string S consisting only of { and }.

Output
Print out minimum reversals required to make S balanced. If it cannot be balanced, then print -1

Examples
Input
4
}{{}}{{{
{{}}}}
{{}{{{}{{}}{{
{{{{}}}}

Output
3
1
-1
0

Solution:

For better understanding of this code,you should Check Parenthesis Checker

#include <bits/stdc++.h>
using namespace std;
int main()
{
    //code
    int t;
    cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        if (s.length() % 2 != 0)
            cout << "-1" << endl;
        else
        {
            stack<char> stk;
            int c1 = 0;
            int c2 = 0;
            for (int i = 0; i < s.length(); i++)
            {
                char ch = s[i];
                if (ch == '{')
                {
                    stk.push(ch);
                    c2++;
                }
                else if (ch == '}' and !stk.empty() and stk.top() == '{')
                {
                    stk.pop();
                    c2--;
                }
                else
                {
                    c1++;
                }
            }

            if (c1 % 2 != 0)
                //checking odd even
                c1 = (c1 / 2) + 1;
            //odd then add 1 else remain same
            else
                c1 = c1 / 2;
            if (c2 % 2 != 0)
                //checking odd or even
                c2 = (c2 / 2) + 1;
            //odd then add 1 else remain same
            else
                c2 = c2 / 2;
            cout << c1 + c2 << endl;
        }
    }
    return 0;
}


Data structures and algorithms-String data structure


Split the binary string into substrings with equal number of 0s and 1s

 Split the binary string into substrings with equal number of 0s and 1s

Q.Given a binary string str of length N, the task is to find the maximum count of substrings str can be divided into such that all the substrings are balanced i.e. they have equal number of 0s and 1s. If it is not possible to split str satisfying the conditions then print -1

Example:

Input: str = “0100110101”
Output: 4
The required substrings are “01”, “0011”, “01” and “01”.

Input: str = “0111100010”
Output: 3


SOLUTION

#include <iostream >
    using namespace std;
int countBinary(string s, int n){
    int count0 = 0;
    int count1 = 0;
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '0') {
            count0++;
        }
        else {
            count1++;
        }
        if (count0 == count1) {
            count++;
        }
    }
    if (count0 != count1) {
        return -1;
    }

    return count;
}
int main() {
    string s;
    cin >> s;
    int n = s.length();
    int num = countBinary(s, n);
    cout << num;
    return 0;
}

Data Structures and Algorithms


Thank You 

Recent Post

Sort an array of 0s, 1s and 2s

  Sort an array of 0s, 1s and 2s- Data Structures   Sort an array of 0s, 1s and 2s- Data Structures Given an array of size N containing onl...