Cognizant GenC Elevate Coding Questions 2024 (Latest) | Cognizant GenC Elevate MCQ Questio

Cognizant GenC Elevate Coding Questions 2024 | Cognizant GenC Elevate SQL Questions | Cognizant GenC Elevate Questions and Answers | Cognizant GenC Elevate Expected Questions | Cognizant GenC Elevate Previous Year Question Paper 

Cognizant GenC Elevate Coding Questions 2024 | Cognizant GenC Elevate MCQ Questions & Answers:  Generation Cognizant is the company that created Genc Elevate. It is an entry-level position at Cognizant for those with little or no prior job experience. A set of Genc Elevate coding questions are posed to users to evaluate their coding and cognitive skills. Genc Elevate Coding Questions range in difficulty from simple to moderate. We’ll examine many Cognizant GenC Elevate Coding Questions in this post and talk about their answers.

One of the most crucial aspects of GenC Elevate’s employment process is coding. Your programming efficiency and problem-solving skills will be evaluated in this part. You will have four code questions based on diverse topics in this round, and you must offer the answers in any of the programming languages provided. Only Java and Python were permitted for Cognizant’s hiring last year, even though GenC Elevate did not specify a language. Nevertheless, we advise you to place greater emphasis on Java and Python. To get more Private Jobs, click here

Also, Check Out
Cognizant GenC Elevate Technical Assessment Questions
Cognizant GenC Aptitude Questions with Answers  Cognizant BPS Hiring Assessment Questions and Answers 
Cognizant Previous Year Question Papers PDF Cognizant GenC Salary for Freshers 
Cognizant Hike 2024 Predictions Salary After 1 Year in Cognizant

Cognizant GenC Elevate Coding Syllabus 2024

To pass any exam, applicants must have the following two things: A solid preparation strategy and the latest Cognizant GenC Elevate syllabus. To achieve this, it is essential to know the syllabus in-depth and create a smart strategy for exam preparation. For better clarity on the upcoming Cognizant GenC Elevate Exams, applicants should refer to the Exam pattern & Syllabus

  • Arrays
  • Stacks
  • Operations on Numbers and strings
  • Data Structures
  • Dynamic Programming
  • Application of Algorithms like
    • Knapsack
    • N Queen’s Problem
    • Dining Philosopher
    • Tower of Hanoi
    • +many more…

GenC Elevate Coding Questions and Solutions 2024

 Print the length of a string without using the string functions.

import java.util.*;
class ninjaSolution {
    public static void main(String[] args) {
        String str = "Hey Ninja!!";
        int len=0;
        char[] strCharArray=str.toCharArray();
        for(char c:strCharArray){
            len++;
        }
        System.out.println("Lenght of string "+str+" is: "+len);
    }
}

Write a code to add two numbers without using arithmetic operations.

class ninjaSolution {
    public static void main(String[] args) {
        int num1 = 10, num2 = 50;
        while (num2 != 0){
            int carry = (num1 & num2) ; //CARRY is AND of two bits
          
            num1 = num1^num2; //SUM of two bits is A XOR B
          
            num2 = carry << 1; //shifts carry to 1 bit to calculate sum
        }
        System.out.println("Sum of 10 and 50 is: "+num1);
    }
}

Write a code to print numbers from 1 to 10 without using a loop or recursion in C++.

#include <iostream>
using namespace std;
 
template<int n>
class PrintZeroToN
{
public:
   static void display()
   {
       PrintZeroToN<n-1>::display();
       cout << n << endl;
   }
};
 
template<>
class PrintZeroToN<0>
{
public:
   static void display()
   {
       cout << 0 << endl;
   }
};
int main()
{
   const int n = 10;
   PrintZeroToN<n>::display();
   return 0;
}

Print the address of a variable without using a pointer.

We can directly print the address of a variable using the ‘&’ symbol in c,

#include <stdio.h>
int main()
{
   int x;
   printf("Address of x: %p\n", &x);
   return 0;
}

Write a program to check whether the given number is even or odd

import java.util.*;
class ninjaSolution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please Enter a number!!");
        int num = sc.nextInt();
        if(num%2==0)
            System.out.println(num+" is even");
        else
            System.out.println(num+" is odd");
    }
}

Write a program to print the Fibonacci series.

