Check if frequency of characters are in Recaman Series

Given a string of lowercase alphabets. The task is to check whether the frequency of alphabets in this string, after arranging in any possible manner, forms the Recaman’s Sequence(excluding the first term).

Print “YES” if they are in sequence, otherwise output “NO”.

Few starting terms of Recaman’s Sequence are:

0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9, 24, 8 ….

Note: First term of Recaman’s Sequence is not considered since it is zero.

Examples:

Input  : str = "dddeweecceee"
Output : YES
Frequency of 'd' => 3
Frequency of 'e' => 6
Frequency of 'w' => 1
Frequency of 'c' => 2
These frequencies form the first 4 terms of 
Recaman's sequence => {1, 3, 6, 2}

Input : str = "geeksforgeeks"
Output : NO

Approach:

  • Traverse the string and store the frequency of the characters in a map. Let the size of the map be N after storing the frequency.
  • Now, make an array and insert first N elements of Recaman’s sequence in it.
  • Now, traverse the array and check if the elements of the array are present as a key in map (excluding the check for zero).
  • If each and every element of array is present in map, output “YES”, otherwise “NO”.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check whether frequency of
// characters in a string makes
// Recaman Sequence
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to fill the array with first N numbers
// from Recaman's Sequence
int recaman(int arr[], int n)
{
    // First term of the sequence is always 0
    arr[0] = 0;
  
    // Fill remaining terms using recursive
    // formula
    for (int i = 1; i <= n; i++) {
        int temp = arr[i - 1] - i;
        int j;
  
        for (j = 0; j < i; j++) {
  
            // If arr[i-1] - i is negative or
            // already exists.
            if ((arr[j] == temp) || temp < 0) {
                temp = arr[i - 1] + i;
                break;
            }
        }
  
        arr[i] = temp;
    }
}
  
// Function to check if the frequencies
// are in Recaman series
string isRecaman(string s)
{
    // Store frequencies of characters
    unordered_map<char, int> m;
    for (int i = 0; i < s.length(); i++) 
        m[s[i]]++;    
  
    // Get the size of the map
    int n = m.size();
  
    int arr[n + 1];
    recaman(arr, n);
  
    int flag = 1;
  
    // Compare vector elements with values in Map
    for (auto itr = m.begin(); itr != m.end(); itr++) {
  
        int found = 0;
  
        for (int j = 1; j <= n; j++) {
            if (itr->second == arr[j]) {
                found = 1;
                break;
            }
        }
  
        if (found == 0) {
            flag = 0;
            break;
        }
    }
  
    if (flag == 1)
        return "YES";
    else
        return "NO";
}
  
