Open In App

Array Transformation with Repeated Steps and Modulo Operation

Last Updated : 23 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ Implementation
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
// Function to find size of the array
int solve(int N, vector<int>& A, int T)
{
 
    int mod = 1e9 + 7;
 
    // As last 81 is there
    int freq[82] = { 0 };
 
    // Iterate over array
    for (int i = 0; i < N; i++)
        freq[A[i]]++;
 
    // Iterate over T steps
    for (int i = 0; i < T; i++) {
 
        int cur = freq[0];
 
        // Iterate for whole length arr
        for (int j = 1; j < 81; j++) {
 
            // Assign over values
            int temp = freq[j];
            freq[j] = cur;
            cur = temp;
        }
        freq[0] = cur;
        freq[1] += cur;
        freq[1] %= mod;
    }
 
    // Sum of all number present in freq array
    int sum = 0;
 
    for (int i = 0; i <= 80; i++) {
        sum += freq[i];
        sum %= mod;
    }
    return sum;
}
 
// Driver code
int main()
{
 
    int N = 4;
    vector<int> A = { 80, 80, 79, 79 };
 
    int T = 2;
 
    // Function call
    cout << solve(N, A, T);
 
    return 0;
}


Java




import java.util.Arrays;
import java.util.Vector;
 
public class Main {
    // Function to find the size of the array
    static int solve(int N, Vector<Integer> A, int T) {
        int mod = 1000000007;
 
        // As last 81 is there
        int[] freq = new int[82];
 
        // Iterate over the array
        for (int i = 0; i < N; i++)
            freq[A.get(i)]++;
 
        // Iterate over T steps
        for (int i = 0; i < T; i++) {
            int cur = freq[0];
 
            // Iterate for the whole length of the array
            for (int j = 1; j < 81; j++) {
                // Assign over values
                int temp = freq[j];
                freq[j] = cur;
                cur = temp;
            }
            freq[0] = cur;
            freq[1] += cur;
            freq[1] %= mod;
        }
 
        // Sum of all numbers present in freq array
        int sum = 0;
 
        for (int i = 0; i <= 80; i++) {
            sum += freq[i];
            sum %= mod;
        }
        return sum;
    }
 
    // Driver code
    public static void main(String[] args) {
        int N = 4;
        Vector<Integer> A = new Vector<>(Arrays.asList(80, 80, 79, 79));
        int T = 2;
 
        // Function call
        System.out.println(solve(N, A, T));
    }
}


Python3




def solve(N, A, T):
    mod = 10**9 + 7
 
    # As last 81 is there
    freq = [0] * 82
 
    # Iterate over array
    for i in range(N):
        freq[A[i]] += 1
 
    # Iterate over T steps
    for _ in range(T):
        cur = freq[0]
 
        # Iterate for the whole length of the array
        for j in range(1, 81):
            # Assign over values
            temp = freq[j]
            freq[j] = cur
            cur = temp
 
        freq[0] = cur
        freq[1] += cur
        freq[1] %= mod
 
    # Sum of all numbers present in freq array
    sum_val = 0
 
    for i in range(81):
        sum_val += freq[i]
        sum_val %= mod
 
    return sum_val
 
# Driver code
if __name__ == "__main__":
    N = 4
    A = [80, 80, 79, 79]
    T = 2
 
    # Function call
    print(solve(N, A, T))


C#




using System;
using System.Collections.Generic;
using System.Linq;
 
public class GFG
{
    // Function to find the size of the array
    static int Solve(int N, List<int> A, int T)
    {
        int mod = 1000000007;
 
        // As last 81 is there
        int[] freq = new int[82];
 
        // Iterate over the array
        foreach (var num in A)
            freq[num]++;
 
        // Iterate over T steps
        for (int i = 0; i < T; i++)
        {
            int cur = freq[0];
 
            // Iterate for the whole length of the array
            for (int j = 1; j < 81; j++)
            {
                // Assign over values
                int temp = freq[j];
                freq[j] = cur;
                cur = temp;
            }
            freq[0] = cur;
            freq[1] += cur;
            freq[1] %= mod;
        }
 
        // Sum of all numbers present in freq array
        int sum = freq.Take(81).Sum() % mod;
        return sum;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int N = 4;
        List<int> A = new List<int> { 80, 80, 79, 79 };
        int T = 2;
 
        // Function call
        Console.WriteLine(Solve(N, A, T));
    }
}


Javascript




// Function to find the size of the array
function solve(N, A, T) {
    const mod = 1000000007;
 
    // As last 81 is there
    const freq = new Array(82).fill(0);
 
    // Iterate over the array
    for (let i = 0; i < N; i++)
        freq[A[i]]++;
 
    // Iterate over T steps
    for (let i = 0; i < T; i++) {
        let cur = freq[0];
 
        // Iterate for the whole length of the array
        for (let j = 1; j < 81; j++) {
            // Assign over values
            let temp = freq[j];
            freq[j] = cur;
            cur = temp;
        }
        freq[0] = cur;
        freq[1] += cur;
        freq[1] %= mod;
    }
 
    // Sum of all numbers present in freq array
    let sum = 0;
 
    for (let i = 0; i <= 80; i++) {
        sum += freq[i];
        sum %= mod;
    }
    return sum;
}
 
// Driver code
function main() {
    const N = 4;
    const A = [80, 80, 79, 79];
    const T = 2;
 
    // Function call
    console.log(solve(N, A, T));
}
 
// Invoke the main function
main();


Output

8






Time Complexity: O(N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads