TechoSagar: 2017-08-06

Search

Google Alert - jobs

Change the string

Given a string S, the task is to change the string according to the condition; If the first letter in a string is capital letter then change the full string to capital letters, else change the full string to small letters.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. 
Each test case contains a string S.
Output:
For each test case, print the changed string in a new line.
Constraints:
1<=T<=200
1<=|string length|<=104
Example:
Input:
2
geEkS
FoR

Output:
geeks
FOR

Your Code

#include<bits/stdc++.h>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int l=s.length();
int i;
if(islower(s[0]))
{
for(i=0; i<l; i++)
{
s[i]=tolower(s[i]);
cout<<s[i];
}
}
else
{
for(i=0; i<l; i++)
{
s[i]=toupper(s[i]);
cout<<s[i];
}
}
cout<<endl;
}

Balanced Array

Given an array of even size, task is to find minimum value that can be added to an element so that array become balanced. An array is balanced if the sum of the left half of the array elements is equal to the sum of right half.
Input:
The first line of input contains an integer T denoting the number of test cases. Each test case contains the number of elements in the array a[] as n and next line contains space separated n elements in the array a[].

Output:
Print an integer which is the required answer.

Constraints:
1<=T<=20
2<=n<=10000
1<=a[i]<=100000

Example:
Input:

2
6
1 2 1 2 1 3
2
20 10
Output:
2
10
Explanation:
Suppose, we have an array 1 2 1 2 1 3. The Sum of first three elements is 1 + 2 + 1 = 4 and sum of last three elements is 2 + 1 + 3 = 6
So this is unbalanced, to make it balanced the minimum number we can add is 2 to any element in first half.
Your Code 
#include<iostream>
#include<math.h>
#include <stdlib.h> 
using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[10000];
int i;
for(i=0; i<n; i++)
{
cin>>a[i];
}
int sum=0, sum1=0;
for(i=0; i<n/2; i++)
{
  sum=sum+a[i];
  sum1=sum1+a[n-i-1];
}
int d=sum-sum1;
int ans=abs(d);
cout<<ans<<endl;
}
}
 


Check Binary String

Given a binary string of 0 and 1, write a program to check that the given string is valid or not. The given string is valid when there is no zero is present in between 1’s. For example, 1111, 0000111110, 1111000 are valid strings but 01010011, 01010, 101 are not.

Input:
First line of input contains a single integer T which denotes the number of test cases. Then T test cases follows. First line of each test case contains a binary string.

Output:
For each test case print "VALID" without quotes if the string is valid otherwise print "INVALID" without quotes if the string is invalid.

Constraints:
1<=T<=100
2<=length(Str)<=105

Example:
Input:
3
100
1110001
00011
Output:
VALID
INVALID
VALID

Your Code

#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int l=s.length();
int i;
int temp=1;
int j;
for(i=0; i<l-2; i++)
{
if(s[i]=='1')
{
if(s[i+1]=='0')
{
  for(j=i+2; j<l; j++)
  {
  if(s[j]=='1')
  {
  temp=0;
     break;
  }
  }
}
}
}
if(temp==1)
{
cout<<"VALID"<<endl;

}
else
cout<<"INVALID"<<endl;
}

}

Krishnamurthy number

A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself. Given a number N as input. Write a program to check whether this number is krishnamurthy number or not.

Input:
First line of input contains a single integer T which denotes the number of test cases. Then T test cases follows. First line of each test case contains a single integer N.

Output:
For each test case print "YES" without quotes if N is a Krishnamurthy Number otherwise print "NO".

Constraints:
1<=T<=100
1<=N<=105

Example:
Input:

2
145
235
Output:
YES
NO

Your Code
#include<iostream>

using namespace std;
int fact(int x)
{
int i;
int product=1;
for(i=1; i<=x; i++)
{
product=product*i;
}
return product;
}

int main()
{
int t ;
cin>>t;
while(t--)
{
int n;
cin>>n;
int y=n;
int sum=0;
while(n!=0)
{
int digit=n%10;
n=n/10;
sum=sum+fact(digit);
}
if(sum==y)
{
cout<<"YES"<<endl;
}
else
cout<<"NO"<<endl;

}

}

