# Cracking the coding interview | Step-12

### DAY 12: coding interview questions

Question 1:

Generate series and find Nth element:
Given three numbers, a, b and c, such that
either
a < b < c
or
a > b > c
and
a, b, and c can be positive, negative or zero.
Form the series such that the gap between c and its next element (say d) should be the same as the gap between a and b. Similarly, the gap between c’s next element (d) and d’s next element (say e) should be the same as the gap between b and c.
Find and return the Nth element.
Example1- If the three numbers are a=1, b=3, c=6 and N=17
The series will be formed as below –
1, 3, 6, 8, 11, 13, 16, 18, 21, 23, 26, 28, 31, 33, 36, 38, 41
17 th number in the series is 41
Example2- If the three numbers are a=5, b=-2, c=-4 and N=14
The series will be formed as below –
5, -2, -4, -11, -13, -20, -22, -29, -31, -38, -40, -47, -49, -56
14 th number in the series is -56
The function prototype should be as below –
int seriesN(int a, int b, int c, int N);

//Code

#include<stdio.h>
#include<string.h>
int findpassword(int input1,int input2,int input3,int input4,int input5)
{
int arr[]={input1,input2,input3,input4,input5};
int i,j=0,sum1=0,sum2=0;
for(i=0;i<5;i++)
{
j=stable(arr[i]);
if(j==1)
{
sum1=sum1+arr[i];
}
else if(j==2)
{
sum2=sum2+arr[i];
}
}
int res=sum1-sum2;
return res;

}
public int stable(int num)
{
int[] digitArray = new int[10];
for(int k=0;k<10;k++)
{
digitArray[k]=0;
}
int rem = 0;
int n=num;
while (num > 0)
{
rem= num % 10;
digitArray[rem]++;
num = num / 10;
}
int digitCount = digitArray[0];
int i,counter=0;
if(digitCount==0)
{
while(digitCount==0)
{
digitCount = digitArray[counter];
counter++;
}
for ( i = counter; i < digitArray.length; i++)
{
int val=digitArray[i];
if(val!=0 && digitCount!=val)
{
return 2;
}
}
return 1;
}
else
{
for (i = 1; i < digitArray.length; i++)
{
int val=digitArray[i];
if(val!=0 && digitCount!=val)
{
return 2;
}
}
return 1;
}
}

Question 2:

Find the one digit to be removed to form palindrome -
Assume that the given number input1 can become a palindrome if only one of its digit is removed. i.e. only one digit in the number is out of place. Find the digit that needs to be removed from input1 to convert input1 to a palindrome.

Example1: If input1 = 12332, the digit '1' needs to be removed to convert input1 to a palindrome 2332. So, the function should return 1.

Example2: If input1 = 251532, the digit '3' needs to be removed to convert input1 to a palindrome 25152. So, the function should return 3.

Example3: If input1 = 10101, NO digit needs to be removed to convert input1 to a palindrome, because it is already a palindrome. So we must return -1 in this case. Example4: If input1 = 981894, In the digit '4' needs to be removed to convert input1 to a palindrome 98189. So, the function should return 4.

//Code

#include<stdio.h>
#include<string.h>
int removeigit(int input)
{
int j=0,i;
String s=Integer.toString(input1);
if(s.equals(reverse(s)))
{
return -1;
}
else
{
int len=s.length();
for(i=0;i<len;i++)
{
String part=s.substring(0,i) + s.substring(i+1, len );
String reversed=reverse(part);
if(part.equals(reversed))
{
j=(int)(s.charAt(i))-48;
break;
}
}
return j;
}

}
public String reverse(String str)
{
String rev="";
for(int i=str.length()-1;i>=0;i--)
{
rev=rev+str.charAt(i);
}
return rev;
}