Find the GCD of an array made up of numeric strings
Last Updated :
25 Aug, 2021
Given an array arr[] consisting of numeric strings, the task is to calculate Greatest Common Divisor of the given array.
Considering strings 'A' and 'B', "B divides A" if and only if A is a concatenation of B more than once. Find the largest string which divides both A and B.
Examples:
Input: arr[] = { "GFGGFG", "GFGGFGGFGGFG" }
Output: "GFGGFG"
Explanation:
"GFGGFG" is the largest string which divides the whole array elements.
Input: arr = { "Geeks", "GFG"}
Output: ""
Approach: Follow the steps below to solve the problem:
- Calculate GCD of the length of all the strings of the given array, say GCD.
- Check if all the strings of the given array can be made by concatenating the substring {arr[0][0], .., arr[0][GCD - 1]} any number of times or not. If found to be true, then print {arr[0][0], .., arr[0][GCD - 1]}.
- Otherwise, print an empty string.
Below is the implementation of the above approach:
C++
// CPP program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Recursive function
// to return gcd of A and B
int GCD(int lena, int lenb)
{
if (lena == 0)
return lenb;
if (lenb == 0)
return lena;
// Base case
if (lena == lenb)
return lena;
// Length of A is greater
if (lena > lenb)
return GCD(lena - lenb, lenb);
return GCD(lena, lenb - lena);
}
// Calculate GCD
string StringGCD(string a, string b)
{
// Store the GCD of the
// length of the strings
int gcd = GCD(a.size(), b.size());
if (a.substr(0, gcd) == b.substr(0, gcd))
{
int x = ((int)b.size()/gcd);
int y = ((int)a.size()/gcd);
string r="",s="";
while (x--) s += a;
while (y--) r += b;
if (s == r)
return a.substr(0, gcd);
}
return "-1";
}
// Driver Code
int main()
{
string a = "geeksgeeks";
string b = "geeks";
// Function call
cout<<(StringGCD(a, b));
}
// This code is contributed by mohit kumar 29
Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
// Recursive function
// to return gcd of A and B
static int GCD(int lena, int lenb)
{
if (lena == 0)
return lenb;
if (lenb == 0)
return lena;
// Base case
if (lena == lenb)
return lena;
// Length of A is greater
if (lena > lenb)
return GCD(lena - lenb, lenb);
return GCD(lena, lenb - lena);
}
// Calculate GCD
static String StringGCD(String a, String b)
{
// Store the GCD of the
// length of the Strings
int gcd = GCD(a.length(), b.length());
if (a.substring(0, gcd).equals(b.substring(0, gcd)))
{
int x = ((int)b.length()/gcd);
int y = ((int)a.length()/gcd);
String r="",s="";
while (x-- >0) s += a;
while (y-- >0) r += b;
if (s.equals(r))
return a.substring(0, gcd);
}
return "-1";
}
// Driver Code
public static void main(String[] args)
{
String a = "geeksgeeks";
String b = "geeks";
// Function call
System.out.print(StringGCD(a, b));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python implementation of the above approach
# Recursive function
# to return gcd of A and B
def GCD(lena, lenb):
if (lena == 0):
return lenb
if (lenb == 0):
return lena
# Base case
if (lena == lenb):
return lena
# Length of A is greater
if (lena > lenb):
return GCD(lena-lenb, lenb)
return GCD(lena, lenb-lena)
# Calculate GCD
def StringGCD(a, b):
# Store the GCD of the
# length of the strings
gcd = GCD(len(a), len(b))
if a[:gcd] == b[:gcd]:
if a*(len(b)//gcd) == b*(len(a)//gcd):
return a[:gcd]
return -1
# Driver Code
a = 'geeksgeeks'
b = 'geeks'
# Function call
print(StringGCD(a, b))
C#
// C# program for the above approach
using System;
class GFG
{
// Recursive function
// to return gcd of A and B
static int GCD(int lena, int lenb)
{
if (lena == 0)
return lenb;
if (lenb == 0)
return lena;
// Base case
if (lena == lenb)
return lena;
// Length of A is greater
if (lena > lenb)
return GCD(lena - lenb, lenb);
return GCD(lena, lenb - lena);
}
// Calculate GCD
static String StringGCD(String a, String b)
{
// Store the GCD of the
// length of the Strings
int gcd = GCD(a.Length, b.Length);
if (a.Substring(0, gcd).Equals(b.Substring(0, gcd)))
{
int x = ((int)b.Length/gcd);
int y = ((int)a.Length/gcd);
String r="", s="";
while (x-- >0) s += a;
while (y-- >0) r += b;
if (s.Equals(r))
return a.Substring(0, gcd);
}
return "-1";
}
// Driver Code
public static void Main(String[] args)
{
String a = "geeksgeeks";
String b = "geeks";
// Function call
Console.Write(StringGCD(a, b));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JAVASCRIPT program for the above approach
// Recursive function
// to return gcd of A and B
function GCD(lena,lenb)
{
if (lena == 0)
return lenb;
if (lenb == 0)
return lena;
// Base case
if (lena == lenb)
return lena;
// Length of A is greater
if (lena > lenb)
return GCD(lena - lenb, lenb);
return GCD(lena, lenb - lena);
}
// Calculate GCD
function StringGCD(a,b)
{
// Store the GCD of the
// length of the Strings
let gcd = GCD(a.length, b.length);
if (a.substring(0, gcd) == (b.substring(0, gcd)))
{
let x = Math.floor(b.length/gcd);
let y = Math.floor(a.length/gcd);
let r="",s="";
while (x-- >0)
s += a;
while (y-- >0)
r += b;
if (s == (r))
return a.substring(0, gcd);
}
return "-1";
}
// Driver Code
let a = "geeksgeeks";
let b = "geeks";
// Function call
document.write(StringGCD(a, b));
// This code is contributed by patel2127
</script>
Time Complexity: O(N * M), where M is the length of strings
Auxiliary Space: O(1)
Similar Reads
Find the GCD of all the non-prime numbers of given Array Given an array arr[] having N integers, the task is to find the GCD of all the numbers in the array which are not prime. If all the numbers are prime return -1. Examples: Input: N = 3, arr[ ] = {2, 3, 5}Output: -1Explanation: All the numbers are prime. Hence answer will be -1. Input: N = 6, arr[ ] =
8 min read
Find the GCD of LCM of all unique pairs in an Array Given an integer array arr[] of size N, the task is to find the GCD of LCM of all unique pair (i, j) of the array, such that i < j.Examples: Input: arr[] = {10, 24, 40, 80} Output: 40 Explanation: LCM of all unique pairs following given conditions are: LCM(10, 24) = 120 LCM(10, 40) = 40 LCM(10, 8
9 min read
GCD of more than two (or array) numbers Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 min read
Minimum operations to make GCD of array a multiple of k Given an array and k, we need to find the minimum operations needed to make GCD of the array equal or multiple of k. Here an operation means either increment or decrements an array element by 1. Examples: Input : a = { 4, 5, 6 }, k = 5 Output : 2 Explanation : We can increase 4 by 1 so that it becom
8 min read
Find pair with maximum GCD in an array We are given an array of positive integers. Find the pair in array with maximum GCD.Examples: Input : arr[] : { 1 2 3 4 5 }Output : 2Explanation : Pair {2, 4} has GCD 2 which is highest. Other pairs have a GCD of 1.Input : arr[] : { 2 3 4 8 8 11 12 }Output : 8Explanation : Pair {8, 8} has GCD 8 whic
15+ min read