Encrypt given Array in single digit using inverted Pascal Triangle
Last Updated :
30 Mar, 2023
Given an array arr[] 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.
- The last two elements are concatenated together.
Examples:
Input: arr[] = {4, 5, 6, 7}
Output: 04
Explanation:
Pascal’s encryption of [4, 5, 6, 7]
Input: arr[] = {1, 2, 3}
Output: 35
Explanation:
Pascal’s encryption of [1, 2, 3]
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
#include <bits/stdc++.h>
using namespace std;
string digitEncrypt(vector< int >& numbers)
{
int N = numbers.size();
string ans;
if (N == 2) {
if (numbers[0] == 0 && numbers[1] == 0)
return "00" ;
else if (numbers[0] == 0)
return "0" + to_string(numbers[1]);
return to_string(numbers[0])
+ to_string(numbers[1]);
}
for ( int i = 0; i < N - 1; i++)
numbers[i] = (numbers[i]
+ numbers[i + 1])
% 10;
numbers.pop_back();
return digitEncrypt(numbers);
}
int main()
{
vector< int > numbers = { 4, 5, 6, 7 };
cout << digitEncrypt(numbers);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static String
digitEncrypt(ArrayList<Integer> numbers)
{
int N = numbers.size();
if (N == 2 ) {
if (numbers.get( 0 ) == 0 && numbers.get( 1 ) == 0 )
return "00" ;
else if (numbers.get( 0 ) == 0 )
return "0"
+ Integer.toString(numbers.get( 1 ));
else
return Integer.toString(numbers.get( 0 ))
+ Integer.toString(numbers.get( 1 ));
}
for ( int i = 0 ; i < N - 1 ; i++)
numbers.set(
i, ((numbers.get(i) + numbers.get(i + 1 ))
% 10 ));
numbers.remove(numbers.size() - 1 );
return digitEncrypt(numbers);
}
public static void main(String[] args)
{
ArrayList<Integer> numbers = new ArrayList<Integer>(
Arrays.asList( 4 , 5 , 6 , 7 ));
System.out.print(digitEncrypt(numbers));
}
}
|
Python3
def digitEncrypt(numbers) :
N = len (numbers)
if N = = 2 :
if numbers[ 0 ] = = 0 and numbers[ 1 ] = = 0 :
return "00"
elif numbers[ 0 ] = = 0 :
return "0" + str ((numbers[ 1 ]))
return str (numbers[ 0 ]) + str (numbers[ 1 ])
for i in range ( 0 ,N - 1 ) :
numbers[i] = (numbers[i] + numbers[i + 1 ]) % 10
numbers.pop()
return digitEncrypt(numbers)
if __name__ = = "__main__" :
numbers = [ 4 , 5 , 6 , 7 ]
print (digitEncrypt(numbers))
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static string digitEncrypt(List< int > numbers)
{
int N = numbers.Count;
if (N == 2) {
if (numbers[0] == 0 && numbers[1] == 0)
return "00" ;
else if (numbers[0] == 0)
return "0" + Convert.ToString(numbers[1]);
else
return Convert.ToString(numbers[0])
+ Convert.ToString(numbers[1]);
}
for ( int i = 0; i < N - 1; i++)
numbers[i] = (numbers[i] + numbers[i + 1]) % 10;
numbers.RemoveAt(numbers.Count - 1);
return digitEncrypt(numbers);
}
public static void Main( string [] args)
{
List< int > numbers
= new List< int >( new int [] { 4, 5, 6, 7 });
Console.Write(digitEncrypt(numbers));
}
}
|
Javascript
<script>
function digitEncrypt(numbers)
{
var N = numbers.length;
var ans;
if (N == 2) {
if (numbers[0] == 0 && numbers[1] == 0)
return "00" ;
else if (numbers[0] == 0)
return "0" + (numbers[1]);
return to_string(numbers[0])
+ to_string(numbers[1]);
}
for ( var i = 0; i < N - 1; i++)
numbers[i] = (numbers[i]
+ numbers[i + 1])
% 10;
numbers.pop();
return digitEncrypt(numbers);
}
var numbers = [ 4, 5, 6, 7 ];
document.write(digitEncrypt(numbers));
</script>
|
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>
using namespace std;
string digitEncrypt(vector< int > numbers){
queue< int >q;
for ( auto x: numbers){
q.push(x);
}
while (q.size() != 2){
int n = q.size()-1;
while (n--){
int a= q.front();
q.pop();
int b = q.front();
int sum = (a%10) + (b%10);
sum = sum%10;
q.push(sum);
}
q.pop();
}
int a= q.front();
q.pop();
int b = q.front();
string ans = "" ;
ans+=to_string(a);
ans+=to_string(b);
return ans;
}
int main()
{
vector< int > numbers = { 4, 5, 6, 7 };
cout << digitEncrypt(numbers);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static String
digitEncrypt(ArrayList<Integer> numbers)
{
Queue<Integer> q = new LinkedList<>();
for (Integer x : numbers) {
q.add(x);
}
while (q.size() != 2 ) {
int n = q.size() - 1 ;
while (n > 0 ) {
int a = q.peek();
q.remove();
int b = q.peek();
int sum = (a % 10 ) + (b % 10 );
sum = sum % 10 ;
q.add(sum);
n--;
}
q.remove();
}
int a = q.peek();
q.remove();
int b = q.peek();
String ans = String.valueOf(a);
ans += String.valueOf(b);
return ans;
}
public static void main(String[] args)
{
ArrayList<Integer> numbers = new ArrayList<Integer>(
Arrays.asList( 4 , 5 , 6 , 7 , 8 , 9 ));
System.out.print(digitEncrypt(numbers));
}
}
|
Python3
def digitEncrypt(numbers):
q = []
for x in numbers:
q.append(x)
while len (q) ! = 2 :
n = len (q) - 1
while n > 0 :
a = q.pop( 0 )
b = q[ 0 ]
sum = (a % 10 ) + (b % 10 )
sum = sum % 10
q.append( sum )
n - = 1
q.pop( 0 )
a = q.pop( 0 )
b = q[ 0 ]
ans = str (a) + str (b)
return ans
if __name__ = = "__main__" :
numbers = [ 4 , 5 , 6 , 7 ]
print (digitEncrypt(numbers))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static string DigitEncrypt(List< int > numbers) {
Queue< int > q = new Queue< int >();
foreach ( int x in numbers) {
q.Enqueue(x);
}
while (q.Count != 2) {
int n = q.Count - 1;
while (n > 0) {
int a = q.Peek();
q.Dequeue();
int b = q.Peek();
int sum = (a % 10) + (b % 10);
sum = sum % 10;
q.Enqueue(sum);
n--;
}
q.Dequeue();
}
int a1 = q.Peek();
q.Dequeue();
int b1 = q.Peek();
string ans = a1.ToString() + b1.ToString();
return ans;
}
public static void Main( string [] args) {
List< int > numbers = new List< int >( new int [] {4, 5, 6, 7, 8, 9});
Console.Write(DigitEncrypt(numbers));
}
}
|
Javascript
function digitEncrypt(numbers) {
const q = [];
for (let i = 0; i < numbers.length; i++) {
q.push(numbers[i]);
}
while (q.length !== 2) {
let n = q.length - 1;
while (n > 0) {
const a = q.shift();
const b = q[0];
const sum = (a % 10) + (b % 10);
const digitSum = sum % 10;
q.push(digitSum);
n--;
}
q.shift();
}
const a = q.shift();
const b = q.shift();
let ans = a.toString();
ans += b.toString();
return ans;
}
const numbers = [4, 5, 6, 7, 8, 9];
console.log(digitEncrypt(numbers));
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...