import java.util.*;
class ninjaSolution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the limit!!");
        int limit = sc.nextInt();
         int n1 = 0, n2=1, n3;
        System.out.println("Fibonacci series upto "+limit+" numnber");
        System.out.print("\n\n"+n1+" "+n2+" ");
        for(int i=2;i<limit;i++){
            n3 = n1+n2;
            n1 = n2;
            n2 = n3;
            System.out.print(" "+n3+" ");
        }
    }
}

Write a code to check whether a number is prime or not.

import java.util.*;
class ninjaSolution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a number");
        int num = sc.nextInt();
        int count=0;
        for(int i=2;i<num/2;i++){
            if(num%i==0)
                count++;
        }
        if(count>0)
            System.out.println("Number is not prime");
        else
            System.out.println("Number is prime");
    }
}

Cognizant GenC Elevate MCQ Questions

Questions) We_____________to inform you that we cannot include your thesis in our library, on the __________________of not receiving permission from your supervisor.

  1. saddened, reason
  2. lament, pretext
  3. repent, justification
  4. regret, grounds

ANSWER: Option D

EXPLANATION:

‘regret’ is a feeling of disappointment.
repent is feeling remorse for one’s own wrong doings.
lament is to mourn.
Hence the 4th option is the best fit.

Questions) When her illness took a turn for the worse, the doctors took _____________ to ____________ the situation.

  1. steps, ameliorate
  2. approaches, better
  3. modes, improve
  4. measures, enhance

ANSWER: Option C

EXPLANATION: ‘Ameliorate’ – to make better, improve. Modes does not fit the first blank so option 3.

Questions)

  1. A temple was erected to him
  2. at the foot of the Capitoline Hill,
  3. in which were deposited the
  4. public treasury and the laws of the state.

ANSWER: Option A

EXPLANATION: A temple was erected in his honour.

Pick the solution that provides the optimum structure for the sentence to ensure that the point is made clearly

Questions)

  1. There were great stable, where a dozen grooms and boys held forth, rows of vine-clad servant cottages, an endless and orderly array of outhouses, long grape arbors, green pastures, orchards, and berry patches.
  2. There were great stables, where a dozen grooms and boys held forth, rows of vine-clad servants’ cottages, an endless and orderly array of outhouses, long grape arbors, green pastures, orchards, and berry patches.
  3. There are great stables, where a dozen grooms and boys held forth, rows of vine-clad servants’ cottages, an endless and order array of outhouses, long grape arbors, green pastures, orchards, and berry patches.
  4. There were great stables, where a dozen grooms and boys held forth, rows of vine-clad servant’s cottages, an endless and orderly array of outhouse, long grape arbor, green pasture, orchard, and berry patch.

ANSWER: Option B

EXPLANATION:

In the 1st sentence- ‘stable’ does not agree with ‘were’, so a subject verb agreement error.
2nd sentence-correct.
3rd sentence- there ‘are’, the rest of the sentence is in past tense, so there is a tense mismatch.
4th sentence- servant’s, should be servants’.

Questions)

  1. Then there is the pumping plant for the artesian well, and the big cement tank where Judge Miller’s boys took their morning plunge and kept cool in the hot afternoon.
  2. hen there was the pumping plant for the artesian well, and the big cement tank where Judge Miller’s boys took their morning plunge and kept cool in the hot afternoon.
  3. hen there was the pumping plant for the artesian well, and the big cement tank were Judge Miller’s boys took their morning plunge and keep cool in the hot afternoon.
  4. Then there was the pumping plant though the artesian well, and the big cement tank where Judge Miller’s boys took their morning plunge and kept cool in the hot afternoon.

ANSWER: Answer: Option B

EXPLANATION:

1st sentence- there is a tense mismatch.
2nd sentence- correct.
3rd sentence- ‘keep’ cool should be kept.
4th sentence – ‘through’ the artesian well is incorrect.

Questions) According to the passage, the author

I. received an Oscar for her portrayal as a feisty heroine in an epic drama.
II. feels that moderation is the key to well being in cyber space.
III. feels that she is the centre of all she checks in the net.

  1. I, II and III are correct.
  2. I and II are correct.
  3. II and III are correct.
  4. I and III are correct.

ANSWER: Option C

Questions) It can be inferred from the passage that

  1. you have gone too far when you start googling your name multiple times daily.
  2. competitive rival surfing is the best way to increase your self esteem.
  3. being addicted to self googling is the way forward in the 21st century.
  4. the advent of the internet has helped in creating ‘a somebody’ out of ‘a nobody’.

