TCS ITP Advanced coding questions

TCS ITP Advanced Coding Questions

Following page contains TCS ITP Advance Coding Questions with Answers for freshers, along with sectional details, importance, difficulty etc.

The TCS Integrated test Pattern allows you to showcase your talent and skills and based on your test performance, you will either be shortlisted for Ninja Interviews or Digital Interviews.

Here below you will find similar type programming questions that are asked constantly in TCS ITP test.

TCS ITP Coding Questions

Question 1

Problem Description -: Given an array Arr[ ] of N integers and a positive integer K. The task is to cyclically rotate the array clockwise by K.

Note : Keep the first of the array unaltered.

Example 1:

  • 5  —Value of N
  • {10, 20, 30, 40, 50}  —Element of Arr[ ]
  • 2  —–Value of K

Output :

40 50 10 20 30

Example 2:

  • 4  —Value of N
  • {10, 20, 30, 40}  —Element of Arr[]
  • 1  —–Value of K

Output :

40 10 20 30

Run
#include<bits/stdc++.h>
using namespace std;

vector<int> rotate(int nums[], int n, int k) {

    if (k > n)
        k = k % n;

    vector<int> ans(n);

    for (int i = 0; i < k; i++) {
        ans[i] = nums[n - k + i];
    }

    int index = 0;
    for (int i = k; i < n; i++) {
        ans[i] = nums[index++];
    }
    return ans;
}

