Skip to content

Commit dd05680

Browse files
Create 1D Array
1 parent f4a9c23 commit dd05680

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

1D Array

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/* Largest Number At Least Twice of Others
2+
You are given an integer array nums where the largest integer is unique.
3+
Determine whether the largest element in the array is at least twice as much as every other number in the array.
4+
If it is, print the index of the largest element, or print -1 otherwise. */
5+
6+
import java.util.*;
7+
public class Main
8+
{
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
int n =sc.nextInt();
12+
int a[] = new int[n];
13+
int max = Integer.MIN_VALUE;
14+
int maxindex =0;
15+
for(int i=0;i<n;i++){
16+
a[i]=sc.nextInt();
17+
if(a[i]>max){
18+
max =a[i];
19+
maxindex =i;
20+
}
21+
22+
}
23+
boolean flag = false;
24+
for(int i=0;i<n;i++){
25+
if( i!=maxindex && max < 2*a[i]){
26+
flag = true;
27+
}
28+
}
29+
if(flag){
30+
System.out.println(-1);
31+
}
32+
else{
33+
System.out.println(maxindex);
34+
}
35+
36+
}
37+
}
38+
39+
40+
/* PEAK ELEMENT
41+
An element is called a peak element if its value is not smaller than the value of its adjacent elements(if they exists). Given an array arr[] of size n, find the index of first peak element. If peak element does not exist print -1.
42+
43+
Input
44+
line 1: contains an integer n denoting size of array.
45+
46+
line 2: contains n spaced integers denoting elements of array.
47+
48+
Output
49+
Print a single integer denoting the index of first peak element in array. If no such element exists, print -1. */
50+
import java.util.*;
51+
public class Main
52+
{
53+
public static void main(String[] args) {
54+
Scanner sc = new Scanner(System.in);
55+
int n =sc.nextInt();
56+
int a[] = new int[n];
57+
for(int i=0;i<n;i++){
58+
a[i]=sc.nextInt();
59+
60+
}
61+
if(n==1){
62+
System.out.println(0);
63+
return;
64+
}
65+
if(a[0]>a[1] ){
66+
System.out.println(0);
67+
return;
68+
}
69+
70+
for(int i=1;i<n-1;i++){
71+
if(a[i]>a[i+1] &&a[i]>a[i-1]){
72+
System.out.println(i);
73+
return;
74+
}
75+
}
76+
77+
if(a[n-1]>a[n-2]){
78+
System.out.println(n-1);
79+
return;
80+
}
81+
82+
System.out.println(-1);
83+
84+
}
85+
}
86+
/* SUM OF ARRAY EXCEPT SELF */
87+
import java.util.*;
88+
public class Main
89+
{
90+
public static void main(String[] args) {
91+
Scanner sc = new Scanner(System.in);
92+
int n =sc.nextInt();
93+
int a[] = new int[n];
94+
int sum =0;
95+
for(int i=0;i<n;i++){
96+
a[i]=sc.nextInt();
97+
sum += a[i];
98+
99+
}
100+
for(int i=0;i<n;i++){
101+
System.out.print(sum-a[i] +" ");
102+
}
103+
104+
105+
}
106+
}
107+
// largest and second largest number in an array
108+
109+
import java.util.*;
110+
public class Main
111+
{
112+
public static void main(String[] args) {
113+
Scanner sc = new Scanner(System.in);
114+
int n =sc.nextInt();
115+
int a[] = new int[n];
116+
int max =Integer.MIN_VALUE;
117+
int secondmax = Integer.MIN_VALUE;
118+
for(int i=0;i<n;i++){
119+
a[i]=sc.nextInt();
120+
if(a[i]>max){
121+
secondmax = max;
122+
max = a[i];
123+
}
124+
else if(a[i]>secondmax){
125+
secondmax =a[i];
126+
}
127+
}
128+
129+
System.out.println(max);
130+
System.out.println(secondmax);
131+
132+
}
133+
}
134+
135+

0 commit comments

Comments
 (0)