ANSWER: Option D

EXPLANATION: We can’t infer that somebody has gone far if one starts googling one’s name multiple times. Nothing can be inferred about increasing self esteem. Option C is out of scope.

Questions) According to the passage,

  1. Helen Walne is a patient of cancer.
  2. Helen Walne is a megabyte megalomaniac.
  3. Helen Walne thinks that constant rival surfing is the best way to improve self worth.
  4. Helen Walne is a conscientious worker.

ANSWER: Option B

EXPLANATION: Megalomania is an obsession with doing extravagant or grand things. Helen Wayne is also obsessed with this. The other options are out of scope.

Questions) According to the passage, all of the following are not true, except

  1. Helen Walne received a prize wearing a turtle neck.
  2. Helen Walne’s husband feels she needs help to get out of her addiction.
  3. In reality there is very little difference in the number of internet users today and five years ago.
  4. Rival surfing gives a better job

ANSWER: Option B

EXPLANATION: Refer to 1st para line “My husband has gently…..addiction.”

Questions)

A. The upsurge of public activism against the setting up of Special Economic Zones, which eventually forced the State government to announce the scrapping of all 15 such projects, is an impressive case in point.
B. Early last year, a similar agitation coerced the government into calling for a revision of the Goa Regional Plan 2Q11, a controversial document that opened up large swathes of land, including green belts and coastal stretches, for construction.
C. The broad-based agitation against SEZs has demonstrated the power of popular protest in the State.
D. Those opposed to the projects had questioned the propriety of the government acquiring large tracts of land and then selling them to promoters at low prices.
E. A coastal State with an area of 3,700 square kilometers and a population of about 1.4 million, Goa has always been extremely sensitive to the impact of unrestrained economic development.

  1. BCDEA
  2. CDEAB
  3. EABCD
  4. DABCE

ANSWER: Option C

EXPLANATION: Sentence E introduces ‘Goa that has been extremely sensitive to the impact of unrestrained economic development’. A exemplifies on ‘the upsurge. B carries the idea forward with ‘a similar agitation’

Cognizant GenC Elevate Coding Round Questions and Solutions 

Question) Given a positive whole number n, find the smallest number which has the very same digits existing in the whole number n and is greater than n. In the event that no such certain number exists, return – 1.

Note: that the returned number should fit in a 32-digit number, if there is a substantial answer however it doesn’t fit in a 32-bit number, return – 1.

Example 1:

Input: n = 12
Output: 21
Explanation: Using the same digit as the number of permutations, the next greatest number for 12 is 21.

Example 2:

Input: n = 21
Output: -1
Explanation: The returned integer does not fit in a 32-bit integer

C++ PROGRAM

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

void swap(char *a, char *b)
{
	char temp = *a;
	*a = *b;
	*b = temp;
}

void findNext(char number[], int n)
{
	int i, j;

	for (i = n-1; i > 0; i--)
		if (number[i] > number[i-1])
		break;

	if (i == 0)
	{
		cout << "Next number is not possible";
		return;
	}

	int x = number[i-1], smallest = i;
	for (j = i+1; j < n; j++) if (number[j] > x && number[j] < number[smallest])
			smallest = j;

	swap(&number[smallest], &number[i-1]);


	sort(number + i, number + n);

	cout << number; return; } int main() { char digits[]; cin >> digits
	int n = strlen(digits);
	findNext(digits, n);
	return 0;
}

JAVA PROGRAM

import java.util.*;
public class Main
{
	static void swap(char ar[], int i, int j)
	{
		char temp = ar[i];
		ar[i] = ar[j];
		ar[j] = temp;
	}
	
	static void findNext(char ar[], int n)
	{
		int i;
		
		for (i = n - 1; i > 0; i--)
		{
			if (ar[i] > ar[i - 1]) {
				break;
			}
		}
		
		if (i == 0)
		{
			System.out.println("Not possible");
		}
		else
		{
			int x = ar[i - 1], min = i;
			
			
			for (int j = i + 1; j < n; j++)
			{
				if (ar[j] > x && ar[j] < ar[min])
				{
					min = j;
				}
			}
		
			swap(ar, i - 1, min);
			Arrays.sort(ar, i, n);
			
			for (i = 0; i < n; i++)
				System.out.print(ar[i]);
		}
	}