int main()
{
    int Array[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(Array) / sizeof(Array[0]);
    int K = 2;

    vector <int>ans = rotate(Array, N, K);
    for (int i = 0; i < N; ++i) {
        cout << ans[i] << ' ';
    }
}
Run
import java.util.*;
public class Main
{
    static int[] rotate(int nums[], int n, int k) {
        if (k > n)
            k = k % n;

        int[] ans = new int[n];

        for (int i = 0; i < k; i++) {
            ans[i] = nums[n - k + i];
        }

        int index = 0;
        for (int i = k; i < n; i++) {
            ans[i] = nums[index++];
        }
        return ans;
    }
    public static void main(String[] args) {
        int Array[] = { 1, 2, 3, 4, 5 };
        int N = 5;
        int K = 2;

        int[] ans = rotate(Array, N, K);
        for (int i = 0; i < N; ++i) {
            System.out.println(ans[i]);
        }
    }
}

Output :

4 5 1 2 3

Question 2

Problem Statement-:

Identify the logic behind the series

6 28 66 120 190 276….

The numbers in the series should be used to create a Pyramid. The base of the Pyramid will be the widest and will start converging towards the top where there will only be one element. Each successive layer will have one number less than that on the layer below it. The width of the Pyramid is specified by an input parameter N. In other words there will be N numbers on the bottom layer of the pyramid.

The Pyramid construction rules are as follows

  1. First number in the series should be at the top of the Pyramid
  2. Last N number of the series should be on the bottom-most layer of the Pyramid, with Nthnumber being the right-most number of this layer.
  3. Numbers less than 5-digits must be padded with zeroes to maintain the sanctity of a Pyramid when printed. Have a look at the examples below to get a pictorial understanding of what this rule actually means.

Example

If input is 2, output will be

  • 00006
  • 00028 00066

If input is 3, output will be

  • 00006
  • 00028 00066
  • 00120 00190 00276

Formal input and output specifications are stated below

Input Format:

  • First line of input will contain number N that corresponds to the width of the bottom-most layer of the Pyramid

Output Format:

  • The Pyramid constructed out of numbers in the series as per stated construction rules

Constraints:

  • 0 < N <= 14

 

Run
#include<stdio.h>
int main ()
{
  int n, a = 0, b = 3, i, re, j;
  n = 2;
  for (i = 1; i <= n; i++)
    {
      for (j = 1; j <= i; j++)
	{
	  a = a + 2;
	  if (i == 1)
	    b = 3;
	  else
	    b = b + 4;
	  re = a * b;
	  printf ("%.5d ", re);
	}
      printf ("\n");
    }
  return 0;
}

Run

import java.util.Scanner;
public class Main
{
  public static void main (String[]args)
  {
    int n, a = 0, b = 3, i, re, j;
  
      n = 2;
    for (i = 1; i <= n; i++)
      {
	for (j = 1; j <= i; j++)
	  {
	    a = a + 2;
	    if (i == 1)
	      b = 3;
	    else
	      b = b + 4;
	    re = a * b;
	    System.out.println (re);
	  }
	System.out.println ();
      }
  }
}
Run
n=2
a,b=0,3
for i in range(1,n+1):
    for j in range(1,i+1):
        a=a+2
        if(i==1):b=3
        else:b=b+4
        re=a*b
        print("%.5d"%(re),end=" ")
    print()

Output :

00006 
00028 00066 

Question 3

Problem Description -:  In this 3 Palindrome, Given an input string word, split the string into exactly 3 palindromic substrings. Working from left to right, choose the smallest split for the first substring that still allows the remaining word to be split into 2 palindromes.

Similarly, choose the smallest second palindromic substring that leaves a third palindromic substring.

If there is no way to split the word into exactly three palindromic substrings, print “Impossible” (without quotes). Every character of the string needs to be consumed.

Cases not allowed –

  • After finding 3 palindromes using above instructions, if any character of the original string remains unconsumed.
  • No character may be shared in forming 3 palindromes.

Constraints

  • 1 <= the length of input sting <= 1000

Input

  • First line contains the input string consisting of characters between [a-z].

Output

  • Print 3 substrings one on each line.

Time Limit

1

Examples

Example 1

Input

nayannamantenet

Output

nayan

naman

tenet

Explanation

  • The original string can be split into 3 palindromes as mentioned in the output.
  • However, if the input was nayanamantenet, then the answer would be “Impossible”.

Example 2

Input

aaaaa

Output

a

a

aaa

Explanation

  • The other ways to split the given string into 3 palindromes are as follows –
  • [a, aaa, a], [aaa, a, a], [aa, aa, a], etc.
  • Since we want to minimize the length of the first palindromic substring using left to right processing, the correct way to split is [a, a, aaa].

Example 3

Input

aaaabaaaa

Output

a

aaabaaa

a

Explanation

  • The other ways to split the given string into 3 palindromes are as follows –
  • [aaaa, b, aaaa], [aa, aabaa, aa], etc.
  • Since we want to minimize the length of the first palindromic substring using left to right processing, the correct way to split is [a, aaabaaa, a].
Run
#include<bits/stdc++.h>
typedef long long int lld;
#define mod 1000000007
using namespace std;
bool pali(string s)
{
    if(s.length()==1) return true;
    string s1=s;reverse(s1.begin(),s1.end());
    return (s1==s);
}
int main()
{
    
    string s,s1,s2,s3;
    cin>>s;
    int l=s.length();
    for(int i=1;i<l-1;i++)
    {
        s1=s.substr(0,i);
        if(pali(s1))
            for(int j=1;j<l-i;j++)
        {
            s2=s.substr(i,j);s3=s.substr(i+j,l-i-j);
            if(pali(s2)&&pali(s3))
            {
                cout<<s1<<endl<<s2<<endl<<s3;return 0;
            }
        }
    }
    cout<<"Impossible";
    return 0;
}
Run
import sys
def if_palindrome(s):
    if len(s)==1:
        return True
    s1=s[::-1]
    return s1==s
s=input()
l=len(s)
for i in range(1,l-1):
    s1=s[:i]
    if if_palindrome(s1):
        for j in range(1,l-i):
            s2=s[i:i+j]
            s3=s[i+j:]
            if if_palindrome(s2) and if_palindrome(s3):
                print(s1)
                print(s2)
                print(s3)
                sys.exit()
print("Impossible")
Run
import java.util.*;
class Main
{

    public static boolean isPalindrome (String s) 
    {

        if (s.length () == 1)
            return true;
        StringBuilder sb = new StringBuilder (s);
        sb = sb.reverse ();
        String rev = new String (sb);
        return s.equals (rev);
    }

    public static void main (String[]args) 
    {
        Scanner sc = new Scanner (System.in);
        String str = sc.next ();

        int len = str.length ();
        String str1 = "", str2 = "", str3 = "";

        boolean flag = false;

        for (int i = 1; i < len - 1; i++)
        {

            str1 = str.substring (0, i);
            if (isPalindrome (str1))
        	{
                for (int j = 1; j < len - i; j++)
        	    {
		            str2 = str.substring (i, i + j);
                    str3 = str.substring (i + j, len);
                    if (isPalindrome (str2) && isPalindrome (str3))
            		{
                        System.out.println (str1 + "\n" + str2 + "\n" + str3);
                        flag = true;
                        break;
                    }
                }
                if (flag)
                    break;
            }
        }
        if (flag == false)
            System.out.println ("Impossible");
    }
}

Question 4

Problem Statement -: In this even odd problem Given a range [low, high] (both inclusive), select K numbers from the range (a number can be chosen multiple times) such that sum of those K numbers is even.

Calculate the number of all such permutations.

As this number can be large, print it modulo (1e9 +7).

Constraints

  • 0 <= low <= high <= 10^9
  • K <= 10^6.

Input

  • First line contains two space separated integers denoting low and high respectively
  • Second line contains a single integer K.

Output

  • Print a single integer denoting the number of all such permutations

Time Limit

1

Examples

Example 1

Input

4 5

3

Output

4

Explanation

There are 4 valid permutations viz. {4, 4, 4}, {4, 5, 5}, {5, 4, 5} and {5, 5, 4} which sum up to an even number.

Example 2

Input

1 10

2

Output

50

Explanation

There are 50 valid permutations viz. {1,1}, {1, 3},.. {1, 9} {2,2}, {2, 4},… {2, 10} . . . {10, 2}, {10, 4},… {10, 10}. These 50 permutations, each sum up to an even number.

Run
#include<bits/stdc++.h>
using namespace std;
typedef long long int lld;
#define mod 1000000007

long e_sum(long m,long n,long K,long N)
{
    if(K==1)
    {
       return n;
    }
    else
    {
        return (N-(m-n)*e_sum(m,n,K-1,N)%(1000000007));
    }
}
int main()
{
    long low,high,K,m,n,diff,Out,N,i;
    scanf("%ld",&low);
    scanf("%ld",&high);
    scanf("%ld",&K);
    diff=high-low+1;
    if(diff%2==0)
    {
        m=diff/2;
        n=m;
    }
    else
    {
        if(low%2==0)
        {
            m=(diff-1)/2;
            n=m+1;
        }
        else
        {
            m=(diff+1)/2;
            n=m-1;
        }
    }
    N=m;
    for(i=0;i<K-1;i++)
    {
        N=(N*(m+n))%1000000007;
    }
    Out=e_sum(m,n,K,N)%1000000007;
    printf("%ld",Out);
    return 0;
}
Run
mod=1000000007
def esum(m,n,K,N):
    if(K==1):
       return n
    else:
        return (N-(m-n)*esum(m,n,K-1,N)%mod)
 
  low=int(input())
high=int(input())
K=int(input())
diff=high-low+1
if(diff%2==0):
    m=diff//2
    n=m
else:
    if(low%2==0):
        m=(diff-1)//2
        n=m+1
    else:
        m=(diff+1)//2
        n=m-1
N=m
for i in range(K-1):
    N=(N*(m+n))%mod
out=esum(m,n,K,N)%mod
print(out)
Run
import java.util.*;
class Main
{
    public static int evenSum (int m, int n, int k, int N) 
    {
        if (k == 1)
            return n;
        else
            return (N - (m - n) * evenSum (m, n, k - 1, n) % (1000000007));
    }

    public static void main (String[]args) 
    {
        Scanner sc = new Scanner (System.in);
        int low = sc.nextInt ();
        int high = sc.nextInt ();
        int k = sc.nextInt ();
        int diff = high - low + 1;
        int out, n, N, m;

        if (diff % 2 == 0)
        {
            m = diff / 2;
            n = m;
        }
        else
        {

            if (low % 2 == 0)
    	    {
                m = (diff - 1) / 2;
                n = m + 1;
            }
        	else
    	    {
                m = (diff + 1) / 2;
                n = m - 1;
            }
        }
        N = m;
        for (int i = 0; i < k - 1; i++)
            N = (N * (m + n)) % 1000000007;
        out = evenSum (m, n, k, N) % 1000000007;
        System.out.println (out);
    } 
}

Question 5

Problem Statement:- Jaya invented a Time Machine and wants to test it by time-traveling to visit Russia on the Day of Programmer (the 256thday of the year) during a year in the inclusive range from 1700 to 2700. From 1700 to 1917 , Russia’s official calendar was the Julian Calendar since 1919 they used the Gregorian calendar system. The transition from the Julian to Gregorian calendar system occurred in 1918 , when the next day after 31 January was February 14 . This means that in 1918, February 14 was the 32nd day of the year in Russia. In both calendar systems, February is the only month with a variable amount of days; it has 29 days during a leap year, and 28 days during all other years. In the Julian calendar, leap years are divisible by 4 ; in the Gregorian calendar, leap years are either of the following:

  • Divisible by 400
  • Divisible by 4 and not divisible by 100

Given a year, y, find the date of the 256th day of that year according to the official Russian calendar during that year. Then print it in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is y.

For example, the given year is 1984.1984 is divisible by 4, so it is a leap year. The 256 day of a leap year after 1918 is September 12, so the answer is 12.9.1984. 

Function Description

  • Complete the programmerday function in the editor below. It should return a string representing the date of the 256th day of the year given.
  • programmerday has the following parameter(s):
    • year: an integer 

Input Format

  • A single integer denoting year y.

Output Format

  • Print the full date of programmerday during year y in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is y.

Sample Input

           2017

Sample Output

           13.09.2017

Run
import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        int year = 2017;
        if(year>=1700 && year<=1917){
            if(year%4==0){
                System.out.println("12.09."+year);
                }
            else{
                System.out.println("13.09."+year);
                }
        
        }
        else if(year==1918){
            System.out.println("26.09."+year);
            }
        else{
            if(( year%400 == 0) | (( year%4 == 0 ) && ( year%100 != 0))){
                    System.out.println("12.09."+year);
            }
            else{
                System.out.println("13.09."+year);
                }
        }
    }
}
Run
def dayOfProgrammer(year):
    if year>=1700 and year<=1917:
        if year%4==0:
            return "12.09."+str(year)
        else:
            return "13.09."+str(year)
    elif year==1918:
        return "26.09."+str(year)
    else:
        a=not(year%100)
        if (( year%400 == 0)or (( year%4 == 0 ) and ( year%100 != 0))):
            return "12.09."+str(year)
        else:
            return "13.09."+str(year)
