0% found this document useful (0 votes)
12 views7 pages

My Selfpractice ARRAY

The document outlines a self-practice training for Software Development Engineers (SDE) focusing on arrays and functions. It includes seven coding problems, each with sample inputs and outputs, covering topics such as counting occurrences, detecting duplicates, rotating arrays, and identifying progressions. Each problem is designed to enhance coding skills and problem-solving abilities in a competitive programming context.

Uploaded by

2k22cse081
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

My Selfpractice ARRAY

The document outlines a self-practice training for Software Development Engineers (SDE) focusing on arrays and functions. It includes seven coding problems, each with sample inputs and outputs, covering topics such as counting occurrences, detecting duplicates, rotating arrays, and identifying progressions. Each problem is designed to enhance coding skills and problem-solving abilities in a competitive programming context.

Uploaded by

2k22cse081
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

SELF PRACTICE

SDE Readiness Training

Practice No. : 3a

Topic : Arrays and Function

Date : 14.02.2025

Solve the following problems

Q.
Question Detail Level
No.
1 You're working on a program that tracks student grades, and Easy
you need to find out how many times a specific grade, say 90,
appears in an array of test scores (frequency).
Sample Input:
arr[] = {85, 90, 78, 90, 92, 90, 87, 88, 90}
Sample Output:
The grade 90 appears 4 times in the array

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.println("Enter the no of elements:");
int n = s.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements:");
for(int i=0;i<n;i++) {
arr[i]=s.nextInt();
}
int repeat = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(arr[i]==arr[j]) {
repeat = arr[i];
}
}
}
int count =0;
for(int i=0;i<n;i++) {
if(arr[i]==repeat) {
count++;
}
}
System.out.println("The grade "+repeat+" appears
"+count+" times in the array");
}

OUTPUT:
Enter the no of elements:
9
Enter elements:
85 90 78 90 92 90 87 88 90
The grade 90 appears 4 times in the array

Sometimes later becomes never. DO IT NOW!


1
SELF PRACTICE
SDE Readiness Training

2 You're participating in a coding competition where the Easy


challenge is to identify duplicate elements in an array. How
would you devise a strategy to detect and remove these
duplicates?
Sample Input: [5,8,2,5,9,2,3,8]
Sample Output:[5,8,2,9,3]

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.println("Enter the no of elements:");
int n = s.nextInt();
int[] arr = new int[n];
int[] b = new int[n];
System.out.println("Enter elements:");
for(int i=0;i<n;i++) {
arr[i]=s.nextInt();
}
int flag =0;
for(int i=0;i<n;i++) {
flag =0;
for(int j=0;j<i;j++) {
if(arr[i]==arr[j]) {
flag = 1;
break;
}
}
if(flag==0) {
System.out.print(arr[i]+" ");
}
}
}

OUTPUT:
Enter the no of elements:
8
Enter elements:
5 8 2 5 9 2 3 8
5 8 2 9 3

3 You're designing a thrilling game where players embark on an Easy


adventure through an array of challenges. One of the key
mechanics involves rotating the elements of an array by a
certain number of steps.
Sample Input: [1,2,3,4,5,6,7]
Rotate : 3
Sample Output: [4,5,6,7,1,2,3]

public static void main(String[] args) {


Scanner s = new Scanner(System.in);

Sometimes later becomes never. DO IT NOW!


2
SELF PRACTICE
SDE Readiness Training
System.out.println("Enter the no of elements:");
int n = s.nextInt();
int[] a = new int[n];
System.out.println("Enter the elements:");
for(int i=0;i<n;i++) {
a[i]=s.nextInt();
}
System.out.println("Enter the Rotate:");
int k = s.nextInt();
for(int i=k;i<n;i++){
System.out.print(a[i]+" ");
}
for(int i=0;i<k;i++) {
System.out.print(a[i]+" ");
}
}

OUTPUT: Enter the no of elements:


7
Enter the elements:
1 2 3 4 5 6 7
Enter the Rotate:
4
5 6 7 1 2 3 4

4 Take 20 integer inputs from user and print the following: Easy
a) number of positive numbers
b) number of negative numbers
c) number of odd numbers
d) number of even numbers
e) number of 0s.

Sample Input: 1 -3 6 9 8 -13 -5 7 0 12 0 -4 4 0 17 21 6 16 11


19
Sample Output: 13 4 10 10 3

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.println("Enter the no of elements:");
int n = s.nextInt();
int pos=0;
int neg=0;
int odd =0;
int even =0;
int zero =0;
int[] a = new int[n];
for(int i=0;i<n;i++) {
a[i]=s.nextInt();
}
for(int i=0;i<n;i++) {
if(a[i]>0) {
pos++;
Sometimes later becomes never. DO IT NOW!
3
SELF PRACTICE
SDE Readiness Training
}
if(a[i]<0) {
neg++;
}
if((a[i]%2)!=0) {
odd++;
}
if(a[i]%2==0) {
even++;
}
if(a[i]==0) {
zero++;
}
}
System.out.print(pos+" "+neg+" "+odd+" "+even+"
"+zero);
}

