Array Transformation with Repeated Steps and Modulo Operation
Last Updated :
23 Dec, 2023
Given an array A of size N. In one move you have to follow these steps for every element Ai, Find the size of the array after T repeated steps.
- If Ai equals 80 at some point, add another element 0 to the end of the array
- Replace Ai by (Ai + 1) modulo 81.
Examples:
Input: N = 4, A[] = {65, 2, 80, 4}, T = 3
Output: 5
Explanation: After first move array will be: {66, 3, 0, 5, 1}, after second move array will be {67, 4, 1, 6, 2}, after third move array will be {68, 5, 2, 7, 3}. Hence final size is 5.
Input: N = 4, A[] = {80, 80, 79, 79}, T = 2
Output: 8
Explanation: After first move array will be: {0,0,80,80,0,0}, after second move array will be {1, 1, 0, 0, 1, 1, 0, 0}. Hence final size is 8.
Approach: This can be solved with the following idea:
As for each element we have to do modulo by 81, So, adding frequency of each element in array. For T steps, we can find out what will be total size of the array.
Below are the steps involved:
- Create a freq array of 82 size.
- Iterate over array, Add frequency of each element in freq array.
- Iterate for T steps, where in each steps:
- Will replace Ai by (Ai + 1), will make the count of each element.
- At last intialize a sum = 0, Iterate over freq array taking mod.
- Return sum.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int solve( int N, vector< int >& A, int T)
{
int mod = 1e9 + 7;
int freq[82] = { 0 };
for ( int i = 0; i < N; i++)
freq[A[i]]++;
for ( int i = 0; i < T; i++) {
int cur = freq[0];
for ( int j = 1; j < 81; j++) {
int temp = freq[j];
freq[j] = cur;
cur = temp;
}
freq[0] = cur;
freq[1] += cur;
freq[1] %= mod;
}
int sum = 0;
for ( int i = 0; i <= 80; i++) {
sum += freq[i];
sum %= mod;
}
return sum;
}
int main()
{
int N = 4;
vector< int > A = { 80, 80, 79, 79 };
int T = 2;
cout << solve(N, A, T);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Vector;
public class Main {
static int solve( int N, Vector<Integer> A, int T) {
int mod = 1000000007 ;
int [] freq = new int [ 82 ];
for ( int i = 0 ; i < N; i++)
freq[A.get(i)]++;
for ( int i = 0 ; i < T; i++) {
int cur = freq[ 0 ];
for ( int j = 1 ; j < 81 ; j++) {
int temp = freq[j];
freq[j] = cur;
cur = temp;
}
freq[ 0 ] = cur;
freq[ 1 ] += cur;
freq[ 1 ] %= mod;
}
int sum = 0 ;
for ( int i = 0 ; i <= 80 ; i++) {
sum += freq[i];
sum %= mod;
}
return sum;
}
public static void main(String[] args) {
int N = 4 ;
Vector<Integer> A = new Vector<>(Arrays.asList( 80 , 80 , 79 , 79 ));
int T = 2 ;
System.out.println(solve(N, A, T));
}
}
|
Python3
def solve(N, A, T):
mod = 10 * * 9 + 7
freq = [ 0 ] * 82
for i in range (N):
freq[A[i]] + = 1
for _ in range (T):
cur = freq[ 0 ]
for j in range ( 1 , 81 ):
temp = freq[j]
freq[j] = cur
cur = temp
freq[ 0 ] = cur
freq[ 1 ] + = cur
freq[ 1 ] % = mod
sum_val = 0
for i in range ( 81 ):
sum_val + = freq[i]
sum_val % = mod
return sum_val
if __name__ = = "__main__" :
N = 4
A = [ 80 , 80 , 79 , 79 ]
T = 2
print (solve(N, A, T))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG
{
static int Solve( int N, List< int > A, int T)
{
int mod = 1000000007;
int [] freq = new int [82];
foreach ( var num in A)
freq[num]++;
for ( int i = 0; i < T; i++)
{
int cur = freq[0];
for ( int j = 1; j < 81; j++)
{
int temp = freq[j];
freq[j] = cur;
cur = temp;
}
freq[0] = cur;
freq[1] += cur;
freq[1] %= mod;
}
int sum = freq.Take(81).Sum() % mod;
return sum;
}
public static void Main( string [] args)
{
int N = 4;
List< int > A = new List< int > { 80, 80, 79, 79 };
int T = 2;
Console.WriteLine(Solve(N, A, T));
}
}
|
Javascript
function solve(N, A, T) {
const mod = 1000000007;
const freq = new Array(82).fill(0);
for (let i = 0; i < N; i++)
freq[A[i]]++;
for (let i = 0; i < T; i++) {
let cur = freq[0];
for (let j = 1; j < 81; j++) {
let temp = freq[j];
freq[j] = cur;
cur = temp;
}
freq[0] = cur;
freq[1] += cur;
freq[1] %= mod;
}
let sum = 0;
for (let i = 0; i <= 80; i++) {
sum += freq[i];
sum %= mod;
}
return sum;
}
function main() {
const N = 4;
const A = [80, 80, 79, 79];
const T = 2;
console.log(solve(N, A, T));
}
main();
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...