Check whether the given String is good for any ascending Array or not
Last Updated :
09 Jan, 2024
Given a string S of length N. Consider any ascending array A[] such that each A[i] > 0 for all (1 <= i <= N) and some conditions, the task is to output YES/NO, by checking that the above-given conditions are true for all possible ascending A[]’s or not with given S.
- Sum up all the elements of A[] at each index i, where S[i] = 1 in a variable let’s say X.
- Sum up all the elements of A[] at each index i, where S[i] = 0 in a variable let’s say Y.
- Then |X – Y| must be less than or equal to Max(A[]).
Any array is said to be ascending, If all element except the last is less than or equal to its next element. Formally, A[] = {A1 <= A2 <= A3 <= A4 . . . . <= A5}.
Examples: All the below arrays are ascending arrays.
- A[] = {1, 2, 3, 5}
- A[] = {5, 6, 7, 8}
- A[] = {2, 2, 2, 2}
- A[] = {3, 6, 8, 8}
Examples:
Input: N = 4, S = 1010
Output: YES
Explanation: Let us check the given S for multiple ascending arrays:
- A[] = {2, 5, 8, 9}
- S contains 1s at index 1 and 3, Then X = A[1] + A[3] = 2+8 = 10
- S contains 0s at index 2 and 4, Then X = A[2] + A[4] = 9+5 =14
- |X – Y| = |10 – 14| = 4.
- Max element of A[] = 9
- It can be checked that (|X-Y| = 4) <= 9. Thus, A[] satisfies that given condition with given S.
- A[] = {2, 2, 2, 2}
- S contains 1s at index 1 and 3, Then X = A[1] + A[3] = 2+2 = 4
- S contains 0s at index 2 and 4, Then X = A[2] + A[4] = 2+2 = 4
- |X – Y| = |4 – 4| = 0.
- Max element of A[] = 9
- It can be checked that (|X-Y| = 0) <= 9. Thus, A[] satisfies that given condition with given S.
For any possible ascending array A[] of length 4. The given conditions will always be true. Therefore, output is YES.
Input: N = 5, S = 00001
Output: NO
Explanation: S doesn’t follow all the given conditions with all possible ascending A[]’s. One such A[] is: {2, 2, 2, 2, 2}. For this A[], X and Y will be 2 and 8 respectively and then |X – Y| = |2 – 8| = 6 and Max(A[]) = 2. Thus, the condition (|X – Y| = 6) <= 2 fails. Therefore, output is NO.
Approach: Implement the idea below to solve the problem
This is an observation-based problem. Reverse S and start counting 1s. Then go with the approach of balanced parenthesis. If multiple 1 or 0 are found and the count of 1s goes less than -1 or greater than 1, Then it is not possible all cases else it is possible.
Steps were taken to solve the problem:
- Create a boolean variable, let say Flag and mark it initially True.
- Declare a variable let say Count to store the frequency of 1s.
- Run a loop from back of the String and follow below steps under the scope of loop:
- If (current character == 1)
- Else
- If (Count < -1 || Count > 1)
- Flag = false
- Break the loop
- If (Flag), then output YES.
Code to implement the approach:
C++
#include <iostream>
#include <string>
void Satisfies_all_A( int N, std::string S);
int main() {
int N = 5;
std::string S = "00001" ;
Satisfies_all_A(N, S);
return 0;
}
void Satisfies_all_A( int N, std::string S) {
bool flag = true ;
int count = 0;
for ( int i = N - 1; i >= 0; i--) {
if (S[i] == '1' )
count--;
else
count++;
if (count < -1 || count > 1) {
flag = false ;
std::cout << "NO" << std::endl;
break ;
}
}
if (flag)
std::cout << "YES" << std::endl;
}
|
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int N = 5 ;
String S = " 00001 ";
Satisfies_all_A(N, S);
}
public static void Satisfies_all_A( int N, String S)
{
boolean flag = true ;
int count = 0 ;
for ( int i = N - 1 ; i >= 0 ; i--) {
if (S.charAt(i) == '1' )
count--;
else
count++;
if (count < - 1 || count > 1 ) {
flag = false ;
System.out.println("NO");
break ;
}
}
if (flag)
System.out.println("YES");
}
}
|
Python3
def satisfies_all_A(N, S):
flag = True
count = 0
for i in range (N - 1 , - 1 , - 1 ):
if S[i] = = '1' :
count - = 1
else :
count + = 1
if count < - 1 or count > 1 :
flag = False
print ( "NO" )
break
if flag:
print ( "YES" )
if __name__ = = "__main__" :
N = 5
S = "00001"
satisfies_all_A(N, S)
|
C#
using System;
public class GFG {
public static void Main( string [] args)
{
int N = 5;
string S = "00001" ;
SatisfiesAllA(N, S);
}
public static void SatisfiesAllA( int N, string S)
{
bool flag = true ;
int count = 0;
for ( int i = N - 1; i >= 0; i--) {
if (S[i] == '1' )
count--;
else
count++;
if (count < -1 || count > 1) {
flag = false ;
Console.WriteLine( "NO" );
break ;
}
}
if (flag)
Console.WriteLine( "YES" );
}
}
|
Javascript
function satisfiesAllA(N, S) {
let flag = true ;
let count = 0;
for (let i = N - 1; i >= 0; i--) {
if (S[i] === '1' ) {
count -= 1;
} else {
count += 1;
}
if (count < -1 || count > 1) {
flag = false ;
console.log( "NO" );
break ;
}
}
if (flag) {
console.log( "YES" );
}
}
const N = 5;
const S = "00001" ;
satisfiesAllA(N, S);
|
Time Complexity: O(N)
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...