-
Notifications
You must be signed in to change notification settings - Fork 1
/
Day 33.1.txt
104 lines (91 loc) · 2.34 KB
/
Day 33.1.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
747. Largest Number At Least Twice of Others
You are given an integer array nums where the largest integer is unique.
Determine whether the largest element in the array is at least twice as much as every other number in the array. If it is, return the index of the largest element, or return -1 otherwise.
Example 1:
Input: nums = [3,6,1,0]
Output: 1
Explanation: 6 is the largest integer.
For every other number in the array x, 6 is at least twice as big as x.
The index of value 6 is 1, so we return 1.
Example 2:
Input: nums = [1,2,3,4]
Output: -1
Explanation: 4 is less than twice the value of 3, so we return -1.
Example 3:
Input: nums = [1]
Output: 0
Explanation: 1 is trivially at least twice the value as any other number because there are no other numbers.
Constraints:
1 <= nums.length <= 50
0 <= nums[i] <= 100
The largest element in nums is unique.
class Solution {
static int a[];
static int b[];
public int dominantIndex(int[] n) {
if(n.length==1)
return 0;
a=new int[n.length];
b=new int[n.length];
int i;
for(i=0;i<n.length;i++)
{
a[i]=n[i];
b[i]=n[i];
}
task(a,0,i-1);
for(i=0;i<a.length;i++)
System.out.print(a[i]);
if(a[a.length-1]>=(a[a.length-2]*2))
{
for(i=0;i<n.length;i++)
{
if(a[a.length-1]==b[i])
return i;
}
}
return -1;
}
void task(int n[] , int i , int j)
{
if(i<j)
{
int m=(i+j)/2;
task(n,i,m);
task(n,m+1,j);
task1(n,i,m,j);
}
}
void task1(int n[] , int s , int m , int e)
{
int c[]=new int[e-s+1];
int i,j,k=0;
i=s;
j=m+1;
while(i<=m&&j<=e)
{
if(n[j]>=n[i])
{
c[k++]=n[i];
i++;
}
else
{
c[k++]=n[j];
j++;
}
}
while(i<=m)
{
c[k++]=n[i++];
}
while(j<=e)
{
c[k++]=n[j++];
}
for(i=s;i<=e;i++)
{
a[i]=c[i-s];
}
}
}