- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if characters of each word can be rearranged to form an Arithmetic Progression (AP)
In this article, we will discuss how to check if the characters of each word in a given string can be rearranged to form an Arithmetic Progression (AP). We will also implement the solution in C++ and provide an example to illustrate the working of the code.
Arithmetic Progression (AP)
An Arithmetic Progression (AP) is a sequence of numbers in which each term after the first is obtained by adding a constant d to the preceding term. The constant d is called the common difference.
For example, the sequence 1, 3, 5, 7, 9 is an Arithmetic Progression with common difference 2.
Approach
To check if the characters of each word in a given string can be rearranged to form an Arithmetic Progression, we will follow the below approach −
We will split the given string into individual words.
For each word, we will sort the characters in alphabetical order.
We will calculate the common difference between each pair of adjacent characters in the sorted word.
If the common difference is the same for all pairs of adjacent characters, then the characters of the word can be rearranged to form an Arithmetic Progression.
We will repeat steps 2-4 for all words in the given string.
If all words can be rearranged to form an Arithmetic Progression, then we return true. Otherwise, we return false.
Example
Following are the programs to the above approach −
#include <stdio.h> #include <stdbool.h> #include <string.h> bool canFormAP(char *s) { int len = strlen(s); char words[100][100]; // Assuming a maximum of 100 words with 100 characters each int wordCount = 0; int wordLen = 0; for (int i = 0; i <= len; i++) { if (s[i] == ' ' || s[i] == '\0') { words[wordCount][wordLen] = '\0'; // Null-terminate the word wordCount++; wordLen = 0; } else { words[wordCount][wordLen] = s[i]; wordLen++; } } for (int i = 0; i < wordCount; i++) { int n = strlen(words[i]); if (n <= 2) { continue; } char d = words[i][1] - words[i][0]; for (int j = 2; j < n; j++) { if (words[i][j] - words[i][j - 1] != d) { return false; } } } return true; } int main() { char s[] = "abcdefgh"; if (canFormAP(s)) { printf("Characters of each word can be rearranged to form an Arithmetic Progression\n"); } else { printf("Characters of each word cannot be rearranged to form an Arithmetic Progression\n"); } return 0; }
Output
Characters of each word can be rearranged to form an Arithmetic Progression
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; // Function to check if characters of each word can be rearranged to form an Arithmetic Progression bool canFormAP(string s) { vector<string> words; string word = ""; // Split the input string into words using spaces for (char c : s) { if (c == ' ') { words.push_back(word); // Store the word in the vector word = ""; // Reset the word } else { word += c; // Build the word character by character } } words.push_back(word); // Store the last word // Check if characters within each word can form an Arithmetic Progression for (string w : words) { sort(w.begin(), w.end()); int n = w.length(); if (n <= 2) { continue; } int d = w[1] - w[0]; // Calculate the common difference for (int i = 2; i < n; i++) { if (w[i] - w[i-1] != d) { return false; // Not an Arithmetic Progression } } } return true; // All words satisfy the condition } int main() { string s = "abcdefgh"; // Check if characters of each word can be rearranged to form an Arithmetic Progression if (canFormAP(s)) { cout << "Characters of each word can be rearranged to form an Arithmetic Progression\n"; } else { cout << "Characters of each word cannot be rearranged to form an Arithmetic Progression\n"; } return 0; }
Output
Characters of each word can be rearranged to form an Arithmetic Progression
import java.util.Arrays; // Import the Arrays class public class Main { public static boolean canFormAP(String s) { String[] words = s.split(" "); // Split the input string into words for (String w : words) { char[] arr = w.toCharArray(); Arrays.sort(arr); // Sort characters of the word int n = arr.length; if (n <= 2) { continue; } int d = arr[1] - arr[0]; for (int i = 2; i < n; i++) { if (arr[i] - arr[i - 1] != d) { return false; } } } return true; } public static void main(String[] args) { String s = "abcdefgh"; if (canFormAP(s)) { System.out.println("Characters of each word can be rearranged to form an Arithmetic Progression"); } else { System.out.println("Characters of each word cannot be rearranged to form an Arithmetic Progression"); } } }
Output
Characters of each word can be rearranged to form an Arithmetic Progression
def can_form_ap(s): words = s.split() # Split the input string into words for w in words: w = ''.join(sorted(w)) # Sort characters of the word n = len(w) if n <= 2: continue d = ord(w[1]) - ord(w[0]) for i in range(2, n): if ord(w[i]) - ord(w[i - 1]) != d: return False return True s = "abcdefgh" if can_form_ap(s): print("Characters of each word can be rearranged to form an Arithmetic Progression") else: print("Characters of each word cannot be rearranged to form an Arithmetic Progression")
Output
Characters of each word can be rearranged to form an Arithmetic Progression
In this example, the given string is "the quick brown fox jumps over the lazy dog". Each word in the string can be rearranged to form an Arithmetic Progression. For example, the word "quick" can be rearranged to form the sequence "cikqu" which is an Arithmetic Progression with common difference 2. As we were discussing, the word "lazy" can be rearranged to form the sequence "alzy" which is an Arithmetic Progression with common difference 11.
Therefore, in this example, the characters of each word in the given string can be rearranged to form an Arithmetic Progression, and the output of the code is "Characters of each word can be rearranged to form an Arithmetic Progression".
Conclusion
In this article, we discussed how to check if the characters of each word in a given string can be rearranged to form an Arithmetic Progression (AP). We followed a simple approach, which involved sorting the characters of each word and checking if the common difference between each pair of adjacent characters is the same. We also provided an implementation of the solution in C++ and explained it with an example test case.
This problem can have various real-life applications. For example, in cryptography, rearranging the characters of a string can be used to encrypt the original message, and checking if the characters can be rearranged to form an AP can be used as a verification step in the decryption process.
To Continue Learning Please Login
Login with Google