Infosys Main
Infosys Main
PLACEMENT
MATERIALS
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
About
Welcome to the ultimate resource for Infosys placement preparation! This
comprehensive guide is meticulously crafted to help you excel in every aspect of the
UF
Infosys recruitment process. Whether you're gearing up for logical reasoning, honing
your coding skills, or brushing up on technical questions, we've got you covered.
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Content
Coding Questions
Practices
UF
Interview Experiences
Mock tests
O
KN
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Pattern
UF
No. of
Infosys Test
questions in Time Difficulty
Pattern
test
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Important Topic
Logical
UF
Arrangements
Puzzles
Syllogisms
Coding-Decoding
O
Number series
Cryptarithmetic
Data Sufficiency
Logical Deduction
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Verbal
Reading Comprehension
Sentence Correction
UF
Sentence selection
Sentence Completion
Para Jumbles
Spotting error
Analogy
O
Fill in the Blanks
Critical Reasoning
KN
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Aptitude
Percentages
Data Interpretation
UF
Permutation and Combination
Probability
Problem on Ages
Divisibility
KN
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Codings
UF
c) Stack
d) Queue
e) Tree
f) Graph
g) Hash Map
h) Linked List
Medium: That can be solved by applying the basics of the Greed algorithm.
a) Pure Greedy Algorithms.
O
b) Orthogonal Greedy Algorithms.
c) Relaxed Greedy Algorithms.
g) Dijkstra's algorithm
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
Input:-
Asp5w8w@k7!l23mn69
UF
Output:-
8527639
Input:-
O
#bn7856!@kn2n65jbnj482375
Output:-
7856523674582
KN
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
n=input()
a=[]
b=[]
c=[]
e=0
f=0
k=1
UF
l=0
for i in n:
if(i.isalnum()):
f=1
else: else:
e=e+1 if(len(b)>=len(a)):
for i in n: for i in a:
if(i.isdigit()): b.insert(k,i)
O
if(int(i)%2==0): k=k+2
a.append(i) else:
else: l=len(b)
b.append(i) for i in range(l):
KN
if(e%2==0): b.insert(k,a[i])
if(len(a)>=len(b)): k=k+2
for i in b: k=len(b)
a.insert(k,i) for i in range(l,len(a)):
k=k+2 b.insert(k,a[i])
else: k=k+1
l=len(a) c=b
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
Input:- 93012630
Output:- 2,6,12,30,930,
UF
We should divide the total number into substrings and we should verify
each num is pronic num or not if pronic we should print that num
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
a=int(input())
b=str(a)
n=len(b)
c=[]
k=0
l=0
for i in range(n):
UF
k=0
l=i
if(i==0):
for j in range(n):
c.append(int(b[j]))
else:
for j in range(n-i):
c.append(int(b[k:l+1]))
k=k+1
O
l=l+1
d=[]
for i in c:
if(i not in d):
KN
d.append(i)
d=sorted(d)
for i in d:
for j in range(1,150):
if(j*(j+1)==i):
print(i,end=',')
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
You are given an array of size N. You need to change this array into a
mountain. By mountain we mean, the either ends of the array should have
UF
equal elements. Then as we move towards the middle from both ends, the
next element is just one more than the previous one. So, it would have a
peak in the middle and decrease if you go towards either end, just like a
mountain.
array [1, 2, 3, 1] is also not a mountain because the elements 2 and 3 are
You need to find the minimum number of elements that should be changed
to make the array a mountain. You can make the elements negative or
zero as well.
Input Format
TE
N :: INTEGER
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Sample Input 1:
UF
4
Sample Output 1 :
Sample input 2:
O
9
1
1
1
KN
2
3
2
1
1
1
Sample Output 2:
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
#include <iostream>
UF
if(arr[m1] == arr[m2]){
for(int i =m1-1; i>=0; i--){
if(arr[i] != (arr[i+1] -1)){
count++;
arr[i] = (arr[i+1]-1);
}
}
}
O
else if(arr[m1] != arr[m2]){
arr[m2] = arr[m1];
for(int i =m1-1; i >= 0; i--){
if(arr[i] != (arr[i+1] -1)){
KN
count++;
arr[i] = (arr[i+1]-1);
} int mid = n/2;
} for(int i = mid-1; i >= 0; i--){
if(arr[i] =! (arr[i+1]-1)){
} count++;
int j = m1-1; arr[i] = (arr[i+1]-1);
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Java
import java.util.Scanner;
class Main{
public static int mountain(int[] arr, int n){
int count=0;
if(n%2==0){
int m2 = n/2;
UF
int m1 = m2-1;
if(arr[m1] == arr[m2]){
for(int i =m1-1; i>=0; i--){
if(arr[i] != (arr[i+1] -1)){ int j = mid-1;
count++; for(int i = mid+1;i < n; i++){
arr[i] = (arr[i+1]-1); if(arr[i] !=arr[j]){
} count++;
} }
O
} j--;
else if(arr[m1] != arr[m2]){ }
arr[m2] = arr[m1]; return count;
for(int i =m1-1; i>=0; i--){ }
KN
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Sample Input 1
989898
Sample Output 1
UF
989898
Explanation:
Xor_sum(0)=(0^989898)=989898
Sample Input 2
7
O
1
6
KN
Sample Output 2
14
Explanation
Xor_sum(4)=(4^1)+(4^6)+(4^3)=14.
TE
Sample Input 3
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
You have an interesting string S of length N. It is interesting because you
can rearrange the characters of this string in any order. You want to cut
UF
this string into some contiguous pieces such that after cutting, all the
You can’t rearrange the characters in the cut pieces or join the pieces
Input Format
KN
S :: STRING
Sample Input 1:
TE
zzzzz
Sample Output 1 :
Sample input 2:
ababcc
Sample Output 2:
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Sample input 2:
abccdcabacda
Sample Output 2:
UF
#include<iostream>
#include<unordered_map>
#include<string>
#include<math.h>
using namespace std;
O
int ans(string s){
unordered_map map;
for(int i =0; i map[s[i]]){
val = map[s[i]];
KN
key = map[s[i]];
//we will get the smallest number.
}
}
return val;
}
TE
int main()
{
string s;
cin>>s;
cout<< ans(s);
return 0;
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Java
import java.util.*;
class Main {
public static void main(String[] args) {
String s;
Scanner sc = new Scanner(System.in);
UF
s = sc.next();
System.out.print(ans(s));
}
static int ans(String s) {
HashMap map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
if (!map.containsKey(s.charAt(i))) {
map.put(s.charAt(i), 1);
} else {
O
map.put(s.charAt(i),map.getOrDefault(s.charAt(i), 1)+1);
}
}
int key = 100000;
KN
int val = 0;
for (int i = 0; i < s.length(); i++) {
if (key > map.get(s.charAt(i))) {
val = map.get(s.charAt(i));
key = map.get(s.charAt(i));
// we will get the smallest number.
}
TE
}
return val;
}
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
Today you decided to go to the gym. You currently have energy equal to E
units. There are N exercises in the gym. Each of these exercises drains Ai
UF
amount of energy from your body.
You feel tired if your energy reaches 0 or below. Calculate the minimum
number of exercises you have to perform such that you become tired.
Input Format
E :: INTEGER
KN
E :: 1 -> 10^5
N :: INTEGER
:: 1 -> 10^5
A :: INTEGER ARRAY
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Sample Input 1:
6
2
1
2
Sample Output 1 :
4
UF
Sample input 2:
10
2
1
2
Sample Output 2:
-1
O
Sample input 3:
2
3
1
KN
5
2
Sample Output 3:
1
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
#include <iostream>
UF
if(arr[i] == energy){
return 1;
}
}
int sum =0;
int ans =0;
int ans1=0;
for(int i=0; i<test; i++){ sum = sum + arr[i]; if(sum >=energy){
ans = i+1;
O
return ans;
}
}
sum =0;
KN
for(int i =0; i<test; i++){ sum = sum + arr[i]; if(2*sum >= energy){
ans1 = 2*(i+1);
return ans1;
}
}
return -1;
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
int main()
{
int energy ;
cin>>energy;
int test;
cin>>test;
int arr[test];
UF
for(int i =0; i<test; i++){ cin>> arr[i];
}
}
O
KN
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Java
import java.util.Scanner;
public class Main {
public static int solution(int nExcercise,int[] arr,int energy){
int sum = 0;
for(int i = 0; i< nExcercise; i++) {
if(arr[i]==energy)
UF
return 1;
}
for(int i =0; i= energy){
return i+1;
}
if(2*sum >= energy){
return 2*(i+1);
}
}
O
//if sum is not equal to the energy.
return -1;
}
KN
arr[i] = s.nextInt();
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
There is a battle between heroes and villains going on. You have M heroes,
all of them have the same health H. There are N villains, health of the i-th
UF
villain is Vi.
When a hero, with health H battles a villain with health Vi, one of the three
if H > Vi: The villain is defeated, and the health of the hero is decreased by
Vi if H < Vi: The villain wins, his health is not affected, and the hero is no
O
longer able to fight. if H = Vi: Both are considered defeated, and neither
can fight.
The heroes start fighting villains one by one in the same order, first villain
KN
1 then villain 2 and so on. It might be possible that before defeating all the
villains, all the heroes are defeated. Therefore, to ensure the victory of the
Your task is to find the minimum number of villains you need to remove
TE
from the front such that the victory of the heroes is guaranteed.
Note: If in the last battle, both the hero and villain are defeated and no
Input Format
N :: INTEGER
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
UF
array :: INTEGER ARRAY
Each line i of the N subsequent lines (where 0 ≤ i < N) contains an
integer describing the health of each of the villains.
array[i] :: 1 -> 10^9
Sample Input 1:
Sample Output 2:
4
0
4
O
3
Sample input 3:
3
5
1
1
3
4
KN
3
1
2
Sample Output 1 :
3
0
1
3
Sample input 2:
5
Sample Output 3:
TE
3
3
3
1
2
3
1
1
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
#include <iostream>
UF
return i;
}
}
}
O
Implement the main function
KN
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Java
import java.util.Scanner;
class Main{
public static int ans(int[] arr, int n, int m, int h){
int total = 0;
for(int i =0; i0; i--){
UF
sumh= sumh+arr[i];
if(sumh >= totalhero){
return i;
}
}
}
return 0;
O
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
Khaled has an array A of N elements. It is guaranteed that N is even. He
UF
choose consecutive elements. Khaled is interested in XOR of all the
elements he chooses.
For example:
If A=[2,4,6,8], then Khaled can choose the subset [2,4,8] to achieve XOR=
O
(2 XOR 4 XOR 8)=14.
Khaled wants to maximize the XOR of all the elements he chooses. Your
task is to help Khaled to find the max XOR of a subset that he can achieve
KN
Input format:
describing Ai.
Constraints
1<=N<=120
1<=A[i]<=10^6
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Sample Input 1
Sample Output 1
UF
2
Explanation:
N=2, A=[1,2] Khaled can choose the subset[2]. The xor of the elements in
1
KN
Sample Output 2
TE
Explanation:
N=4, A=[1,2,4,7] Khaled can choose the subset [7]. The xor of the elements
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Python
UF
max_xor = max(arr)
for i in range(1, N // 2):
comb = combinations(arr, i + 1)
for j in comb:
sub.append(list(j))
for a in sub:
z=0
for b in a:
z=z^b
O
if z > max_xor:
max_xor = z
return max_xor
KN
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
UF
int max_xor = arr.get(0);
for (int num : arr) {
if (num > max_xor) {
max_xor = num;
}
}
for (int b : a) {
z = z ^ b;
}
if (z > max_xor) {
max_xor = z;
}
}
TE
return max_xor;
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
UF
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Length: ");
int N = scanner.nextInt();
List<Integer> arr = new ArrayList<>();
scanner.close();
}
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
Wael is well-known for how much he loves the bitwise XOR operation,
while kaito is well known for how much he loves to sum numbers, so their
UF
friend Resli decided to make up a problem that would enjoy both of them.
Can you find the integer x in the range [0,K] with the maximum Xor-sum (x)
O
value?
Input format
KN
describing Ai.
TE
Constraints
1<=N<=10^5
0<=K<=10^9
0<=A[i]<=10^9
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Sample Output 3
46
Explanation:
Xor_sum(8)=(8^7)+(8^4) +(8^0)+(8^3)=46.
Python
if s > xorSum:
xorSum = s
return xorSum
n=4
x=9
TE
arr = [7, 4, 0, 3]
print(Xor_sum(x, arr))
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Java
UF
for (int num : arr) {
xorSum += num;
}
return xorSum;
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
One of the first lessons IT students learn is the representation of natural
numbers in the binary number system (base 2) This system uses only two
UF
digits, 0 and 1. In everyday life we use for convenience the decimal
system (base 10) which uses ten digits, from 0 to 9. In general, we could
quotient (as first digit) and is followed by the remainder of the previous
divisions For example : If M=122 and K=8, 122 in base 10= 172 in base 8
In some cases in the new representation all the digits of the number are
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Input Format
UF
Constraints
1 <= M = 10^12
Sample Input 1 :
41
Sample Output 1 :
O
40
Explanation :
KN
Here 41 in base 40. will be 11 so it has all digits the same, and there is no
Sample Input 2 :
34430
Sample Output 2 :
TE
312
Explanation :
Here 34430 in base 312 will have all digits the same and there is no
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Python
UF
m = m // base
if m == rem:
return True
return False
m = int(input())
base = 2
while not convertBase(m, base):
base = base + 1
O
print(base)
KN
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Java
import java.util.Scanner;
UF
public static boolean convertBase(int m, int base) {
int rem = m % base;
m = m / base;
while (m >= base && (m % base == rem)) {
m = m / base;
}
return m == rem;
}
O
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int m = scanner.nextInt();
KN
int base = 2;
while (!convertBase(m, base)) {
base++;
}
System.out.println(base);
scanner.close();
}
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
Andy wants to go on a vacation to de-stress himself. Therefore he decides
UF
days as possible to rest, but he can only make one trip to the island.
Suppose that the days are numbered from 1 to N. Andy has M obligations
some specific days. This means that ith obligation is scheduled for day Di.
holidays.
O
Your task is to find out the maximum days of vacation Andy can take by
Input Format
The first line contains an integer N, denoting the total number of days
obligations.
describing Di.
Constraint
1<=N<=10^6 1<=K<=2*10^6
1<=M<=2*10^6 1<=D[i]<=10^6
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Sample Input 1:
10
UF
9
Sample Output 1 :
5
O
Explanation:
Here he could cancel his 3rd and 4th obligation which makes vacation
KN
length 5.
Sample input 2:
7
2
0
3
TE
4
Sample Output 2:
3
Explanation:
Here he could not cancel any obligation since K=0, so the vacation length
is 3.
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Python
n = int(input())
m = int(input())
k = int(input())
arr = [0] * n
UF
for i in range(m):
arr[i] = int(input())
ans = 0
arr.sort()
if k > 0:
for i in range(k + 1, m + 3, 1):
ans = max(ans, arr[i] - arr[i - k - 1] - 1)
else:
j=0
O
while arr[j] == 0:
j=j+1
count = 0
for i in range(1, n + 1, 1):
KN
count += 1
if j < n and (i == arr[j]):
count = 0
j += 1
ans = max(count, ans)
print(ans)
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Java
import java.util.Arrays;
import java.util.Scanner;
UF
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int k = scanner.nextInt();
arr[i] = scanner.nextInt();
}
int ans = 0;
Arrays.sort(arr);
TE
if (k > 0) {
for (int i = k + 1; i < m + 3; i++) {
ans = Math.max(ans, arr[i] - arr[i - k - 1] - 1);
}
} else {
int j = 0;
while (j < n && arr[j] == 0) {
j++;
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
int count = 0;
for (int i = 1; i <= n; i++) {
count++;
if (j < n && (i == arr[j])) {
count = 0;
j++;
UF
}
ans = Math.max(count, ans);
}
}
System.out.println(ans);
scanner.close();
}
}
O
KN
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
You need to build a road in a rugged terrain. You know the sea level of
each segment of the rugged terrain, i.e., the i-th segment is Li meters from
UF
sea level.
You need to transform the terrain into a strictly downward sloping terrain
for the road, i.e., for each i-th segment where 2 <= i <= N, resultant Li-1 >
Li. To do so, you employ a powerful digging team to help you dig and
reduce the sea level of the segments. On day D, the team can reduce the
sea level for each segment that you scheduled that day by 2D-1 meters
O
each.
You are allowed to assign the team to dig on multiple segments and/or dig
KN
Your task is to find the minimum number of days needed to transform the
Input Format
TE
N :: INTEGER
N :: 1 -> 10^5
L :: INTEGER ARRAY
describing Li, the sea level of the i-th segment. L[i] :: -10^9 -> 10^9
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Sample Input 1:
2
3
3
Sample Output 1 :
1
Sample input 2:
UF
2
5
-3
Sample Output 2:
0
O
KN
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Python
import math
def main():
# Read input
UF
n = int(input())
a = [int(input()) for _ in range(n)]
max_dig = 0
for i in range(n - 1):
if a[i] <= a[i + 1]:
max_dig = max(max_dig, (a[i + 1] - a[i] + 1))
a[i + 1] = a[i] - 1
O
ans = math.ceil(math.sqrt(max_dig))
print(ans)
if __name__ == "__main__":
KN
main()
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Java
import java.util.Scanner;
import java.util.Arrays;
UF
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
O
long max_dig = 0;
for (int i = 0; i < n - 1; i++) {
if (a[i] <= a[i + 1]) {
KN
scanner.close();
}
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
You are given an array of size N. You need to change this array into a
mountain. By mountain we mean, the either ends of the array should have
UF
equal elements. Then as we move towards the middle from both ends, the
next element is just one more than the previous one. So, it would have a
peak in the middle and decrease if you go towards either end, just like a
mountain.
You need to find the minimum number of elements that should be changed
to make the array a mountain. You can make the elements negative or
zero as well.
Input Format
N :: INTEGER
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Sample Input 1:
UF
4
Sample Output 1 :
Sample input 2:
9
O
1
1
KN
1
TE
Sample Output 2:
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Python
UF
for i in range(m1 - 1, -1, -1):
if arr[i] != (arr[i + 1] - 1):
count += 1
arr[i] = (arr[i + 1] - 1)
else:
arr[m2] = arr[m1]
for i in range(m1 - 1, -1, -1):
if arr[i] != (arr[i + 1] - 1):
count += 1
O
arr[i] = (arr[i + 1] - 1)
j = m1 - 1
for i in range(m2 + 1, n):
KN
if arr[i] != arr[j]:
count += 1
j -= 1
else:
mid = n // 2
for i in range(mid - 1, -1, -1):
if arr[i] != (arr[i + 1] - 1):
TE
count += 1
arr[i] = (arr[i + 1] - 1)
j = mid - 1
for i in range(mid + 1, n):
if arr[i] != arr[j]:
count += 1
j -= 1
return count
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
def main():
n = int(input("Enter the number of elements: "))
arr = [int(input()) for _ in range(n)]
print(mountain(arr, n))
if __name__ == "__main__":
UF
main()
O
KN
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
import java.util.Scanner;
if (n % 2 == 0) {
int m2 = n / 2;
int m1 = m2 - 1;
UF
if (arr[m1] == arr[m2]) {
for (int i = m1 - 1; i >= 0; i--) {
if (arr[i] != (arr[i + 1] - 1)) {
count++;
arr[i] = arr[i + 1] - 1;
}
}
} else {
O
arr[m2] = arr[m1];
for (int i = m1 - 1; i >= 0; i--) {
if (arr[i] != (arr[i + 1] - 1)) {
count++;
KN
arr[i] = arr[i + 1] - 1;
}
}
}
int j = m1 - 1;
for (int i = m2 + 1; i < n; i++) {
if (arr[i] != arr[j]) {
TE
count++;
}
j--;
}
} else {
int mid = n / 2;
for (int i = mid - 1; i >= 0; i--) {
if (arr[i] != (arr[i + 1] - 1)) {
count++;
arr[i] = arr[i + 1] - 1;
}
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
int j = mid - 1;
for (int i = mid + 1; i < n; i++) {
if (arr[i] != arr[j]) {
count++;
}
j--;
}
}
UF
return count;
}
arr[i] = scanner.nextInt();
}
scanner.close();
TE
}
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Coding Questions
You have been given a string S of length N. The given string is a binary
string which consists of only 0’s and ‘1’s. Ugliness of a string is defined as
UF
the decimal number that this binary string represents.
Example:
“101” represents 5.
“0000” represents 0.
There are two types of operations that can be performed on the given
O
string.
Swap any two characters by paying a cost of A coins. Flip any character
KN
Initially, you have been given coins equal to the value defined in CASH.
Your task is to minimize the ugliness of the string by performing the above
mentioned operations on it. Since the answer can be very large, return the
TE
Note:
You can perform an operation only if you have enough number of coins to
perform it.
After every operation the number of coins get deducted by the cost for
that operation.
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Input Format
the string
The next line contains a string, S, denoting the the binary string
The next line contains an integer, CASH, denoting the total number of
UF
coins present initially
characters.
Then the next line contains an integer, B, denoting the cost to flip a
character.
Constraints
O
1 <= N <= 10^5
1<=CASH <=10^5
1<=A<=10^5
1<=B<=10^5
Sample Input 1 :
4
TE
1111
7
1
2
Sample Output 1 :
1
Explanation:
3 flips can be used to create “0001” which represents 1.
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Sample Input 2:
111011
UF
3
Sample Output 2:
Explanation:
First swap 0 with the most significant 1, then use flip twice first on index
Sample Input 3:
KN
111011
2
TE
Sample Output 3:
Explanation:
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
import java.util.Scanner;
static String s;
static int n, cash, a, b;
UF
static void swapf() {
int i = 0;
while (i < s.length() && s.charAt(i) != '1') {
i++;
}
int j = s.length() - 1;
while (j > i && cash >= a) {
O
if (s.charAt(j) == '0') {
if (s.charAt(i) == '0') {
i++;
} else {
KN
}
} else {
j--;
}
}
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
UF
s = sb.toString();
cash -= b;
}
i++;
}
}
b = scanner.nextInt();
if (a < b) {
swapf();
flipf();
} else {
flipf();
TE
swapf();
}
scanner.close();
}
}
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Python
def flipf():
global s, cash, b
i=0
s = list(s)
UF
while i < len(s) and cash >= b:
if s[i] == '1':
s[i] = '0'
cash -= b
i += 1
s = ''.join(s)
def swapf():
global s, cash, a
O
i=0
while i < len(s) and s[i] != '1':
i += 1
KN
j = len(s) - 1
while j > i and cash >= a:
if s[j] == '0':
if s[i] == '0':
i += 1
else:
s = list(s)
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
def main():
global s, cash, a, b
n = int(input("Enter the number of elements: "))
s = input("Enter the binary string: ")
cash = int(input("Enter the cash amount: "))
a = int(input("Enter the cost of swap: "))
b = int(input("Enter the cost of flip: "))
UF
if a < b:
swapf()
flipf()
else:
flipf()
swapf()
O
result = int(s, 2)
print(result)
if __name__ == "__main__":
KN
s = ""
cash = 0
a=0
b=0
main()
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Practice
UF
Quants, Verbal & Logical O
KN
Coding Questions
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
Interview Experience
UF
O
KN
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
MOCK TEST
UF
O
KN
TE
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf
UBK ANNA
UF
O
69 X 6 = 414/- PAY ONLY
KN
249/-
TO GET DM - 👆🏽 👇🏽
TE
INSTAGRAM.COM/TEKNO.UF
teknouf
youtube.com/@teknouf
instagram.com/tekno.uf