Find elements in Array whose positions forms Arithmetic Progression
Last Updated :
27 Oct, 2022
Given an array A[] of N integers. Consider an integer num such that num occurs in the array A[] and all the positions of num, sorted in increasing order forms an arithmetic progression. The task is to print all such pairs of num along with the common difference of the arithmetic progressions they form.
Examples :
Input: N = 8, A = {1, 2, 1, 3, 1, 2, 1, 5}
Output: {1, 2}, {2, 4}, {3, 0}, {5, 0}
Explanation: Positions at which 1 occurs are: 0, 2, 4, 6
It can be easily seen all the positions have same difference between consecutive elements (i.e. 2).
Hence, positions of 1 forms arithmetic progression with common difference 2.
Similarly, all positions of 2 forms arithmetic progression with common difference 4.
And, 3 and 5 are present once only.
According to the definition of arithmetic progression (or A.P.), single element sequences are also A.P. with common difference 0.
Input: N = 1, A = {1}
Output: {1, 0}
Approach: The idea to solve the problem is by using Hashing.
Follow the steps to solve the problem:
- Create a map having integer as key and vector of integers as value of key.
- Traverse the array and store all positions of each element present in array into the map.
- Iterate through the map and check if all the positions of the current element in the map forms A.P.
- If so, insert that element along with the common difference into the answer.
- Else, continue to iterate through the map
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printAP( int N, int A[])
{
unordered_map< int , vector< int > > pos;
for ( int i = 0; i < N; i++) {
pos[A[i]].push_back(i);
}
map< int , int > ans;
for ( auto x : pos) {
if (x.second.size() == 1) {
ans[x.first] = 0;
}
else {
bool flag = 1;
int diff = x.second[1] - x.second[0];
int prev = x.second[1];
int curr;
for ( auto it = 2;
it < x.second.size(); it++) {
curr = x.second[it];
if (curr - prev != diff) {
flag = 0;
break ;
}
prev = x.second[it];
}
if (flag == 1) {
ans[x.first] = diff;
}
}
}
for ( auto it : ans) {
cout << it.first << " "
<< it.second << endl;
}
}
int main()
{
int N = 8;
int A[] = { 1, 2, 1, 3, 1, 2, 1, 5 };
printAP(N, A);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void printAP( int N, int [] A)
{
HashMap<Integer, List<Integer> > pos
= new HashMap<Integer, List<Integer> >();
for ( int i = 0 ; i < N; i++) {
pos.put(A[i], new ArrayList<Integer>());
}
for ( int i = 0 ; i < N; i++)
{
pos.get(A[i]).add(i);
}
Map<Integer, Integer> ans
= new HashMap<Integer, Integer>();
for (Integer x : pos.keySet()) {
Integer key = x;
List<Integer> value = pos.get(x);
if (value.size() == 1 ) {
ans.put(key, 0 );
}
else {
int flag = 1 ;
int diff = value.get( 1 ) - value.get( 0 );
int prev = value.get( 1 );
int curr;
for ( int it = 2 ; it < value.size(); it++) {
curr = value.get(it);
if (curr - prev != diff) {
flag = 0 ;
break ;
}
prev = value.get(it);
}
if (flag == 1 ) {
ans.put(key, diff);
}
}
}
for (Integer it : ans.keySet()) {
int key = it;
int value = ans.get(it);
System.out.println(key + " " + value);
}
}
public static void main(String[] args)
{
int N = 8 ;
int [] A = { 1 , 2 , 1 , 3 , 1 , 2 , 1 , 5 };
printAP(N, A);
}
}
|
Python3
def printAP(N, A):
pos = {}
for i in range (N):
if (A[i] not in pos):
pos[A[i]] = []
pos[A[i]].append(i)
ans = {}
for x in pos:
if ( len (pos[x]) = = 1 ):
ans[x] = 0
else :
flag = 1
diff = pos[x][ 1 ] - pos[x][ 0 ]
prev = pos[x][ 1 ];
curr = 0
length = len (pos[x])
for it in range ( 2 , length):
curr = pos[x][it];
if (curr - prev ! = diff):
flag = 0 ;
break ;
prev = curr
if (flag = = 1 ):
ans[x] = diff
for x in ans:
print (x, ans[x])
if __name__ = = '__main__' :
N = 8
A = [ 1 , 2 , 1 , 3 , 1 , 2 , 1 , 5 ]
printAP(N, A)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static void printAP( int N, int [] A)
{
Dictionary< int , List< int > > pos
= new Dictionary< int , List< int > >();
for ( int i = 0; i < N; i++) {
pos[A[i]] = new List< int >();
}
for ( int i = 0; i < N; i++)
{
pos[A[i]].Add(i);
}
Dictionary< int , int > ans
= new Dictionary< int , int >();
foreach ( int x in pos.Keys) {
int key = x;
List< int > value = pos[x];
if (value.Count == 1) {
ans[key] = 0;
}
else {
int flag = 1;
int diff = value[1] - value[0];
int prev = value[1];
int curr;
for ( int it = 2; it < value.Count; it++) {
curr = value[it];
if (curr - prev != diff) {
flag = 0;
break ;
}
prev = value[it];
}
if (flag == 1) {
ans[key] = diff;
}
}
}
foreach ( int it in ans.Keys) {
int key = it;
int value = ans[it];
Console.WriteLine(key + " " + value);
}
}
public static void Main()
{
int N = 8;
int [] A = { 1, 2, 1, 3, 1, 2, 1, 5 };
printAP(N, A);
}
}
|
Javascript
<script>
const printAP = (N, A) => {
let pos = {};
for (let i = 0; i < N; i++) {
if (A[i] in pos) pos[A[i]].push(i);
else pos[A[i]] = [i];
}
let ans = {};
for (let x in pos) {
if (pos[x].length == 1) {
ans[x] = 0;
}
else {
let flag = 1;
let diff = pos[x][1] - pos[x][0];
let prev = pos[x][1];
let curr;
for (let it = 2; it < pos[x].length; it++) {
curr = pos[x][it];
if (curr - prev != diff) {
flag = 0;
break ;
}
prev = pos[x][it];
}
if (flag == 1) {
ans[x] = diff;
}
}
}
for (let it in ans) {
document.write(`${it} ${ans[it]}<br/>`);
}
}
let N = 8;
let A = [1, 2, 1, 3, 1, 2, 1, 5];
printAP(N, A);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...