	public static void main(String[] args)
	{
	    Scanner sc = new Scanner(System.in);
	    int n =sc.nextInt();
	    String s = sc.next();
		char digits[] = s.toCharArray();
		
		findNext(digits, n);
	}
}

PYTHON PROGRAM

arr = []
def permute(s,ans):
    n = len(s)
    if (n == 0):
        arr.append(ans)
        return
    for i in range(n):
        ch = s[i]
        L = s[0:i]
        R = s[i + 1:]
        REM = L+ R
        permute(REM,ans + ch)
ans= ""
s = input()
n = len(s)
permute(s,ans)
arr = list(set(arr))
arr.sort()
ind = arr.index(s)
if(ind == len(arr)-1):
    print(-1)
else:
    print(arr[ind+1])

Question) Henry is extremely keen on history and every one of the ages of his family. He does a ton of exploration and understands that he has plummeted from the incomparable Amaya line. After a ton of looking through old records and the most recent records of the general public, he can discover all the parent-kid connections in his family right from the extraordinary ruler Ming of the tradition to himself.

These connections are given in the structure of a direct exhibit where the emperor is at the main position and his kids are at pos (2i + 1) and (2i + 2)

This is the pattern followed throughout.

Henry needs to sort out every one of the kin of individual X from the information.

Write a program for Henry to figure out all the siblings of person X from the data.
Return the sorted list of all of Henry’s siblings.

If no sibling return {-1}

  • input 1: N, the length of the array
  • input2: An array representing the ancestral tree
  • input 3: X, the person whose siblings are sought.
  • output – return the array of all siblings in increasingly sorted order.

Example 1 :

input 1: 5
input 2: {1,2,3,4,5}
input 3: 1
output: {-1}
Explanation: x is the root of the tree and has no siblings

Example 2 :

input 1: 6
input 2: {1,2,3,4,5,6}
input 3: 5
output: {4,6}
Explanation: {2,3 } are the children of {1}. {4,5,6 } are the children of {2,3}, thus the siblings of x= 5 are {4,6}

JAVA PROGRAM

import java.util.*;
 
public class Main
{
    Node root;
    static class Node
    {
        int data;
        Node left, right;
    
        Node (int data) 
        {
            this.data = data;
            this.left = null;
            this.right = null;
        } 
    } 

    public static void main (String[]args)
    {
        Main t2 = new Main ();
        Scanner sc = new Scanner (System.in);
        int length = sc.nextInt ();
        int arr[] = new int[length];
    
        for (int i = 0; i < length; i++)
        {
            arr[i] = sc.nextInt ();
        }
        int target = sc.nextInt ();
        t2.root = t2.insertLevelOrder (arr, t2.root, 0);
    
        Set < Integer > sets = t2.levelOrderBottom (t2.root, target);
        sets.remove (target);
        System.out.println (sets);
    }   
    public static Node insertLevelOrder (int[]arr, Node root, int i) 
    {
        if (i < arr.length)
        {
            Node temp = new Node (arr[i]);
            root = temp;
            root.left = insertLevelOrder (arr, root.left, 2 * i + 1);
            root.right = insertLevelOrder (arr, root.right, 2 * i + 2);
        }
        return root;
    }

    public Set < Integer > levelOrderBottom (Node root, int target)
    {
        if (root == null)
        {
        	return null;
        }
        Queue < Node > q = new LinkedList <> ();
        q.offer (root);
    
        while (!q.isEmpty ())
        {
            int qsize = q.size ();
            Set < Integer > temp = new HashSet <> ();
            
            for (int i = 0; i < qsize; i++)
	        {
                Node child = q.poll ();
                temp.add (child.data);
                
                if (child.left != null)
	            {
                    q.offer (child.left);
                }
                if (child.right != null)
	            {
                    q.offer (child.right);
                }
            }
            if (temp.contains (target))
                return temp;
        }
        return null;
    }
}

PYTHON PROGRAM

def identify_siblings(tree_array, x):
    tree_len = len(tree_array)
    index = tree_array.index(x)
    level = 0
    start_index = level
    number_of_nodes = 0
    while start_index < tree_len:
    	end_index = pow(2, level) + start_index
    	if x in tree_array[start_index:end_index]:
    		break
    	level += 1
    	start_index = (2 * start_index) + 1
    final_array = tree_array[start_index:end_index]
    final_array.remove(x)
    return final_array if final_array else [-1]

n = int(input())
arr = []
for i in range(n):
    arr.append(int(input()))
x = int(input())
print(identify_siblings(arr, x))