year = 2017
result = dayOfProgrammer(year)
print(result)
Run
#include<iostream>
#include <math.h>
using namespace std;


int main() {

 int n=2017;

 if(n==1918)
 cout<<"26.09.1918";
 else if(n==1700 || n==1800 || n==1900)
 cout<<"12.09."<<n;
 else if(n%400==0)
 cout<<"12.09."<<n;

 else if(n%4==0)
 {
 if(n%100==0)
 cout<<"13.09."<<n;

 else 
 cout<<"12.09."<<n;

 }
 else 
 cout<<"13.09."<<n;;
 return 0;
}
Run
#include<stdio.h>
int main()
{
    int year;
    year=2017;
    if(year>=1700 && year<=1917)
    {
       if(year%4==0)
       {
           printf("12.09.%d",year);
       }
       else
       {
           printf("13.09.%d",year);
       }
    }
    else if(year==1918)
    {
        printf("26.09.%d",year);
    }
    else
    {
        if((year%400==0)||((year%4==0)&&(year%100!=0)))
        {
            printf("12.09.%d",year);
        }
        else
        {
            printf("13.09.%d",year);
        }       
    }
   return 0; 
}

Question 6

Problem Description

Question -: A positive integer d is said to be a factor of another positive integer N if when N is divided by d, the remainder obtained is zero. For example, for number 12, there are 6 factors 1, 2, 3, 4, 6, 12. Every positive integer k has at least two factors, 1 and the number k itself.Given two positive integers N and k, write a program to print the kth largest factor of N.

