Maximize K to make given array Palindrome when each element is replaced by its remainder with K
Last Updated :
12 Jul, 2021
Given an array A[] containing N positive integers, the task is to find the largest possible number K, such that after replacing all elements by the elements modulo K(A[i]=A[i]%K, for all 0<=i<N), the array becomes a palindrome. If K is infinitely large, print -1.
Examples:
Input: A={1, 2, 3, 4}, N=4
Output:
1
Explanation:
For K=1, A becomes {1%1, 2%1, 3%1, 4%1}={0, 0, 0, 0} which is a palindromic array.
Input: A={1, 2, 3, 2, 1}, N=5
Output:
-1
Observation: The following observations help in solving the problem:
- If the array is already a palindrome, K can be infinitely large.
- Two numbers, say A and B can be made equal by taking their modulus with their difference(|A-B|) as well as the factors of their difference.
Approach: The problem can be solved by making K equal to the GCD of the absolute differences of A[i] and A[N-i-1]. Follow the steps below to solve the problem:
- Check whether A is already a palindrome. If it is, return -1.
- Store the absolute difference of the first and last elements of the array in a variable, say K, which will store the largest number required to change A into a palindrome.
- Traverse from 1 to N/2-1, and for each current index i, do the following:
- Update K with the GCD of K and the absolute difference of A[i] and A[N-i-1].
- Return K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int largestK( int A[], int N)
{
int l = 0, r = N - 1, flag = 0;
while (l < r) {
if (A[l] != A[r]) {
flag = 1;
break ;
}
l++;
r--;
}
if (flag == 0)
return -1;
int K = abs (A[0] - A[N - 1]);
for ( int i = 1; i < N / 2; i++)
K = gcd(K, abs (A[i] - A[N - i - 1]));
return K;
}
int main()
{
int A[] = { 1, 2, 3, 2, 1 };
int N = sizeof (A) / sizeof (A[0]);
cout << largestK(A, N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
else
return gcd(b, a % b);
}
static int largestK( int A[], int N)
{
int l = 0 , r = N - 1 , flag = 0 ;
while (l < r)
{
if (A[l] != A[r])
{
flag = 1 ;
break ;
}
l++;
r--;
}
if (flag == 0 )
return - 1 ;
int K = Math.abs(A[ 0 ] - A[N - 1 ]);
for ( int i = 1 ; i < N / 2 ; i++)
K = gcd(K, Math.abs(A[i] - A[N - i - 1 ]));
return K;
}
public static void main(String[] args)
{
int A[] = { 1 , 2 , 3 , 2 , 1 };
int N = A.length;
System.out.println(largestK(A, N));
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
else :
return gcd(b, a % b)
def largestK(A, N):
l,r,flag = 0 , N - 1 , 0
while (l < r):
if (A[l] ! = A[r]):
flag = 1
break
l + = 1
r - = 1
if (flag = = 0 ):
return - 1
K = abs (A[ 0 ] - A[N - 1 ])
for i in range ( 1 ,N / / 2 ):
K = gcd(K, abs (A[i] - A[N - i - 1 ]))
return K
if __name__ = = '__main__' :
A = [ 1 , 2 , 3 , 2 , 1 ]
N = len (A)
print (largestK(A, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
static int largestK( int []A, int N)
{
int l = 0, r = N - 1, flag = 0;
while (l < r) {
if (A[l] != A[r]) {
flag = 1;
break ;
}
l++;
r--;
}
if (flag == 0)
return -1;
int K = Math.Abs(A[0] - A[N - 1]);
for ( int i = 1; i < N / 2; i++)
K = gcd(K, Math.Abs(A[i] - A[N - i - 1]));
return K;
}
public static void Main()
{
int []A = { 1, 2, 3, 2, 1 };
int N = A.Length;
Console.Write(largestK(A, N));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
function largestK(A, N)
{
let l = 0, r = N - 1, flag = 0;
while (l < r)
{
if (A[l] != A[r])
{
flag = 1;
break ;
}
l++;
r--;
}
if (flag == 0)
return -1;
let K = Math.abs(A[0] - A[N - 1]);
for (let i = 1; i < N / 2; i++)
K = gcd(K, Math.abs(A[i] - A[N - i - 1]));
return K;
}
let A = [ 1, 2, 3, 2, 1 ];
let N = A.length;
document.write(largestK(A, N) + "<br>" );
</script>
|
Time Complexity: O(NLogM), where M is the largest element in the array
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...