Question) You want to buy a particular stock at its lowest price and sell it later at its highest price. Since the stock market is unpredictable, you steal the price plans of a company for this stock for the next N days.
Find the best price you can get to buy this stock to achieve maximum profit.

Note: The initial price of the stock is 0.

Input Specification:

Input1: N, number of days
Input2: Array representing change in stock price for the day.

Output Specification:

Your function must return the best price to buy the stock at.

Example1:

Input1: 5
Input2: (-39957,-17136,35466,21820,-26711}
Output: -57093
Explanation: The best time to buy the stock will be on Day 2 when the price of the stock will be -57093.

Example2:

Input1: 5
Input2: (-39957, -17136, 1, 2, -26711)
Output: -80801
Explanation: The best time to buy the stock will be on Day 5 when the price of the stock will be -83801.

Example3:

Input1: 9
Input2: (-4527,-1579,-38732,-43669,-9287,-48068,-30293,-30867,18677}
Output: -207022
Explanation: The best time to buy the stock will be on Day 8 when the price of the stock will be -207022.

C++ PROGRAM:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int N, ans, price;
    cin>>N;
   
    int arr[N];
    
    for (int i = 0; i < N; i++)
        cin>>arr[i];
    
    price = 0;
    ans = 0;
    for (int i = 0; i < N; i++)
    {
        price = price + arr[i];
        if (ans > price)
            {
                ans = price;
            }
    }
    cout << ans;
}

C PROGRAM:

#include<stdio.h>
int main()
{
    int N, ans, price;
    scanf("%d", &N);
   
    int arr[N];
    
    for (int i = 0; i < N; i++)
        scanf("%d", &arr[i]);
    
    price = 0;
    ans = 0;
    for (int i = 0; i < N; i++)
    {
        price = price + arr[i];
        if (ans > price)
            {
                ans = price;
            }
    }
    printf("%d", ans);
}

PYTHON PROGRAM

n = int(input())
arr = []
for i in range(n):
   arr.append(int(input()))
min = 0
for i in range(1, n):
   if sum(arr[:i]) < min:
       min = sum(arr[:i])
print(min)

Question) Rohan and his team are participating in the Treasure Hunt event of college in which in each step they have to solve one problem to get a clue about the Treasure location. Rohan’s team has performed very well and reached the final step where they have to provide a code of a problem to get a final clue about treasure .

Given a string s, they need to find the longest palindromic subsequence’s length in s.
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

The string contains only lowercase letters.

Write a program to help Rohan’s team that takes in input as String x and returns the length of the longest palindromic subsequence of x.

Input Specification:

input1: string input

Output Specification:

Return the length of the longest palindromic subsequence

Example 1:

Input: s = “bbbab”
Output: 4
Explanation: One possible longest palindromic subsequence is “bbbb”.

Example 2:

Input: s = “cbbd”
Output: 2
Explanation: One possible longest palindromic subsequence is “bb”.

C++ PROGRAM

#include<iostream>
#include<cstring>
using namespace std;

int max (int x, int y) 
{
    return (x > y)? x : y; 
}

int lps(char *str)
{
    int n = strlen(str);
    int i, j, cl;
    int L[n][n]; 

    for (i = 0; i < n; i++)
	    L[i][i] = 1;

    for (cl = 2; cl <= n; cl++)
	{
		for (i = 0; i < n-cl+1; i++)
		{
		    j = i + cl-1;
		    if (str[i] == str[j] && cl == 2)
		        L[i][j] = 2;
		    else if (str[i] == str[j])
		        L[i][j] = L[i+1][j-1] + 2;
		    else L[i][j] = max(L[i][j-1], L[i+1][j]);
		}
	}
	return L[0][n-1];
}

int main()
{
    char seq[20];
    cin >> seq;
	int n = strlen(seq);
	printf("%d", lps(seq));
	getchar();
	return 0;
}

JAVA PROGRAM

import java.util.*;
public class Main
{
	static int max (int x, int y) 
	{
	    return (x > y)? x : y; 
	    
	}