Input Format: The input is a comma-separated list of positive integer pairs (N, k).

Output Format: The kth highest factor of N. If N does not have k factors, the output should be 1.

Constraints:

  • 1<N<10000000000
  • 1<k<600.

You can assume that N will have no prime factors which are larger than 13.

Example 1

  • Input: 12,3
  • Output: 4

Explanation: N is 12, k is 3. The factors of 12 are (1,2,3,4,6,12). The highest factor is 12 and the third largest factor is 4. The output must be 4.

Example 2

  • Input: 30,9
  • Output: 1

Explanation: N is 30, k is 9. The factors of 30 are (1,2,3,5,6,10,15,30). There are only 8 factors. As k is more than the number of factors, the output is 1.

Run
#include<stdio.h>  
void main() 
{
int number,pos_of_factor,i,c=0;
scanf("%d",&number);
scanf("%d",&pos_of_factor);
for(i=number;i>=1;i--)
{
  if((number%i)==0)
  c++;
  if(c==pos_of_factor)
  {
  printf("%d",i);
  break;
  }
}
if(c<pos_of_factor)
printf("1");
}
Run
#include <iostream>
using namespace std;
int main() 
{
int number,pos_of_factor,i,c=0;
cin>>number;
cin>>pos_of_factor;
for(i=number;i>=1;i--)
{
  if((number%i)==0)
     c++;
  if(c==pos_of_factor)
  {
     cout<<i;
     break;
  }
}
if(c<pos_of_factor)
  cout<<1;
return 0;
}
Run
import java.util.Scanner;
public class Main  {
       public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       
       int number,r,i,count=0;
       number = sc.nextInt();
       r = sc.nextInt();
       
