Encrypt given Array in single digit using inverted Pascal Triangle
Last Updated : 23 Jul, 2025
Given an arrayarr[] of length N (N > 1)containing positive integers, the task is to encrypt the numbers of the array into a single digit using the inverted Pascal triangle as follows.
From the starting of the array find the sum of two adjacent elements.
Replace the sum with only the digit at the unit position of the sum.
Replace all the array elements with the values formed in this way and continue until there are only two elements left.
Input: arr[] = {14, 5} Output: 145 Explanation: As there were two elements they are appended together
Approach: This problem can be solved using recursion based on the following idea:
Calculate the sum of all ith with (i-1)th element and mod by 10 to get least significant digit for next operation until the whole container becomes of length 2.
Follow the steps to solve the problem:
Use a recursive function and do the following:
Traverse numbers to calculate sum of adjacent elements and mod with 10 to get single least significant digit as numbers[i]=(numbers[i]+numbers[i+1])%10
Delete the last element from the array, as one element will be reduced after each operation.
Continue this procedure until only 2 elements are left.
Below is the implementation of the above approach:
C++14
// C++ code for the above approach:#include<bits/stdc++.h>usingnamespacestd;// Recursive function to find the encryptionstringdigitEncrypt(vector<int>&numbers){intN=numbers.size();stringans;// If the value of N is 2if(N==2){if(numbers[0]==0&&numbers[1]==0)return"00";elseif(numbers[0]==0)return"0"+to_string(numbers[1]);returnto_string(numbers[0])+to_string(numbers[1]);}for(inti=0;i<N-1;i++)numbers[i]=(numbers[i]+numbers[i+1])%10;numbers.pop_back();returndigitEncrypt(numbers);}// Drivers codeintmain(){vector<int>numbers={4,5,6,7};// Function callcout<<digitEncrypt(numbers);return0;}
Java
// Java code for the above approach:importjava.io.*;importjava.util.*;classGFG{// Recursive function to find the encryptionpublicstaticStringdigitEncrypt(ArrayList<Integer>numbers){intN=numbers.size();// If the value of N is 2if(N==2){if(numbers.get(0)==0&&numbers.get(1)==0)return"00";elseif(numbers.get(0)==0)return"0"+Integer.toString(numbers.get(1));elsereturnInteger.toString(numbers.get(0))+Integer.toString(numbers.get(1));}for(inti=0;i<N-1;i++)numbers.set(i,((numbers.get(i)+numbers.get(i+1))%10));numbers.remove(numbers.size()-1);returndigitEncrypt(numbers);}// Driver Codepublicstaticvoidmain(String[]args){ArrayList<Integer>numbers=newArrayList<Integer>(Arrays.asList(4,5,6,7));// Function callSystem.out.print(digitEncrypt(numbers));}}// This code is contributed by Rohit Pradhan
Python3
# python3 code to implement the approach# Recursive function to find the encryptiondefdigitEncrypt(numbers):N=len(numbers)# If the value of N is 2ifN==2:ifnumbers[0]==0andnumbers[1]==0:return"00"elifnumbers[0]==0:return"0"+str((numbers[1]))returnstr(numbers[0])+str(numbers[1])foriinrange(0,N-1):numbers[i]=(numbers[i]+numbers[i+1])%10numbers.pop()returndigitEncrypt(numbers)# Driver codeif__name__=="__main__":numbers=[4,5,6,7]# Function callprint(digitEncrypt(numbers))# This code is contributed by jana_sayantan.
C#
// C# code for the above approach:usingSystem;usingSystem.Collections.Generic;classGFG{// Recursive function to find the encryptionpublicstaticstringdigitEncrypt(List<int>numbers){intN=numbers.Count;// If the value of N is 2if(N==2){if(numbers[0]==0&&numbers[1]==0)return"00";elseif(numbers[0]==0)return"0"+Convert.ToString(numbers[1]);elsereturnConvert.ToString(numbers[0])+Convert.ToString(numbers[1]);}for(inti=0;i<N-1;i++)numbers[i]=(numbers[i]+numbers[i+1])%10;numbers.RemoveAt(numbers.Count-1);returndigitEncrypt(numbers);}// Driver CodepublicstaticvoidMain(string[]args){List<int>numbers=newList<int>(newint[]{4,5,6,7});// Function callConsole.Write(digitEncrypt(numbers));}}// This code is contributed by phasing17
JavaScript
<script>// JavaScript code for the above approach:// Recursive function to find the encryptionfunctiondigitEncrypt(numbers){varN=numbers.length;varans;// If the value of N is 2if(N==2){if(numbers[0]==0&&numbers[1]==0)return"00";elseif(numbers[0]==0)return"0"+(numbers[1]);returnto_string(numbers[0])+to_string(numbers[1]);}for(vari=0;i<N-1;i++)numbers[i]=(numbers[i]+numbers[i+1])%10;numbers.pop();returndigitEncrypt(numbers);}// Drivers codevarnumbers=[4,5,6,7];// Function calldocument.write(digitEncrypt(numbers));// This code is contributed by phasing17.</script>
Output
04
Time Complexity: O(N2) Auxiliary Space: O(N)
Approach 2: Using Queue ( just read the code you will get the approach by comments)
C++
#include<bits/stdc++.h>usingnamespacestd;// iterative function to find the encryptionstringdigitEncrypt(vector<int>numbers){queue<int>q;//push all elements of vector number to queue and traverse queue until we get the final result for(autox:numbers){q.push(x);}// when queue has only 2 elements left then stop the cycle while(q.size()!=2){intn=q.size()-1;while(n--){inta=q.front();// first elementq.pop();intb=q.front();// 2nd elementintsum=(a%10)+(b%10);// if 'a' or 'b' greater than 10 then just '%10' to get make is a single digit numbersum=sum%10;// same with sum to make it a single digit numberq.push(sum);// push that sum in queue for the another traversal}q.pop();}// now made from both the digit by converting them into string// we convert them into string because when a=0 we cannot do ans = a*10 + b// thats why we convert them into string and then add to them into our answer stringinta=q.front();q.pop();intb=q.front();stringans="";ans+=to_string(a);ans+=to_string(b);returnans;}intmain(){vector<int>numbers={4,5,6,7};cout<<digitEncrypt(numbers);return0;}
Java
// Java code for the above approach:importjava.io.*;importjava.util.*;classGFG{// Iterative function to find the encryptionpublicstaticStringdigitEncrypt(ArrayList<Integer>numbers){Queue<Integer>q=newLinkedList<>();// push all elements of vector number to queue and// traverse queue until we get the final resultfor(Integerx:numbers){q.add(x);}// when queue has only 2 elements left then stop the// cyclewhile(q.size()!=2){intn=q.size()-1;while(n>0){// first elementinta=q.peek();q.remove();// 2nd elementintb=q.peek();// if 'a' or 'b' greater than 10 then just// '%10' to get make is a single digit// numberintsum=(a%10)+(b%10);// same with sum to make it a single digit// numbersum=sum%10;// push that sum in queue for the another// traversalq.add(sum);n--;}q.remove();}// now made from both the digit by converting them// into string we convert them into string because// when a=0 we cannot do ans = a*10 + b thats why we// convert them into string and then add to them// into our answer stringinta=q.peek();q.remove();intb=q.peek();Stringans=String.valueOf(a);ans+=String.valueOf(b);returnans;}// Driver Codepublicstaticvoidmain(String[]args){ArrayList<Integer>numbers=newArrayList<Integer>(Arrays.asList(4,5,6,7,8,9));// Function callSystem.out.print(digitEncrypt(numbers));}}// This code is contributed by shubhamrajput6156
Python3
# Python3 code for the above approach:# function to find the encryptiondefdigitEncrypt(numbers):q=[]# push all elements of numbers list to queueforxinnumbers:q.append(x)# traverse queue until we get the final resultwhilelen(q)!=2:n=len(q)-1whilen>0:a=q.pop(0)# first elementb=q[0]# 2nd elementsum=(a%10)+(b%10)# if 'a' or 'b' greater than 10 then just '%10' to get make is a single digit numbersum=sum%10# same with sum to make it a single digit numberq.append(sum)# push that sum in queue for the another traversaln-=1q.pop(0)# now made from both the digit by # converting them into string# we convert them into string because # when a=0 we cannot do ans = a*10 + b# thats why we convert them into string # and then add to them into our answer stringa=q.pop(0)b=q[0]ans=str(a)+str(b)returnans# Driver Codeif__name__=="__main__":# Input arraynumbers=[4,5,6,7]# Function Callprint(digitEncrypt(numbers))
C#
usingSystem;usingSystem.Collections.Generic;publicclassGFG{// Iterative function to find the encryptionpublicstaticstringDigitEncrypt(List<int>numbers){Queue<int>q=newQueue<int>();// Push all elements of the list to the queue and// traverse queue until we get the final resultforeach(intxinnumbers){q.Enqueue(x);}// When queue has only 2 elements left then stop the cyclewhile(q.Count!=2){intn=q.Count-1;while(n>0){// First elementinta=q.Peek();q.Dequeue();// Second elementintb=q.Peek();// If 'a' or 'b' greater than 10 then just// '%10' to get make it a single digit numberintsum=(a%10)+(b%10);// Same with sum to make it a single digit// numbersum=sum%10;// Push that sum in queue for the another// traversalq.Enqueue(sum);n--;}q.Dequeue();}// Now made from both the digit by converting them// into string we convert them into string because// when a=0 we cannot do ans = a*10 + b thats why we// convert them into string and then add to them// into our answer stringinta1=q.Peek();q.Dequeue();intb1=q.Peek();stringans=a1.ToString()+b1.ToString();returnans;}// Driver CodepublicstaticvoidMain(string[]args){List<int>numbers=newList<int>(newint[]{4,5,6,7,8,9});// Function callConsole.Write(DigitEncrypt(numbers));}}
JavaScript
// JavaScript code for the above approach:functiondigitEncrypt(numbers){constq=[];// push all elements of array 'numbers' to queue and// traverse queue until we get the final resultfor(leti=0;i<numbers.length;i++){q.push(numbers[i]);}// when queue has only 2 elements left then stop the// cyclewhile(q.length!==2){letn=q.length-1;while(n>0){// first elementconsta=q.shift();// 2nd elementconstb=q[0];// if 'a' or 'b' greater than 10 then just// '%10' to get make it a single digit numberconstsum=(a%10)+(b%10);// same with sum to make it a single digit numberconstdigitSum=sum%10;// push that digitSum in queue for the another// traversalq.push(digitSum);n--;}q.shift();}// now made from both the digit by converting them// into string we convert them into string because// when a=0 we cannot do ans = a*10 + b thats why we// convert them into string and then add to them// into our answer stringconsta=q.shift();constb=q.shift();letans=a.toString();ans+=b.toString();returnans;}// Driver Codeconstnumbers=[4,5,6,7,8,9];// Function callconsole.log(digitEncrypt(numbers));