	static int lps(String seq)
	{
	    int n = seq.length();
	    int i, j, cl;
    	    int L[][] = new int[n][n];
	
    	    for (i = 0; i < n; i++)
	    	L[i][i] = 1;
		
		for (cl = 2; cl <= n; cl++)
		{
			for (i = 0; i < n-cl+1; i++)
			{
				j = i+cl-1;
				if (seq.charAt(i) == seq.charAt(j) && cl == 2)
    				L[i][j] = 2;
				else if (seq.charAt(i) == seq.charAt(j))
	    			L[i][j] = L[i+1][j-1] + 2;
				else
		    		L[i][j] = max(L[i][j-1], L[i+1][j]);
			}
		}
		return L[0][n-1];
	}
	
	public static void main(String args[])
	{
	        Scanner sc = new Scanner(System.in);
		String seq = sc.next();
		int n = seq.length();
		System.out.println(""+ lps(seq));
	}
}

PYTHON PROGRAM

s = input()
n = len(s)
rev = s[::-1]
dp = [[0 for _ in range(n+1)] for _ in range(n+1)]

for i in range(1, n+1):
    for j in range(1, n+1):
        if s[i-1] == rev[j-1]:
            dp[i][j] = 1 + dp[i-1][j-1]
        else:
            dp[i][j] = max(dp[i-1][j], dp[i][j-1])

print(dp[n][n])

Question) Given a number n, the task is to find the remainder when n is divided by 11. The input number may be very large.

Since the given number can be very large, you can not use n % 11.

Input Specification:

inputs a large number in the form of a string

Output Specification:

Return the remainder modulo 11 of input1

Example1:

Input: str = 13589234356546756
Output: 6

Example2:

Input: str = 3435346456547566345436457867978
Output: 4

Example3:

input: str = 121
Output: 0

JAVA PROGRAM

import java.util.*;
import java.io.*;
public class Main
{
	static int remainder(String str)
	{
		int len = str.length();
		int num, rem = 0;
		
		for (int i = 0; i < len; i++)
		{
			num = rem * 10 + (str.charAt(i) - '0');
			rem = num % 11;
		}
		return rem;
	}
	
	public static void main(String args[])
	{
	    Scanner sc = new Scanner(System.in);
		String str = sc.next();
		System.out.println(remainder(str));
	}
}

PYTHON PROGRAM

s, rem = input(), 0

for i in s:
    rem = (rem * 10 + int(i)) % 11

print(rem)

Question) Williamson is an analyst he needs to analyse a particular topic for performing analysis for that he needs to find a permutation of a given object.He don’t know how to find permutation so for simplifying his work he is hiring one software developer who can code for him and find a permutation and combination of a given object.

Consider you are giving an interview to williamson for working with him. Find a permutation of given input to proof that you are fit for his requirement.

Input Specification:
nCr where n and r are numbers given by Williamson to you
nCr is defined as n! / (r! x (n-r)!)

Here, n! denotes the factorial of a number. Also, you have to calculate this number as modulo
Input Specification:

  • input1: The number n.
  • Input2: The number r.
  • Input3: The number m,
    .

Output specification:

  • The value of nCr%m.

Example 1:
Input1: 3
Input2: 2
Input3: 10000000009

Output:3

Explanation:
n=3.r=2,m=100 So, n!=3!=6, r!=2!=2, (n-1)!= 1!=1.
So, nCr = (6/(2*1))%1000000009= 3.

Example 2:
input1: 5
input2: 2
input3: 1000000009

Output: 10

Explanation:
n=5,r=2, m=100 So, n!=5!=120, r!=2=2, (n-1)!= 3!=6.
So, nCr = (120/(2*6))%1000000009= 10.

JAVA PROGRAM

import java.util.*;
public class Main
{
    static int fact (int number)
    {
        int f = 1;
        int j = 1;

        while (j <= number)
        {
            f = f * j;
            j++;
        }
        return f;
    }
    
    public static void main (String args[])
    {
        Scanner sc = new Scanner (System.in);
        int n = sc.nextInt ();
        int r = sc.nextInt ();
        long m = sc.nextLong ();
        int result = fact (n) / (fact (r) * fact (n - r));
        long finalresult = (long) result % m;
        System.out.println ("" + result);
    } 
}

PYTHON PROGRAM

import math
n=int(input())
r=int(input())
m=int(input())
#nCr= (n!)/((n-r)!)*(r!)
numerator=(math.factorial(n))
denominator=(math.factorial(n-r))*math.factorial(r)
ncr=numerator//denominator
print(ncr%m)

Question) A Derangement is a permutation of n elements, such that no element appears in its original position.
For example, a derangement of {0, 1, 2, 3} is {2, 3, 1, 0}.
Given a number n, find the total number of Derangements of a set of n elements.