OUTPUT:
Enter the no of elements:
20
1 -3 6 9 8 -13 -5 7 0 12 -4 4 0 17 21 6 16 11 19
0
13 4 10 10 3

5 Get the values for an array of size 10. Write the logic to find whether the Easy
array elements are in Arithmetic Progression or Geometric Progression. If
the array is in neither order display ‘Random order’.
Sample Input: 1 4 7 10 13 16 19 22 25 28 Sample
Output: Arithmetic Progression Sample Input: 1 2 4 8 16
32 64 128 256 512
Sample Output: Geometric Progression Sample Input: 2
4 7 11 16 22 29 37 46 56 Sample Output: Random
Order

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.println("Enter the no of elements:");
int n = s.nextInt();
int[] a = new int[n];
int flag1=0;
int flag2=0;
int flag3=0;
for(int i=0;i<n;i++) {
a[i]=s.nextInt();
}

Sometimes later becomes never. DO IT NOW!


4
SELF PRACTICE
SDE Readiness Training
int diff = a[1]-a[0];
double ratio =(double) a[1]/a[0];
for(int i=0;i<n-1;i++) {
if((a[i+1]-a[i])!=diff) {
flag1 =1;
}
if((double)a[i+1]/a[i]!=ratio) {
flag2 =1;
}
else {
flag3 = 1;
}
}
if(flag1==0) {
System.out.println("Arithmetic Progression");
}
else if(flag2==0) {
System.out.println("Geometric progression");
}
else if(flag3==1) {
System.out.println("Random order");
}
}

OUTPUT:
Enter the no of elements:
9
1 4 7 10 13 16 19 22 25
Arithmetic Progression

6 In a lucky draw, XYZ finance company selects two sets of its customers Easy
for a promotion. If the customer’s coupon is in first set, then the customer
gets Rs.10000/- as cash prize. If it is in second set, then the customer
gets tour tickets for two days. Otherwise, customer gets a batch ‘Better
luck next time’. Two sets of coupon numbers and a randomly picked
customer coupon are the inputs. Help the company to say the result.
Note: Consider each set has 10 distinctive customer coupons and no
common coupons.

Sometimes later becomes never. DO IT NOW!


5
SELF PRACTICE
SDE Readiness Training
Sample Input: [2 4 7 11 16 22 29 37 46 56], [ 1 5 9 10 13
18 19 22 25 28], 16
Sample Output: Rs.10000 Cash Prize
Sample Input: [2 4 7 11 16 22 29 37 46 56], [ 1 5 9 10 13
18 19 22 25 28], 13
Sample Output: Tour Tickets for two days

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.println("Enter the no of elements:");
int n = s.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int flag = 0;
System.out.println("Enter the first set:");
for(int i=0;i<n;i++) {
a[i]=s.nextInt();
}
System.out.println("Enter the second set:");
for(int i=0;i<n;i++) {
b[i]=s.nextInt();
}
System.out.println("Enter the coupen:");
int t = s.nextInt();
for(int i=0;i<n;i++) {
if(a[i]==t) {
flag =1;
break;
}
}
for(int i=0;i<n;i++) {
if(b[i]==t) {
flag=2;
break;
}
}
if(flag==1) {
System.out.println("Rs.10000 Cash Prize");
}
else if(flag==2) {
System.out.println("Tour Tickets for two days");
}
else {
System.out.println("Better luck next time");
}
}

OUTPUT:
Enter the no of elements:
10
Enter the first set:
2 4 7 11 16 22 29 37 46 56
Enter the second set:
1 5 9 10 13 18 19 22 25 28
Enter the coupon:
16
Sometimes later becomes never. DO IT NOW!
6
SELF PRACTICE
SDE Readiness Training
Rs.10000 Cash Prize

7 XYZ College asked their students to register for NSS and NCC if they are Easy
willing. Some of the students registered for both. Identify them if
student ids(numeric) for each group is the input. Sample Input: 10, 10, [2 4
7 11 16 22 29 37 46 56], [1 4 7
10 13 16 19 22 25 28]
Sample Output: 4 7 16 22

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.println("Enter the no of elements:");
int n = s.nextInt();
int[] a = new int[n];
int[] b = new int[n];
int[] f = new int[n];
int flag = 0;
System.out.println("Enter the first set:");
for(int i=0;i<n;i++) {
a[i]=s.nextInt();
}
System.out.println("Enter the second set:");
for(int i=0;i<n;i++) {
b[i]=s.nextInt();
}
int t=0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(a[i]==b[j]) {
f[t]=a[i];
t++;
}
}
}
for(int i=0;i<t;i++) {
System.out.println(f[i]);
}
}

OUTPUT:
Enter the second set:
1 4 7 10 13 16 19 22 25 28
4
7
16
22

Sometimes later becomes never. DO IT NOW!


7

You might also like