Cognizant GenC Elevate Sample Coding Question 9

Question 9

In this article, we will discuss about Coding Question with their solution in C++ and java. In this question , we have to find the number of words that remain same after rotating the characters of the word left to right N times.

Question 9

Question 9

Vira writes an apology letter to anu. However, before Anu can read it, Vira’s enemy Rohan takes it and rotates the characters of each word left to right N times. Find the number of words that remain the same even after this shifting of letters.

Input Specification:

  • input1: String of words
  • input2: N, number of times rotation happens

Output Specification:

  • Your function should return the number of correct words.

Example 1:

input1: llohe ereth
input2: 2

Output : 2

Example 2:

input1: Sorry
input2: 2

Output : 0

using namespace std;

// In-place rotates s towards left by d
void leftrotate(string &s, int d)
reverse(s.begin(), s.begin()+d);
reverse(s.begin()+d, s.end());
reverse(s.begin(), s.end());

// Driver code
int main()
string str1;
Int n;
leftrotate(str1, n);
cout << str1 << endl;

return 0;
import java.util.*;

public class Apology{

static String leftrotate(String str, int d)
String ans = str.substring(d) + str.substring(0, d);
return ans;

public static void main(String args[])
Scanner sc = new Scanner(;
String str1 = sc.nextLine();
int n = sc.nextInt();
String rotate = leftrotate(str1, n);
char ch1[] = str1.toCharArray();
char ch2[] = rotate.toCharArray();

int count = 0;
for(int i=0 ; i<str1.length() ; i++){
if(ch1[i] == ch2[i])