Input Specification:
input1: N, the number of Objects

Output Specification:
Return the number of arrangements in which no object occurs at its original
position

Example 1:

Input: n = 2
Output: 1
For two elements say {0, 1}, there is only one possible derangement {1, 0}

Example 2:

Input: n = 3
Output: 2
For three elements say {0, 1, 2}, there are two possible derangements {2, 0, 1} and {1, 2, 0}

Example 3:

Input: n = 4
Output: 9
For four elements say {0, 1, 2, 3}, there are 9 possible derangements {1, 0, 3, 2} {1, 2, 3, 0} {1, 3, 0, 2}, {2, 3, 0, 1}, {2, 0, 3, 1}, {2, 3,1, 0}, {3, 0, 1, 2}, {3, 2, 0, 1} and {3, 2, 1, 0

C++ PROGRAM

#include <iostream>
using namespace std;

int countDer(int n)
{

	// base case
	if (n == 1 or n == 2) {
		return n - 1;
	}

	int a = 0;
	int b = 1;

	// using above recursive formula
	for (int i = 3; i <= n; ++i) {
		int cur = (i - 1) * (a + b);
		a = b;
		b = cur;
	}

		return b;
}

// Driver Code
int main()
{
	int n;
	cin>>n;
	cout  << countDer(n);
	return 0;
}

JAVA PROGRAM

import java.util.*;
import java.io.*;
public class Main {

    static int countDer(int n) {
        
        if(n == 1 || n == 2) {
            return n-1;
        }
    
    
        int a = 0;
        int b = 1;
    
        
        for (int i = 3; i <= n; ++i) {
            int cur = (i-1)*(a+b);
            a = b;
            b = cur;
        }
            
    
        return b;
    }
    
    public static void main (String[] args)
    {
        Scanner sc  = new Scanner(System.in);
        int n = sc.nextInt();
        System.out.println(""+countDer(n));
    
    }
}

Question) After Watching a movie at PVR, Adil is pondering over the number of ways in which he can pay for the movie. He has x1, x2, x3, x4 coins of values 1,2,5 and 10 respectively. He wants to determine the number of ways in which he can pay an amount A.

You need to fill in a function that returns the number of ways to pay total amount

Input Specifications:

Input 1: An integer value denoting the total amount to be paid

Output Specification:
Return an Integer value denoting the number of ways to pay the total amount

Example1:
Input1: 40
Output : 195

Example2:
Input1: 4
Output : 3

C++ PROGRAM

#include <iostream>
#include <climits>
using namespace std;

int coinsExchange(int amt, int *deno, int n, int *dp) {
	// base case
	if (amt == 0) {
		return dp[amt] = 0;
	}
	if (dp[amt] != -1) {
		return dp[amt];
	}

	// recursive case
	int ans = INT_MAX;
	for (int i = 0 ; i < n ; i++) {
		if (amt >= deno[i]) {
			int sa = coinsExchange(amt - deno[i], deno, n, dp);
			if (sa != INT_MAX) {
				ans = min(sa + 1, ans);
			}
		}
	}

	return dp[amt] = ans;
}

int main() {

	int deno[4] = {1, 2, 5, 10};
	int amt;
	cin>>amt;
	int *dp = new int[amt + 1];
	for (int i = 0 ; i <= amt ; i++) {
		dp[i] = -1;
	}
	cout << coinsExchange(amt, deno, 4, dp) << endl;

	return 0;
}

JAVA PROGRAM

import java.io.*;
import java.util.*;
public class Coin {
    
    static int count( int S[], int m, int n )
    {
        
        if (n == 0)
            return 1;
        
        if (n < 0)
            return 0;
      
        if (m <=0 && n >= 1)
            return 0;
    
        return count( S, m - 1, n ) +
               count( S, m, n-S[m-1] );
    }
    
    public static void main(String[] args)
    {   Scanner sc = new Scanner(System.in);
        int total  = sc.nextInt();
        int arr[] = {1, 2, 5,10};
        int m = arr.length;
        System.out.println( count(arr, m, total));
    }
}

***BEST OFF LUCK***

Cracking upcoming exams is not that difficult if you work hard. Naukrimessenger.com will help you to do so. Join our Telegram channel page now with the direct link provided on our homepage to get guidance and stay on the right track of preparation. You will also get other features that will help you. To avail of exclusive job offers, join our Telegram Channel page now!