// Driver code
int main()
{
    string s = "geeekkkkkkss";
    cout << isRecaman(s);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check whether frequency of 
// characters in a string makes Recaman Sequence 
import java.util.HashMap;
import java.util.Map;
  
class GfG
{
  
    // Function to fill the array with first 
    // N numbers from Recaman's Sequence 
    static void recaman(int arr[], int n) 
    
        // First term of the sequence is always 0 
        arr[0] = 0
      
        // Fill remaining terms using 
        // recursive formula 
        for (int i = 1; i <= n; i++)
        
            int temp = arr[i - 1] - i; 
      
            for (int j = 0; j < i; j++)
            
      
                // If arr[i-1] - i is negative or 
                // already exists. 
                if ((arr[j] == temp) || temp < 0)
                
                    temp = arr[i - 1] + i; 
                    break
                
            
      
            arr[i] = temp; 
        
    
      
    // Function to check if the frequencies 
    // are in Recaman series 
    static String isRecaman(String s) 
    
        // Store frequencies of characters 
        HashMap <Character, Integer> m = new HashMap<>(); 
        for (int i = 0; i < s.length(); i++) 
              
            if (m.containsKey(s.charAt(i)))
                m.put(s.charAt(i), m.get(s.charAt(i))+1);
            else
                m.put(s.charAt(i), 1);
      
        // Get the size of the map 
        int n = m.size(); 
      
        int arr[] = new int[n + 1]; 
        recaman(arr, n); 
      
        int flag = 1
      
        // Compare vector elements with values in Map 
        for (Map.Entry mapEle : m.entrySet()) 
        
      
            int found = 0
      
            for (int j = 1; j <= n; j++)
            
                if ((int)mapEle.getValue() == arr[j]) 
                
                    found = 1
                    break
                
            
      
            if (found == 0
            
                flag = 0
                break
            
        
      
        if (flag == 1
            return "YES"
        else
            return "NO"
    
  
    // Driver code
    public static void main(String []args)
    {
        String s = "geeekkkkkkss"
        System.out.println(isRecaman(s));
    }
}
  
// This code is contributed by Rituraj Jain

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to check whether 
# frequency of characters in a string 
# makes Recaman Sequence 
  
# Function to fill the array with first 
# N numbers from Recaman's Sequence 
def recaman(arr, n) :
  
    # First term of the sequence 
    # is always 0 
    arr[0] = 0
  
    # Fill remaining terms using 
    # recursive formula 
    for i in range(1, n + 1) :
        temp = arr[i - 1] - i; 
  
        for j in range(i) :
  
            # If arr[i-1] - i is negative 
            # or already exists. 
            if ((arr[j] == temp) or temp < 0) :
                temp = arr[i - 1] + i; 
                break;
                  
        arr[i] = temp; 
  
# Function to check if the frequencies 
# are in Recaman series 
def isRecaman(s) :
      
    # Store frequencies of characters 
    m = dict.fromkeys(list(s), 0); 
      
    for i in range(len(s)) :
        m[s[i]] += 1
  
    # Get the size of the map 
    n = len(m); 
  
    arr = [0] * (n + 1); 
    recaman(arr, n); 
  
    flag = 1
  
    # Compare vector elements with 
    # values in Map 
    for keys in m.keys() :
  
        found = 0
  
        for j in range(1, n + 1) :
            if (m[keys] == arr[j]) :
                found = 1
                break
          
        if (found == 0) :
            flag = 0
            break
  
    if (flag == 1) :
        return "YES"
    else :
        return "NO"
  
# Driver code 
if __name__ == "__main__"
  
    s = "geeekkkkkkss"
      
    print(isRecaman(s)); 
  
# This code is contributed by Ryuga

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check whether frequency of 
// characters in a string makes Recaman Sequence
using System;
using System.Collections.Generic;
  
class GFG
{
    // Function to fill the array with first 
    // N numbers from Recaman's Sequence
    public static void recaman(int[] arr, int n)
    {
        // First term of the sequence is always 0
        arr[0] = 0;
          
        // Fill remaining terms using 
        // recursive formula
        for (int i = 1; i <= n; i++)
        {
            int temp = arr[i - 1] - i;
            for (int j = 0; j < i; j++)
            {
                // If arr[i-1] - i is negative or 
                // already exists.
                if ((arr[j] == temp) || temp < 0)
                {
                    temp = arr[i - 1] + i;
                    break;
                }
            }
  
            arr[i] = temp;
        }
    }
  
    // Function to check if the frequencies 
    // are in Recaman series
    public static String isRecaman(String s)
    {
        // Store frequencies of characters
        Dictionary<char,
                   int> m = new Dictionary<char
                                           int>();
        for (int i = 0; i < s.Length; i++)
        {
            if (m.ContainsKey(s[i]))
                m[s[i]]++;
            else
                m.Add(s[i], 1);
        }
  
        // Get the size of the map
        int n = m.Count;
        int[] arr = new int[n + 1];
        recaman(arr, n);
        int flag = 1;
          
        // Compare vector elements with values in Map
        foreach (KeyValuePair<char
                              int> mapEle in m)
        {
            int found = 0;
            for (int j = 1; j <= n; j++)
            {
                if (mapEle.Value == arr[j])
                {
                    found = 1;
                    break;
                }
            }
  
            if (found == 0)
            {
                flag = 0;
                break;
            }
        }
  
        if (flag == 1)
            return "YES";
        else
            return "NO";
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        String s = "geeekkkkkkss";
        Console.WriteLine(isRecaman(s));
    }
}
  
// This code is contributed by
// sanjeev2552

chevron_right


Output:

YES


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.