Sort a String

Given a string S consisting of lowercase latin letters, arrange all its letters in ascending order. 
Input:
The first line of the input contains T denoting number of testcases.Then follows description of each testcase.The first line of the testcase contains positive integer N denoting the length of string.The second line contains the string S.

Output:
For each testcase,output the sorted string.

Constraints:
1<=T<=100
1<=N<=100

Example:
Input:
1
5
edcab
Output:
abcde

Your Code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
string a;
int n;
cin>>n;
cin.ignore();
getline(cin,a);
int i,k;
for(i=0; i<n-1; i++)
{
for(k=i+1; k<n; k++)
{
if(a[i]>a[k])
{  //cout<<a[i]<<endl;
char temp;
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
}
for(i=0; i<n; i++)
{
cout<<a[i];
}
cout<<endl;
}
}

Automorphic Number

Write a program to check whether a given number is Automorphic number or not.
A number is called Automorphic number if and only if its square ends in the same digits as the number itself.
For example, 762 = 5776 which ends with 76 therefore it is a automorphic number.

Input:
The first line of the input contains T denoting the total number of testcases. Each of the next T lines contains a positive integer N denoting the value of a number.

Output:
Output "Automorphic" if given number is Automorphic else "Not Automorphic" .

Constraints:
1<=T<=100
1<=N<=1000

Example:
Input:
2
1
16
Output:
Automorphic
Not Automorphic

Your Code
#include<iostream>

using namespace std;
#include<math.h>
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int x=n;
long int s;
s=n*n;
int  num=0;
while(n!=0)
{
n=n/10;
num++;
}
int i,number=0;
for(i=0; i<num; i++)
{
int k;
k=s%10;
number=number+k*pow(10,i);
s=s/10;
}
//cout<<number<<" "<<n<<endl;
if(number==x)
cout<<"Automorphic"<<endl;
else
cout<<"Not Automorphic"<<endl;
}
return 0;

}

Sieve of Eratosthenes

Given a number n, calculate the prime numbers upto n using Sieve of Eratosthenes.  
Input: The first line of the input contains T denoting the number of testcases. First line of test case is the number to which we have to compute prime numbers.
Output: All the prime numbers upto or equal to n are displayed.
Constraints:
1 <=T<= 100
1 <=N<= 10000
Example:
Input:
2
10
35
Output:
2 3 5 7
2 3 5 7 11 13 17 19 23 29 31 
Your Code
#include<iostream>

using namespace std;
 void prime(int);
int main()
{
int t,x,i, temp=1;
cin>>t;
while(t--)
{
int n;
cin>>n;
if(n!=1)
{
for(x=2; x<=n; x++) 
{   
 prime(x);
}
}
cout<<endl;
}
return 0;
}

void prime(int n)
{ int i, temp=1;


 for(i=2; i<=n/2; i++)
  {
  if(n%i==0)
  {
  temp=0;
  break;
    }
 }  

if(i!=1)
{


  if(temp==1)
  {
   cout<<n<<" ";
  
  }
}
}

Print the Kth Digit

Given two numbers a and b, find kth digit from right of a^b.
Input:
The first line of the input contains T denoting the number of test cases.Each of the next T lines contains three space separated positive integers denoting the value of a , b and k respectively.

Output:
For each test case, output the kth digit from right of a^b in new line.

Constraints:
1<=T<=100
1<=a , b <=15
1<=k<=|totaldigits in a^b|

Example:
Input:
2
3 3 1
5 2 2
Output:
7
2
Your Code
#include<iostream>

using namespace std;
#include<math.h>

int main()
{
int t;
cin>>t;
while(t--)
{
int a,b,k;
cin>>a>>b>>k;;
long int c;
c=pow(a, b);
int i,digit;
for(i=0; i<k; i++)
{
digit=c%10;
c=c/10;
}
cout<<digit<<endl;
}
return 0;
}

Follow us

Follow Bijendra Kumar on Facebook