Check if maximum difference between indices of Non-Zero Elements is greater than X
Given an array arr[] and an integer X, the task is to check that maximum difference between indices of the non-zero elements is greater than or equal to X
Examples:
Input: arr[] = {1, 0, 1}, X = 3
Output: No
Explanation:
Maximum Difference between the indices of non-zero elements is 2.
Input: arr[] = {1, 0, 0, 0, 0, 0, 1}, X = 6
Output: Yes
Explanation:
Maximum Difference between the indices of non-zero elements is 6.
Approach: The idea is to maintain the occurrence of last non-zero element in the array and As soon as there is a new element which is non-zero then compare the distance between the last occurrence index to the current index. If the difference between them is greater than or equal to X. Then, update the last occurrence of the non-zero element and continue checking. Otherwise, return False.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void findRuleFollowed( int arr[], int n, int x)
{
int last_occur = -1;
bool flag = true ;
for ( int i = 0; i < n; i++)
{
if (arr[i] != 0 && last_occur != -1)
{
int diff = i - last_occur;
if (diff >= x)
{
continue ;
}
else
{
flag = false ;
break ;
}
}
else if (arr[i] != 0 && last_occur == -1)
{
last_occur = i;
}
}
if (flag)
cout << "YES" ;
else
cout << "NO" ;
}
int main()
{
int arr[] = { 0, 1, 0, 0, 1 };
int n = sizeof (arr) / sizeof (0);
int x = 3;
findRuleFollowed(arr, n, x);
}
|
Java
import java.util.*;
class GFG{
static void findRuleFollowed( int arr[], int n, int x)
{
int last_occur = - 1 ;
boolean flag = true ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] != 0 && last_occur != - 1 )
{
int diff = i - last_occur;
if (diff >= x)
{
continue ;
}
else
{
flag = false ;
break ;
}
}
else if (arr[i] != 0 && last_occur == - 1 )
{
last_occur = i;
}
}
if (flag)
System.out.println( "YES" );
else
System.out.println( "NO" );
}
public static void main(String args[])
{
int arr[] = { 0 , 1 , 0 , 0 , 1 };
int n = arr.length;
int x = 3 ;
findRuleFollowed(arr, n, x);
}
}
|
Python3
def findRuleFollowed(arr, x):
last_occur = - 1
flag = True
for i in range ( len (arr)):
if arr[i] ! = 0 and last_occur ! = - 1 :
diff = i - last_occur
if diff > = x:
continue
else :
flag = False
break
elif arr[i] ! = 0 and last_occur = = - 1 :
last_occur = i
if flag:
print ( "YES" )
else :
print ( "NO" )
if __name__ = = "__main__" :
arr = [ 0 , 1 , 0 , 0 , 1 ]
x = 3
findRuleFollowed(arr, x)
|
C#
using System;
class GFG{
static void findRuleFollowed( int []arr,
int n, int x)
{
int last_occur = -1;
bool flag = true ;
for ( int i = 0; i < n; i++)
{
if (arr[i] != 0 && last_occur != -1)
{
int diff = i - last_occur;
if (diff >= x)
{
continue ;
}
else
{
flag = false ;
break ;
}
}
else if (arr[i] != 0 && last_occur == -1)
{
last_occur = i;
}
}
if (flag)
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
public static void Main(String []args)
{
int []arr = { 0, 1, 0, 0, 1 };
int n = arr.Length;
int x = 3;
findRuleFollowed(arr, n, x);
}
}
|
Javascript
class GFG
{
static findRuleFollowed(arr, n, x)
{
var last_occur = -1;
var flag = true ;
for (let i = 0; i < n; i++)
{
if (arr[i] != 0 && last_occur != -1)
{
var diff = i - last_occur;
if (diff >= x)
{
continue ;
}
else
{
flag = false ;
break ;
}
}
else if (arr[i] != 0 && last_occur == -1)
{
last_occur = i;
}
}
if (flag)
{
console.log( "YES" );
}
else
{
console.log( "NO" );
}
}
static main(args)
{
var arr = [0, 1, 0, 0, 1];
var n = arr.length;
var x = 3;
GFG.findRuleFollowed(arr, n, x);
}
}
GFG.main([]);
|
Time Complexity: O(N)
Last Updated :
13 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...