       for (i = number; i >= 1; i--) 
       {
          if((number%i)==0)
              count++;
          if(count==r)
          {
              System.out.println(i);
              break;
          }
       }
       if(count!=r)
           System.out.println(1);
    

    }

}
Run
number, k = [int(i) for i in input().split(",")]
factor = []
count = 0
for i in range(1, number+1):
    if number % i == 0:
        count = count + 1
        factor.append(i)

if count < k:
    print("1")
else:
    print(factor[k])

Question 7

Problem Statement:- In a Conference ,attendees are invited for a dinner after the conference.The Co-ordinator,Sagar arranged around round tables for dinner and want to have an impactful seating experience for the attendees.Before finalizing the seating arrangement,he wants to analyze all the possible arrangements.These are R round tables and N attendees.In case where N is an exact multiple of R,the number of attendees must be exactly N//R,,If N is not an exact multiple of R, then the distribution of attendees must be as equal as possible.Please refer to the example section before for better understanding.
For example, R = 2 and N = 3
All possible seating arrangements are
(1,2) & (3)
(1,3) & (2)
(2,3) & (1)
Attendees are numbered from 1 to N.

Input Format:

  • The first line contains T denoting the number of test cases.
  • Each test case contains two space separated integers R and N, Where R denotes the number of round tables and N denotes the number of attendees.

Output Format:

Single Integer S denoting the number of possible unique arrangements.

Constraints:

  • 0 <= R <= 10(Integer)
  • 0 < N <= 20 (Integer)
Sample Input 1:
1
2 5
Sample Output 1:

10

Explanation:

R = 2, N = 5

(1,2,3) & (4,5)

(1,2,4) & (3,5)

(1,2,5) & (3,4)

(1,3,4) & (2,5)

(1,3,5) & (2,4)

(1,4,5) & (2,3)

(2,3,4) & (1,5)

(2,3,5) & (1,4)

(2,4,5) & (1,3)

(3,4,5) & (1,2)

Arrangements like

(1,2,3) & (4,5)

(2,1,3) & (4,5)

(2,3,1) & (4,5) etc.

But as it is a round table,all the above arrangements are same.

Run
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
map <ll,ll> dp;
int fac(int n)
{
   if(dp[n])
   return dp[n];
    dp[n]=n*fac(n-1);
   return dp[n];
}
int func(int n)
{
    if(n<=3)
        return 1;
    return fac(n-1);
}

