Count elements remained same after applying the given operations
Last Updated :
10 Apr, 2023
Given an array X[]. By using the given operation, you need to return the number of elements having the same initial value except 1 and 2 after applying the given operations after infinite time.
- If at any indices 1 or 2 is present, Then after each second the elements, which are adjacent to them are increased by 1 or 2 linearly in both directions.
- The value of an element will remain unchanged If it is an increment from both sides at the same time by 1 and 2.
- If 1 and 2 are adjacent, They can’t increment, Only the elements except 1 and 2 can be incremented.
- The increment process in that direction is stopped If both 1 and 2 try to increment the same element or no element is there for incrementing. For Example: In the below image in operation 2, 6 is being incremented by both 1 and 2. 6 will remain the same, and the linearly incrementing process by 1 on the right side and 2 on the left side stops.
For more clarification, the operations are defined with the help of a graphical representation.
Explanation of operations
Examples:
Input: N = 9, X[] = {1, 3, 2, 4, 5, 3, 7, 8, 1}
Output: 2
Explanation: The operations take place as:
- After 1 Second: X[ 2 ] = 3, which is adjacent to both 1 and 2. So, X[ 2 ] remains the same. X[ 4 ] = 4 is adjacent to 2, Hence incremented by 2. A[ 8 ] = 8, is adjacent to 1, hence incremented by 1. So updated X[] is = { 1, 3, 2, 6, 5, 3, 7, 9, 1}
- After 2 Second: Updated X[] after 1 second is = { 1, 3, 2, 6, 5, 3, 7, 9, 1}. Now X[ 5 ] = 5 and X[ 7 ] = 7 will be incremented by 2 and 1 at X[ 3 ] and X[9]. So the updated X[] becomes: { 1, 3, 2, 6, 7, 3, 8, 9, 1}
- After 3 Second: Updated X[] after 2 seconds is = { 1, 3, 2, 6, 7, 3, 8, 9, 1} Now X[ 6 ] = 3 will be incremented by 2 and 1 at the same time. So it will remain unchanged, then updated X[] becomes: { 1, 3, 2, 6, 7, 3, 8, 9, 1}.
- There are a total of 2 elements X[2] and X[ 6 ], Which remain the same as the initial value except 1 and 2. Therefore, the output is 2.
Input: N = 9, X[] = { 1, 2, 1, 4, 3, 4, 2, 2, 1}
Output: 1
Explanation: It can be verified that after infinite time, there will be only 1 unchanged element, which will be X[ 5 ] = 3. Therefore, the output is 1.
Approach: Implement the idea below to solve the problem
The problem is Greedy logic based and can be solved by using some mathematics. For more clarification see the Concept of approach section.
Concept of approach:
It is clear from the statement that, We have to output the number of elements. Which will remain the same as their initial value after performing the given operations an infinite number of times. Those elements will remain the same if they are being forced by incrementing their values by 1 and 2 from both sides at the same time.
So, the first observation comes that, They will lie in between somewhere elements 1 and 2.
Now it can be two cases:
- The number of elements between 1 and 2 is even: In this case, there will no element, which will remain the same after performing the given operation an infinite number of times.
- For example: X[] = {1, 3, 4, 5, 6, 2}
- After 1 second: 3 is incremented by 1 and 6 in incremented by 2. Then X[] = {1, 4, 4, 5, 8, 2}
- After 2 seconds: 4 is incremented by 1 and 5 in incremented by 2. Then X[] = {1, 4, 5, 7, 8, 2}
- Now, no element is there such that it has same value after performing the operation except 1 and 2. So from here observation comes that no element will remain same, If there are even number of elements are present between 1 and 2.
- The number of elements between 1 and 2 are odd: In this case there will certainly one element, which will remain same after performing the given operation for infinite number of times.
- For example: X[] = {1 3, 4, 3, 5, 6, 2}
- After 1 second: 3 is incremented by 1 and 6 in incremented by 2. Then X[] = {1 4, 4, 3, 5, 8, 2}
- After 2 seconds: 4 is incremented by 1 and 5 in incremented by 2. Then X[] = {1 4, 5, 3, 7, 8, 2}
- Now, 3 which is present in middle between 1 and 2 is being forced by increment its value by 1 and 2 from both sides. So, 3 remain same, because of second condition of the problem statement.
- Thus the observation comes that, if there are odd number of elements present in between 1 and 2, Then there will certainly one element, Which will remain same as its initial value.
Steps were taken to solve the problem:
- Create two ArrayList<Integer> let’s say Indices and Values.
- Create ans variable for holding the answer.
- Run a for loop from i = 0 to i < N and follow the below-mentioned steps under the scope of the loop:
- if ( X[ i ] == 1 || X[ i ] == 2 )
- Indices.add( i )
- Values.add( X[ i ] )
- If the size of any ArrayList is zero, then ans will be equal to N.
- else:
- Run a loop for i =1 to i < values.size() and follow the below-mentioned steps under the scope of the loop:
- if (values.get( i ) != values.get(i – 1)
- Create a variable, X.
- X = indices.get( i ) – indices.get(i – 1)
- X = X – 1
- If (X is odd), Then ans++.
- Output the value of ans.
Below is the code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
void UnchangedElements( int N, int X[])
{
vector< int > indices;
vector< int > values;
for ( int i = 0; i < N; i++) {
if (X[i] == 1 || X[i] == 2) {
values.push_back(X[i]);
indices.push_back(i);
}
}
long long ans = 0;
bool flag = false ;
if (values.size() == 0) {
cout << N << endl;
flag = true ;
}
for ( int i = 1; i < values.size(); i++) {
if (values[i] != values[i - 1]) {
long long x = indices[i] - indices[i - 1];
x -= 1;
if (x & 1) {
ans++;
}
}
}
if (flag != true )
cout << ans << endl;
}
int main()
{
int N = 9;
int X[] = { 1, 3, 2, 4, 5, 3, 7, 8, 1 };
UnchangedElements(N, X);
}
|
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int N = 9 ;
int X[] = { 1 , 3 , 2 , 4 , 5 , 3 , 7 , 8 , 1 };
UnchangedElements(N, X);
}
static void UnchangedElements( int N, int [] X)
{
ArrayList<Integer> indices = new ArrayList<>();
ArrayList<Integer> values = new ArrayList<>();
for ( int i = 0 ; i < N; i++) {
if (X[i] == 1 || X[i] == 2 ) {
values.add(X[i]);
indices.add(i);
}
}
long ans = 0 ;
boolean flag = false ;
if (values.size() == 0 ) {
System.out.println(N);
flag = true ;
}
for ( int i = 1 ; i < values.size(); i++) {
if (values.get(i) != values.get(i - 1 )) {
long x
= indices.get(i) - indices.get(i - 1 );
x -= 1 ;
if ((x & 1 ) == 1 ) {
ans++;
}
}
}
if (flag != true )
System.out.println(ans);
}
}
|
Python3
def unchanged_elements(N, X):
indices = []
values = []
for i in range (N):
if X[i] = = 1 or X[i] = = 2 :
values.append(X[i])
indices.append(i)
ans = 0
flag = False
if len (values) = = 0 :
print (N)
flag = True
for i in range ( 1 , len (values)):
if values[i] ! = values[i - 1 ]:
x = indices[i] - indices[i - 1 ] - 1
if x % 2 = = 1 :
ans + = 1
if flag ! = True :
print (ans)
if __name__ = = "__main__" :
N = 9
X = [ 1 , 3 , 2 , 4 , 5 , 3 , 7 , 8 , 1 ]
unchanged_elements(N, X)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static void UnchangedElements( int N, int [] X)
{
List< int > indices = new List< int >();
List< int > values = new List< int >();
for ( int i = 0; i < N; i++) {
if (X[i] == 1 || X[i] == 2) {
values.Add(X[i]);
indices.Add(i);
}
}
long ans = 0;
bool flag = false ;
if (values.Count == 0) {
Console.WriteLine(N);
flag = true ;
}
for ( int i = 1; i < values.Count; i++) {
if (values[i] != values[i - 1]) {
long x = indices[i] - indices[i - 1];
x -= 1;
if ((x & 1) == 1) {
ans++;
}
}
}
if (flag != true )
Console.WriteLine(ans);
}
static public void Main()
{
int N = 9;
int [] X = { 1, 3, 2, 4, 5, 3, 7, 8, 1 };
UnchangedElements(N, X);
}
}
|
Javascript
function UnchangedElements(N, X) {
let indices = [];
let values = [];
for (let i = 0; i < N; i++) {
if (X[i] == 1 || X[i] == 2) {
values.push(X[i]);
indices.push(i);
}
}
let ans = 0;
let flag = false ;
if (values.length == 0) {
console.log(N);
flag = true ;
}
for (let i = 1; i < values.length; i++) {
if (values[i] != values[i - 1]) {
let x = indices[i] - indices[i - 1];
x -= 1;
if (x & 1) {
ans++;
}
}
}
if (flag != true )
console.log(ans);
}
let N = 9;
let X = [1, 3, 2, 4, 5, 3, 7, 8, 1];
UnchangedElements(N, X);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...