Searching in a linked list

Given a singly linked list, write a function which will search for a value in the list. Signature of the function will be as shown below: bool search(Node*head, int x); head points to the first Node in the list. The function should return true is x is present in the list as false.

Majority Element of an Array (Moore's Algorithm)

An integer x in array arr of n elements is the majority element if and only if x appears more than n/2 times in A. – If n is odd then it should appear at least (n+1)/2 times – If n is even then it should appear at least (n/2) + 1 times Array: 1 […]

Print array in the forward and backward direction recursively

Write a recursive function which will print an array in forward and backward order (depending on a parameter). The signature of the function should be /* If the array is 1 2 3 4 5, then Output should be * 1 2 3 4 5 – if (forward == true) * 5 4 3 2 […]

Print all repeating characters in a string

We have already seen the problem to print first repeating character in a string. In this post we will be looking at printing all the repeating characters in the string. For example: Input String: “www.ritambhara.in” Output: w.ria Input String: “social.ritambhara.in” Output: ia.r Input String: “meenakshi” Output: e Note that we are printing the repeating character […]

Print the first repeating character in String

Give a string in which characters may be repeating. Print the first character (from the start) which is repeating in the string. For example: String: ABCBCD Output: B (A is not repeating, hence B is the first character which is repeating) String: ABCDE Output: NULL (No character repeating)

Blue Eyed Islanders puzzle

1000 tribal people lives on an island. Out of them 100 have blue eyes and 900 has black colored eyes. Their religion forbids them to know their own eye color, or even to discuss the topic (there is no reflective surface where they can see their eyes). But all of them knows the eye color […]

Recursive function to reverse a String

We have already seen the problem to reverse the words in a string, in the solution to that problem we have also written function to reverse the string. That was a non-recursive (iterative) function. Write a recursive function to reverse the String. For example: If the String is “ABCD” then the output should be “DCBA“. Before […]

Check if one string is rotation of another

Given two strings, write code to check if one string is rotation of another. For example: str2 below is a rotation of str1. str1 = “ROTATE” str2 = “TATERO” You may use the strstr function, but ONLY ONCE.