int main()
{
    dp[0]=dp[1]=1;
    int tests;cin>>tests;
    while(tests--)
    {int R,N,c=1;
    cin>>R>>N;
    if(N<=R)
    {
        cout<<1;
        continue;
    }
    int a=N/R,n=N%R;
     ll ans=fac(N)/(pow(fac(a),R-n) * pow(fac(a+1),n) )/fac(n)/fac(R-n);
    for(int i=1;i<=n;i++)
        c*=func(a);
    for(int i=n+1;i<=R;i++)
    c*=func(a-1);

cout<<c*ans;}
}
Run
testcases = int(input())
for i in range(testcases):
    tables, people = map(int, input().split())
    result = 1
    if tables >= people:
        print(result)
    else:
        PA = people // tables
        PB = PA + 1
        TB = people % tables
        TA = tables - TB

        # Using DP to store factorials pre-hand
        fact = [1]
        for j in range(1, people + 1):
            fact.append(j*fact[j-1])

        for i in range(TB):
            result = result * (fact[people]//(fact[PB]*fact[people-PB]))
            people = people - PB

        for i in range(TA):
            result = result * (fact[people]//(fact[PA]*fact[people-PA]))
            people = people - PA

        print(result)
Run
import java.util.*;
public class Main
{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int testcases = scanner.nextInt();
        int tables = 0,people;
        while (testcases-->0)
            tables = scanner.nextInt();
            people = scanner.nextInt();
            System.out.println(find(tables,people));
    }
    public static int find(int r,int n)
    {
        int x = n/r;
        int y1 = n%r;
        int x1 = 0;
        int ans1 = 1;
        while(r!=0)
        {
            if(y1>0)
            {
                x1 = x+1;
                y1 = y1-1;
            }
            else
            {
                x1 = x;
            }
            ans1 = ans1*combination(n,x1);
            n = n-x1;
            r--;
        }
        return ans1;
    }
    public static int factorial(int n)
    {
        if(n==0||n==1)
        {
            return 1;
        }
        return n * factorial(n-1);
    }
    public static int combination(int n,int r)
    {
        return factorial(n)/(factorial(n-r)*factorial(r));
    }
}

Question 8

Problem Description

In the theory of numbers, square free numbers have a special place.  A square free number is one that is not divisible by a perfect square (other than 1).  Thus 72 is divisible by 36 (a perfect square), and is not a square free number, but 70 has factors 1, 2, 5, 7, 10, 14, 35 and 70.  As none of these are perfect squares (other than 1), 70 is a square free number.

For some algorithms, it is important to find out the square free numbers that divide a number.  Note that 1 is not considered a square free number. 

In this problem, you are asked to write a program to find the number of square free numbers that divide a given number.

Input:-

  • The only line of the input is a single integer N which is divisible by no prime number larger than 19.

Output:-

  • One line containing an integer that gives the number of square free numbers (not including 1).

Constraints:-

  • N   < 10^9

Complexity:-

  • Simple

Time Limit:-

  • 1

Examples


Example 1

  • Input:-
    20

  • Output:-
    3

Explanation

N=20

If we list the numbers that divide 20, they are

1, 2, 4, 5, 10, 20

1 is not a square free number, 4 is a perfect square, and 20 is divisible by 4, a perfect square.  2 and 5, being prime, are square free, and 10 is divisible by 1,2,5 and 10, none of which are perfect squares.  Hence the square free numbers that divide 20 are 2, 5, 10.  Hence the result is 3.

Example 2

  • Input:-
    72

  • Output:-
    3

Explanation

N=72.  The numbers that divide 72 are

1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72

1 is not considered square free.   4, 9 and 36 are perfect squares, and 8,12,18,24 and 72 are divisible by one of the.  Hence only 2, 3 and 6 are square free.  (It is easily seen that none of them are divisible by a perfect square).  The result is 3.

Run
#include <stdio.h>
#include <math.h>
int main()
{
    long int n;
    double sqnum;
    long int i,j=0,flag,chksqr,temp[10000];
  	int count=0,k;
  	printf("Enter the number:");
    scanf("%ld",&n);
    
    for(i=2;i<=n/2;i++)
    {
        if(n%i==0)
        {    
            count++;
            sqnum=sqrt(i);
            chksqr=sqnum;
            if(chksqr==sqnum)
            {
                count--;
                temp[j]=i;
                j++;
            }
            else
            {
                for(k=0;k<j;k++) { if(i>temp[k] && j!=0)
                    {
                        if(i%temp[k]==0)
                        {	
                          	count--;
                        	k=j+1;
                        }
                    }
                    else
                        break;
                }
            }
        }
    }

    printf("%d",count);
    return 0;
}
Run
#include <iostream> 
#include  <math.h>
using namespace std;
int main()
{
    long int n;
    double sqnum;
    long int i,j=0,flag,chksqr,temp[10000];
  	int count=0,k;
  	cout<<"Enter the number:";
    cin>>n;
    
    for(i=2;i<=n/2;i++)
    {
        if(n%i==0)
        {    
            count++;
            sqnum=sqrt(i);
            chksqr=sqnum;
            if(chksqr==sqnum)
            {
                count--;
                temp[j]=i;
                j++;
            }
            else
            {
                for(k=0;k<j;k++) { if(i>temp[k] && j!=0)
                    {
                        if(i%temp[k]==0)
                        {	
                          	count--;
                        	k=j+1;
                        }
                    }
                    else
                        break;
                }
            }
        }
    }

    cout<<count;
    return 0;
}

Question 9

Problem Statement

Find the minimum number of coins required to form any value between 1 to N,both inclusive.Cumulative value of coins should not exceed N. Coin denominations are 1 Rupee, 2 Rupee and 5 Rupee.Let’s Understand the problem using the following example. Consider the value of N is 13, then the minimum number of coins required to formulate any value between 1 and 13, is 6. One 5 Rupee, three 2 Rupee and two 1 Rupee coins are required to realize any value between 1 and 13. Hence this is the answer.However, if one takes two 5 Rupee coins, one 2 rupee coin and two 1 rupee coin, then too all values between 1 and 13 are achieved. But since the cumulative value of all coins equals 14, i.e., exceeds 13, this is not the answer.

  • Input Format:
    • A single integer value.
  • Output Format:
    • Four space separated integer values.
      • 1st – Total number of coins.
      • 2nd – number of 5 Rupee coins.
      • 3rd – number of 2 Rupee coins.
      • 4th – number of 1 Rupee coins.
  • Constraints:
    • 0 < n < 1000

Refer the sample output for formatting

Sample Input

    13

Sample Output

   6 1 3 2

Explanation

  • The minimum number of coins required is 6 with in it:
    • minimum number of 5 Rupee coins = 1
    • minimum number of 2 Rupee coins = 3
    • minimum number of 1 Rupee coins = 2

Using these coins, we can form any value with in the given value and itself, like below:

Here the given value is 13

  • For 1 = one 1 Rupee coin
  • For 2 = one 2 Rupee coin
  • For 3 = one 1 Rupee coin and one 2 Rupee coins
  • For 4 = two 2 Rupee coins
  • For 5 = one 5 Rupee coin
  • For 6 = one 5 Rupee and one 1 Rupee coins
  • For 7 = one 5 Rupee and one 2 Rupee coins
  • For 8 = one 5 Rupee, one 2 Rupee and one 1 Rupee coins
  • For 9 = one 5 Rupee and two 2 Rupee coins
  • For 10 = one 5 Rupee, two 2 Rupee and one 1 Rupee coins
  • For 11 = one 5 Rupee, two 2 Rupee and two 1 Rupee coins
  • For 12 = one 5 Rupee, three 2 Rupee and one 1 Rupee coins
  • For 13 = one 5 Rupee, three 2 Rupee and two 1 Rupee coins
Run
number = int (input ())
five = int ((number - 4) / 5) 
if ((number - 5 * five) % 2)== 0:
    one = 2
else:
    one =1
two = (number - 5 * five - one)	//2
print (one + two + five, five, two, one)
Run
import java.util.*;
public class Main
{
  public static void main (String[]args)
  {
    System.out.println ("Enter the Number");
    Scanner s = new Scanner (System.in);
    int number = s.nextInt ();
    int one = 0, two = 0;
    int five = (number - 4) / 5;
    if (((number - 5 * five) % 2) == 0)
      {
	one = 2;
      }
    else
      {
	one = 1;
      }
    two = (number - 5 * five - one) / 2;
    System.out.println (one + two + five);
    System.out.println (five);
    System.out.println (two);
    System.out.println (one);

  }
}

Question 10

Problem Statement:-

Compute the nearest larger number by interchanging its digits updated.Given 2 numbers a and b find the smallest number greater than b by interchanging the digits of a and if not possible print -1.

  • Input Format
    2 numbers a and b, separated by space.
  • Output Format
    A single number greater than b.

    If not possible, print -1

  • Constraints

    1 <= a,b <= 10000000

Example 1:

Sample Input:

    459 500

Sample Output:
    549

Example 2:

Sample Input:

    645757 457765

Sample Output:
    465577

Run
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a;
    int b,c;
    cin>>a>>b;
    sort(a.begin(),a.end(),greater());

    c=atoi(a.c_str());

    if(b>c)
    {cout<<-1;
    return 0;}
    while(b<c)
    {
        prev_permutation(a.begin(),a.end());
        c=atoi(a.c_str());
    }
    next_permutation(a.begin(),a.end());
    cout<<a;
}
Run
#import itertools to get permutation function
from itertools import permutations
#take inputs
num1 = int(input('Enter the 1st number :'))
num2 = int(input('Enter the 2nd number :'))
#initialize a flag variable
flag = 0
#convert num1 to string list
num1 = list(str(num1))
#sort the list
num1 = sorted(num1)
#find all permutations
perm = permutations(num1) 
#iterate through all permutations 
for i in list(perm): 
    #initialize an string
    string = " "
    #iterate through an string
    for j in i:
        string+=j
    #typecast string to integer
    #check for next greater value
    if int(string) > num2:
        #if True Change the flag variable 
        #break the loop
        flag = 1
        break
#check if the number is found or not
if flag == 1:
    print(string)
else:
    print(-1)